In VB it was best practice to define all your variables at the top of a procedure.
Is this the same with Java?
Printable View
In VB it was best practice to define all your variables at the top of a procedure.
Is this the same with Java?
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.
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.
I believe that the second line is more clear than the first one.Code:addNumbers
add_Numbers
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.
Yap, I used Java Sun recommended naming conversion, and based on that I put my thoughts on post #3.