• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialize before or during  RSS feed

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the more acceptable way of assigning a value :




OR :




The question is basicaly where should I be placing the "String" word. Assume that the while loop is actually looping through lines of text pr something.

Thanks.
 
Masoud Kalali
Author
Ranch Hand
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think it more depend on your requirements.
but first one seems to be more acceptable and more readable.
( java String type is Immmutable so its not important from performace view to new a String variable each time or changing its value AFAIK).


I hope it helps
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about String in particular, or Object's in general?

The reason I ask is coz String is kind of special, in that it is immutable, as Massoud said. With non-immutable objects, it matters where you place the declaration, for performance and garbage collection.

Ben
SCJP 1.4
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I think this is a simple question of scope. If you need to reference the variable outside the scope of the while block then use the first declaration. If the variable is only referenced inside the while block, use the second.

There's no point declaring variables outside the scope in which they're needed. This only opens the door to bugs should the variable then be accidentally used or reused.

Jules
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to second, and amplify, Jules' response.This is bad practice, regardless of what the type of "var" is. In general, you should give variables the smallest scope possible. By giving "var" essentially method scope, anyone trying to understand the code needs to read the entire remainder of the method to verify that nothing else interacts with the variable[1]. In toy examples like this, that is not much of a problem, but in real code with nontrivial methods and half a dozen variables or more it will make a difference[2]. Code likeIs much more explicit and unambiguous in where it uses "var".

There's another take on this matter which is suggested by my use of the word "explicit" - the viewpoint of programmer intent. A piece of Java source is not primarily fodder for the compiler. It is first and foremost a statement of programmer intent which is human-readable in addition to being machine-readable. Most software expenditure does not go into its original creation, but into its bugfixing, maintenance and enhancement. Good source code is a clear, concise and complete statement of programmer intent. If your intent is to use "var" only in a limited scope, then that should be made as explicit as you can make it.

Performance should not enter the picture here; premature optimisation is the root of all evil in programming (Hoare). In any case, the two alternatives listed above compile down to exactly the same bytecode and there is actually no performance difference between them.

- Peter

[1] Unless (s)he happens to know that the compiler would complain about an possibly uninitialised variable if it were used elsewhere without (re-)initialisation. The intent is still too implicit, though.
[2] This is actually a special case of the observation that a surprising number of programming evils stem from, or are made possible by, the maintenance of state. Therefore it is a good thing to minimise the amount of state visible at any one time - local variables, instance variables, and so on. Obviously you can take this only so far unless you're a functional programmer
[ August 10, 2004: Message edited by: Peter den Haan ]
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They only compile down to the same thing because they are both String, right? I mean, if they were some non-immutable Object, they would not necessarily compile down to the same thing, right?

My point was that new Object()'s are expensive and must sooner or later be garbage-collected, and some trade-off thinking should be given to performance vs readability/maintainability, especially where iteration is involved. When java compiler optimization reaches a point where it handles this for us like, say, String concatenation, then that changes the balance of the equation...or are you saying that compiler optimization has come that far?

Ben
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!