• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Effective Java : Minimize the scope of local variables

 
Marshal
Posts: 5316
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is Item 45 in the book and is categorized in the "General Programming" section where Bloch discusses some techniques to be used down in the language 'nuts and bolts'.

The first rule for this item is

Effective Java wrote:The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used


It just sounds really bloomin' obvious but I still see Java code with a block of local var declarations as the first thing, then the code. Granted a lot of this is old code but I do still see the odd new method written in this style too, and by young programmers too. Somebody somewhere is still teaching this old C hangover to new Java programmers.

I really like this quote and think it's a great little rule to work by

Effective Java wrote:If you don't yet have enough information to initialize a variable sensibly, you should postpone the declaration until you do.


What do you think about this item? Do you still see local variable declaration blocks up front? Do you still write local variable declaration blocks up front?
 
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is in fact one of the things I like about java, and it has become sort of ingrained now- declaring local variables where I need to use. So far never seemed to have any issues with this.
 
Tim Cooke
Marshal
Posts: 5316
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another nice thing about keeping your variable declarations next to your code is that you can more easily spot when your method is doing too much. Take the following for example:

There's a high chance that this method is doing two independent things and could (should) bet split into two methods. Having the variables defined close to the code that operates on it makes this smell quite obvious to spot. But it it were done like this:

Now it's not so obvious and you have to trawl through all of the code to check for the variable usage.
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow.. your example gave a new perspective, thanks.
 
Marshal
Posts: 75615
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been in our style suggestions for a long time.
 
author & internet detective
Posts: 41071
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was in school, they taught to put all declarations together. But they also didn't teach not to have long methods. I've moved to better practices in both those areas over time!

I have seen the variables up front. Some people don't realize they should move them. Some people think it is a "performance issue" to keep redeclaring the variable. Fallacies abound.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic