|
-
May 11th, 2007, 04:28 AM
#1
Thread Starter
Frenzied Member
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?
-
May 11th, 2007, 04:42 AM
#2
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.
-
May 22nd, 2007, 10:14 PM
#3
PowerPoster
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
-
May 23rd, 2007, 12:47 PM
#4
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.
-
May 23rd, 2007, 09:38 PM
#5
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|