• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Location of declaration / Initialization of local variables (S&B self contradiciton?)

 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Where should local method variables be declared / initialized? At the beggining to the block they are used, or just before use. I am a tad confused as in the Sierra & Bates book (bought about a year ago, so there may be a new edition by now) there seems to be conflicting advice:

p584: Declare and initialize local variables before other statements (whenever possible).
Declare and initialize block variables before other statements (whenever possible).

However:
p589: Wait to declare a local variable until just before it's used. And you should always initialize a local variable at the time its declared (which is just before use).

If in a method I need a local variable that is used near the end of the method, where should the variable be declared / initialized? At the beginning as p584, or just before it's used (near the end) as p589?

Thanks,

Michal

Edit: Actually I think I bought the S&B book two years ago.

Edit: Removed smiley that appeared where it shouldn't

[ June 06, 2005: Message edited by: Michal Charemza ]

[ June 06, 2005: Message edited by: Michal Charemza ]
[ June 06, 2005: Message edited by: Michal Charemza ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The confusion stems from the fact that on one hand, most experts would agree that local variables should be declared and initialized right before they are used, and on the other hand, the Sun Java Coding Coventions prescribe declaring the local variables at the beginnig of the method. Unfortunately, those coding conventions have not been updated since 1999, so the "unprofessional" advice is still there.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal:

Just my two cents...ultimately, it is entirely up to you...I think this is a classic common sense decision. Personally, I declare variables that are used all over a method at the very top. On the other hand, variables that are used in highly specialized, few limited lines of code I declare right before they are used.

I initialize immediately after declaring in all cases (call it hangover from C).

As a matter of context, there are a lot of C (not C++) compilers/interpreters out there
that still enforce the rule that all local variables must be declared at the top of the method (from time to time, I use a proprietary Adobe API that enforces this due to macro definitions...it drives me nuts...). This may have been the origins of this practice, not to say it does not make code easier to understand under certain conditions...

In either case, I don't think Sun enforces this as a "matter of law" and neither does any sensible company that I know of...

Reza
[ June 06, 2005: Message edited by: Reza Rahman ]
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Smith:
The confusion stems from the fact that on one hand, most experts would agree that local variables should be declared and initialized right before they are used,

Can I ask... why?

Michal
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal:

Here is the convention John is referring to: http://java.sun.com/docs/codeconv/html/CodeConventions.doc5.html#16817. Hope this helps.

Reza
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I ask... why?

The program source is much more readable if the local variables are declared and initialized immediately before their first use. Additionally, you are reducing the scope of that variable and thereby the risk of it misused in any form. Think of it in terms of the "minimize access privileges" rule. Just like you would prefer to make your methods private (unless they really must be public), you would also prefer to have your local variable be accessible just where it is needed, and not before or after that.

Here is a recent JR discussion about it.
[ June 10, 2005: Message edited by: John Smith ]
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Smith:
[b]The program source is much more readable if the local variables are declared and initialized immediately before their first use.

I've always found code more readable with the declarations at the top of methods. Somehow it just seems less messy. Although, I suppose though it is good to be sure of what variables are when reading the source, and if the declarations are right were they are first used, this is easier.

Originally posted by John Smith:
[b]
Additionally, you are reducing the scope of that variable and thereby the risk of it misused in any form.

Is it then a good idea to use anonymous blocks within methods where possible, to declare and use variables in them?

Michal
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal:

Just my two cents...avoid anonymous blocks unless they are an absolute necessity.

Reza
 
Lara McCarver
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting local variable declarations near where they are used also improves maintainability for the code. Over time, people will change code to act differently. If they change the code to use new local variables, the compiler will force them to be declared. But if they simply stop using the old local variables, I do not believe the java compiler even gives a warning about this. Still later someone goes to investigate something related to this code, and will be confused about the variables which are declared at the beginning of the method and then never used.

In theory, you are supposed to keep all your methods short, so this issue isn't as important. But in practice... especially when you have all this exception handling, locking, etc. some of your methods that are the "meat" of your functionality do end up being a little bit long.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it then a good idea to use anonymous blocks within methods where possible, to declare and use variables in them?

That would be a bit extreme, with a side effect of reducing the source readability, so I agree with Reza -- don't do it unless doing something really special.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic