declare variable inside or outside block?

A

Angus Parvis

Hi,

I just asked myself, whether it is better to declare a variable inside
or outside a block, if you only want to use it inside the block;

I always do it like this:

String foo;
while (true) {
foo = new String();
}

Guess that's better than:

while (true) {
String foo = new String();
}

In the second example i declare the variable String foo every time the
loop is repeated - guess that's the worse solution, isn't it?

Well, I'd be interested in your comments on this,

thanks in advance,

Angus
 
B

Boudewijn Dijkstra

Angus Parvis said:
Hi,

I just asked myself, whether it is better to declare a variable inside or
outside a block, if you only want to use it inside the block;

I always do it like this:

String foo;
while (true) {
foo = new String();
}

Guess that's better than:

while (true) {
String foo = new String();
}

In the second example i declare the variable String foo every time the loop
is repeated - guess that's the worse solution, isn't it?

Declaration happens only inside the compiler. The JVM operates on numbered
variables; in both cases 'foo' gets the same number every iteration. What
remains is a matter of variable scope, rendering the second example the better
one, provided that 'foo' is only needed inside the loop.
 
A

Andrew McDonagh

Angus said:
Hi,

I just asked myself, whether it is better to declare a variable inside
or outside a block, if you only want to use it inside the block;

I always do it like this:

String foo;
while (true) {
foo = new String();
}

Guess that's better than:

while (true) {
String foo = new String();
}

In the second example i declare the variable String foo every time the
loop is repeated - guess that's the worse solution, isn't it?

Well, I'd be interested in your comments on this,

thanks in advance,

Angus

Hi,

Actually the second is 'better' as its only needed within the block. Do
not be fooled into thinking that the multiple declaration within the
block would have dramatic performance issues - it doesn't.

By declaring references as close to their actual use, it will enable
easier refactorings as well as communicate their first usage easier.

Its (was) typical for C programs to have all local variables defined at
the beginning of a function, but this isn't (IMHO) a good practice.

Andrew
 
A

Angus Parvis

thank you for your answers. very interesting, i think i'll have to
change my coding style :)

Angus
 
T

Thomas Kellerer

Angus Parvis wrote on 02.01.2005 00:19:
Hi,

I just asked myself, whether it is better to declare a variable inside
or outside a block, if you only want to use it inside the block;

I always do it like this:

String foo;
while (true) {
foo = new String();
}

Guess that's better than:

while (true) {
String foo = new String();
}

In the second example i declare the variable String foo every time the
loop is repeated - guess that's the worse solution, isn't it?

Well, I'd be interested in your comments on this,

thanks in advance,

Angus


In contrast to the other two answers I prefer the first version. The simple
reason: it's easier to debug, because the variable does not go out of scope
each time the start of the loop is reached, so I can add a watch more easily.

Thomas
 
T

The Abrasive Sponge

Angus said:
Hi,

I just asked myself, whether it is better to declare a variable inside
or outside a block, if you only want to use it inside the block;

I always do it like this:

String foo;
while (true) {
foo = new String();
}

Guess that's better than:

while (true) {
String foo = new String();
}

In the second example i declare the variable String foo every time the
loop is repeated - guess that's the worse solution, isn't it?

Well, I'd be interested in your comments on this,

thanks in advance,

Angus


If I need reference to foo after the while loop I use the first if I
don't I use the second.
 
C

Chris Smith

Andrew McDonagh said:
Its (was) typical for C programs to have all local variables defined at
the beginning of a function, but this isn't (IMHO) a good practice.

Though this is a bit off-topic for this newsgroup, the "typical" thing
in C is to declare variables at the beginning of a block. This isn't
just "typical" in C, but in fact is required by the C language
specification prior to C99. Declaring variables anywhere else has been
non-standard C for most of the C language's lifetime, though it's
generally supported by many "C/C++" compilers, since it's valid in the
C++ language.

I believe (but am not sure) that this changes with C99. However, it
seems somewhat rare to find compilers that are fully compliant with C99,
so in practical terms you're either declaring variables at the beginning
of a block, or not writing valid ANSI C code.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
H

hilz

Angus Parvis said:
Hi,

I just asked myself, whether it is better to declare a variable inside
or outside a block, if you only want to use it inside the block;

I always do it like this:

String foo;
while (true) {
foo = new String();
}

Guess that's better than:

while (true) {
String foo = new String();
}

In the second example i declare the variable String foo every time the
loop is repeated - guess that's the worse solution, isn't it?

Well, I'd be interested in your comments on this,

thanks in advance,

Angus

I like to agree with others that declaring variables as close to their scope
as possible is a better idea.
I just like to ask why would you do:
String foo= new String();
This does not seem to be the correct thing to do.
what you probably need is:
String foo = someOtherString;
or
String foo = "some hardcoded string";

String foo = new String() does not seem to serve any purpose to me. Does
anybody care to comment on this?

thanks
hilz
 
A

anonymous

hilz said:
I like to agree with others that declaring variables as close to their scope
as possible is a better idea.
I just like to ask why would you do:
String foo= new String();
This does not seem to be the correct thing to do.
what you probably need is:
String foo = someOtherString;
or
String foo = "some hardcoded string";

String foo = new String() does not seem to serve any purpose to me. Does
anybody care to comment on this?

thanks
hilz

I believe you're right. Oracle newest JDeveloper beta will flag this
code and suggest to change the new String() to just assigning a value to
the variable.
 
A

Andrew McDonagh

hilz wrote:

snipped.
String foo = new String() does not seem to serve any purpose to me. Does
anybody care to comment on this?

thanks
hilz

Its the same as:

String foo = "";

Both are fine - though I prefer the second.

The only problem I've seen with the first one, is when the constructor
is later given an argument, as in...

String foo = new String("someString");

Which actually ends up creating two string objects just to initialise
the foo ref.
 
J

Joona I Palaste

Andrew McDonagh said:
hilz wrote:
Its the same as:
String foo = "";
Both are fine - though I prefer the second.
The only problem I've seen with the first one, is when the constructor
is later given an argument, as in...
String foo = new String("someString");
Which actually ends up creating two string objects just to initialise
the foo ref.

It's actually not quite the same thing. Using new String(), either
with or without a String parameter, always gives you a reference to a
freshly created String object that is distinct from any other String
object. However, using the construct String foo = ""; or equivalent
can, depending on the compiler settings, give you a reference to an
already existing String object, without creating a new object at all.
The difference can be observed by using the == operator instead of the
equals() method.
 
C

Chris Smith

hilz said:
I like to agree with others that declaring variables as close to their scope
as possible is a better idea.
I just like to ask why would you do:
String foo= new String();
This does not seem to be the correct thing to do.
what you probably need is:
String foo = someOtherString;
or
String foo = "some hardcoded string";

I'd go a step further. Since this is a local variable, you should do
something like this by default:

String foo;

What this does is declare a variable, but give it no defined value. If
you try to use the variable before assigning a value to it, the
*compiler* will let you know. Any time you need to declare a variable
but there's no initial value needed for it to make the code correct,
this is what you should do.

If foo needs an initial value for the code to be correct, of course,
then you give it one; but in that case, it's silly to be guessing at
what it should be when we don't know how you're using it. Maybe it
should be null, or maybe it should be "abracadabra". We simply don't
know.
String foo = new String() does not seem to serve any purpose to me. Does
anybody care to comment on this?

Joona's answer is the whole story. Since it's really bad form to depend
on instance identity for String objects, using "" should be functionally
identical to new String()... but it is nevertheless possible to observe
a difference between the two.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
H

Harish

A variable declaration inside means that the variable has to be created for
each iteration.
For eg:
while (true) {
String foo = new String();
}
if the loop executes say 100 times, the variable foo will be created 100
times. (I am not talking about the "new String()" part, but the creation of
the foo: creation of a reference to store the
string)
now if the varaible was declared outside the loop, this won't happen, and
you get yourself a better code.

They(?) also say, a good optmizing compiler will be smart enough to detect
these and handle'em appropriately.
Also, in java, a reference is only a couple of bytes. So there is nuthing
much one can save by moving the decl outside.
 
J

Joona I Palaste

Harish said:
A variable declaration inside means that the variable has to be created for
each iteration.
For eg:
while (true) {
String foo = new String();
}
if the loop executes say 100 times, the variable foo will be created 100
times. (I am not talking about the "new String()" part, but the creation of
the foo: creation of a reference to store the
string)
now if the varaible was declared outside the loop, this won't happen, and
you get yourself a better code.

This, of course, is nonsense. The variable foo is only created at
compile time, and at compile time it doesn't matter at which scope the
variables are in, they are handled the same way anyway. The compiler
reserves a few bytes of space in the variable table for each variable
and translates all uses of the variable into use of its space in the
table.
Now, when the while loop actually runs, foo isn't created at all any
more. The only thing that is created is new String(). The way the
run-time environment sees it, it's creating a new String and assigning
a reference to it into some space in the variable table. Where that
space came from, the environment doesn't know or care.
They(?) also say, a good optmizing compiler will be smart enough to detect
these and handle'em appropriately.

Make that "any compiler anywhere, optimising or not". Compilers that
actually ran while loops while translating variable names into spaces in
the variable table would be downright idiotic.
It has nothing to do with optimising. It's a matter of basic compiler
operation.
Also, in java, a reference is only a couple of bytes. So there is nuthing
much one can save by moving the decl outside.

This is the only part that is absolutely true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top