Results 1 to 5 of 5

Thread: Coding Best Practice

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Coding Best Practice

    In VB it was best practice to define all your variables at the top of a procedure.
    Is this the same with Java?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Coding Best Practice

    I consider it best to declare variables only where and when they are needed and when you can give them a meaningful value. If you declare all variables at the top of a procedure, many of them will inevitably have to be uninitialised. Separating initialisation and declaration can cause problems and should be done only when absolutely necessary.

    This applies to all languages, except some (I think C is one) which explicitly require you to place declarations before statements.

  3. #3
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Coding Best Practice

    I believe that variables should be declared just before it is used.

    And also gives a meaningful names for variables as well. Better to initialized at the same time if you can.

    In Java, if the variable name is too long, don't worry just use the "_" sign to separate them. Say there is a variable which define addition of two numbers.

    Code:
    addNumbers
    
    add_Numbers
    I believe that the second line is more clear than the first one.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Coding Best Practice

    Sun has some recommended naming conventions for Java.

    The Table of Contents includes some other recommendations that might be useful.

    Obviously you don't have to follow them all, and there are some that I would do differently, but it's a good starting point.

    Hope this helps.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Coding Best Practice

    Yap, I used Java Sun recommended naming conversion, and based on that I put my thoughts on post #3.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width