Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local variables declarations

 
Eugenio Flores
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I would like to konw about local variables declarations.

I read the coding standard chapter of Sierra and Bates' book, and they say that "Declare and initialize local variables before other statements (whenever possible)".

So, what about variables that I'm goin to use down the code of a method? What is the point of having to declare all this variables near the method's declaration, just like we do with instance variables near the class declaration.

Like this:


I think if someone starts reading this methods he/she would not know what those variables are for, because they are going to be used deep down the method. Would be easier just to declare the variables just before we use them?
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They say this, and other say "declare and initialize variables only when they are needed, not sooner", so the exact opposite. I use the latter approach, and I passed, so you should be cool as well.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eugenio!

I also use the approach Raf said. I declare and initialize local variables when they are needed.
I justify that by pointing that variables should have smalles possible scope, so what about variable you use only in a for / while loop or if statement? By declaring it inside, you keep the smalles scope the variable should be used for.

I initialize instance variables as soon as possible (most likely when declaring).

I think some programmers appreciate style which says that you should "show" what variables you are using in the method, so they are prepared what lurks inside
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I need a local variable, I declare (and initialize) it at the spot (so I do not declare them all at the beginning of a method)
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Having variables declared at the head of a function makes the code more readable, for one. Though I do have a performance concern. Declaring variables at the start of a method gives them more visibility (for the life-time of the method) whereas if each is declared in the appropriate block as per the necessity makes them eligible for garbage collection earlier before the method has finished execution. I agree this aspect alone is not going to improve the performance in leaps and bounds. But why retain a resource (memory) for longer than it is required?

I would not say there are hard and fast rules relating to ideal ways for variable declaration/initialization. Depending on the requirements a hybrid model should be achieved.

Cheers,
Raj.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kamal wrote:Having variables declared at the head of a function makes the code more readable, for one.

Says who? Depends on what you are used to, for one. I, for one, find the other (only when needed) much more readable; all variables in the header remind me of the old-school coding, like in Pascal.

Raj Kamal wrote:whereas if each is declared in the appropriate block as per the necessity makes them eligible for garbage collection earlier before the method has finished execution

How is this possible? They will be visible from the point they are declared until the end of the block, which in the case described here is method block. So now, they cannot be collected earlier, unless you explicitly assign null to the references or do this dreadful thing:

The only concern could be that the objects that are allocated at the top of the method will not be necessary (for example, there is an if and they will be used only if something happens, or there is an exception). But, do you really care about this - the compilers are currently very very smart, they will optimize the code anyways.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Raf wrote

Says who? Depends on what you are used to, for one. I, for one, find the other (only when needed) much more readable; all variables in the header remind me of the old-school coding, like in Pascal.


[b]I[/b] said "Having variables declared at the head of a function makes the code more readable, for one." My opinion?

Raf said

I, for one, find the other (only when needed) much more readable;


Why "only when needed"? So at other times the vice versa is an good option?

As about the final point, I had already pointed out.

Raj said

I agree this aspect alone is not going to improve the performance in leaps and bounds. But why retain a resource (memory) for longer than it is required?


Raf said

But, do you really care about this


Not in the least! Thats why I mentioned the hybrid approach and there being no hard and fast rule for the question under discussion.


Cheers,
Raj.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic