• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Local variables declarations

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic