Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declaration of variables

Reply
Thread Tools

Declaration of variables

 
 
Gaijinco
Guest
Posts: n/a
 
      03-06-2006
I have always felt that you should only declared variables as needed,
and implicitily it seems many authors to encourage it, but the other
day a friend told me that declaring variables inside a loop wasn't good
practice, something like:

for(int i=0; i<size-1; ++i){
int aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

should be written like:

int aux;
for(int i=0; i<size-1; ++i){
aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

What do you think? Thaks!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-06-2006
Gaijinco wrote:
> I have always felt that you should only declared variables as needed,
> and implicitily it seems many authors to encourage it, but the other
> day a friend told me that declaring variables inside a loop wasn't good
> practice, something like:
>
> for(int i=0; i<size-1; ++i){
> int aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> should be written like:
>
> int aux;
> for(int i=0; i<size-1; ++i){
> aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> What do you think? Thaks!


I think it's your friend who should _prove_ it to you that there is some
advantage to his method.

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-06-2006
Gaijinco wrote:

> I have always felt that you should only declared variables as needed,
> and implicitily it seems many authors to encourage it, but the other
> day a friend told me that declaring variables inside a loop wasn't good
> practice, something like:
>
> for(int i=0; i<size-1; ++i){
> int aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> should be written like:
>
> int aux;
> for(int i=0; i<size-1; ++i){
> aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> What do you think? Thaks!


Your friend might think that the first code is slower. In some few similar
cases it might indeed be slower. You shouldn't worry about that.

Tell your friend that premature optimization is the root of all evil. The
most important resource to optimize is programmer time. Programmers must
write clean code first, because it's easy to make beautiful code fast than
fast code beautiful.

After your for loop ends, aux's only meaning is "the last variable in the
array". Subsequent statements should not rely on the for loop "leaking" its
last aux value out. If they want the last value in the array, they should
get it themselves. They should decouple as much as possible from the rest
of your program, including the statements just before them.

Always give any identifier the narrowest scope and access possible. Don't
use global variables or public data members. Never create a variable
without giving it an initial value. Put all these rules together, and you
have many reasons to put int aux inside the loop.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      03-06-2006
Gaijinco wrote:
> I have always felt that you should only declared variables as needed,
> and implicitily it seems many authors to encourage it, but the other
> day a friend told me that declaring variables inside a loop wasn't good
> practice, something like:
>
> for(int i=0; i<size-1; ++i){
> int aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> should be written like:
>
> int aux;
> for(int i=0; i<size-1; ++i){
> aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> What do you think? Thaks!
>

Since `aux' is only meaningful inside the loop why not limit its scope
to the block inside the loop?

On the other hand, there are situations where you wouldn't want to do
this, when, for example, the variable in question is of a type that
requires significant construction overhead. This, however, seems not to
be one of those situations.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-06-2006
Gaijinco posted:

> I have always felt that you should only declared variables as needed,
> and implicitily it seems many authors to encourage it, but the other
> day a friend told me that declaring variables inside a loop wasn't good
> practice, something like:
>
> for(int i=0; i<size-1; ++i){
> int aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> should be written like:
>
> int aux;
> for(int i=0; i<size-1; ++i){
> aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
> }
>
> What do you think? Thaks!



Here's how I'd to do it:

for (int aux, i = 0; i < size -1; ++i)
{
...
}


If I had to pick one of the two ways you posted, then I'd put the
variable declaration outside the for loop. Does it make a difference?
Probably not... but at least your safe in the thought that memory isn't
being continuously allocated and deallocated for each iteration of the
loop.

Also as another Artie said, if you dealing with something like an
std::string, then definitely put it *outside* the loop.

The final reason is that I like consistency; if I'm going to put
std::string outside the loop, then I'll put an "int" outside the loop
too.

-Tomás

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      03-06-2006
On 6 Mar 2006 12:13:48 -0800, "Gaijinco" <> wrote:

>I have always felt that you should only declared variables as needed,
>and implicitily it seems many authors to encourage it, but the other
>day a friend told me that declaring variables inside a loop wasn't good
>practice, something like:
>
>for(int i=0; i<size-1; ++i){
> int aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
>}
>
>should be written like:
>
>int aux;
>for(int i=0; i<size-1; ++i){
> aux = array[i];
> array[i]=array[i+1];
> array[i+1]=aux;
>}


Why? Both versions are essentially the same. The performance is the
same. The second version is bad style because it doesn't obey to the
'minimal scoping rule'.

>What do you think? Thaks!


Your friend has gone astray. Lead him back to the right path.
Roland Pibinger
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-06-2006
Tomas wrote:

> Here's how I'd to do it:
>
> for (int aux, i = 0; i < size -1; ++i)
> {
> ...
> }


Why?

The scope is not as narrow as possible. It's one tick wider, into a scope
where it's not needed.

> If I had to pick one of the two ways you posted, then I'd put the
> variable declaration outside the for loop. Does it make a difference?
> Probably not... but at least your safe in the thought that memory isn't
> being continuously allocated and deallocated for each iteration of the
> loop.


That's premature optimization.

Further, the earliest definitions of C state that storage for all local
variables in a function allocate when the function enters, regardless of
their scope. I'm sure The Standard has since mutilated that requirement,
but I can't imagine a compiler doing it any other way.

> Also as another Artie said, if you dealing with something like an
> std::string, then definitely put it *outside* the loop.


Why? Have you time tested it?

What if std::string inside a loop fit in your CPU cache, but outside the
loop it overflowed your cache and caused memory thrashing?

> The final reason is that I like consistency; if I'm going to put
> std::string outside the loop, then I'll put an "int" outside the loop
> too.


Then put it consistently inside the loop. This improves the odds it will go
away!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-06-2006
Tomás posted:

> Gaijinco posted:
>
>> I have always felt that you should only declared variables as needed,
>> and implicitily it seems many authors to encourage it, but the other
>> day a friend told me that declaring variables inside a loop wasn't

good
>> practice, something like:
>>
>> for(int i=0; i<size-1; ++i){
>> int aux = array[i]; array[i]=array[i+1]; array[i+1]=aux; }
>>
>> should be written like:
>>
>> int aux;
>> for(int i=0; i<size-1; ++i){
>> aux = array[i]; array[i]=array[i+1]; array[i+1]=aux; }
>>
>> What do you think? Thaks!

>
>
> Here's how I'd to do it:
>
> for (int aux, i = 0; i < size -1; ++i)
> {
> ...
> }
>
>
> If I had to pick one of the two ways you posted, then I'd put the
> variable declaration outside the for loop. Does it make a difference?
> Probably not... but at least your safe in the thought that memory isn't
> being continuously allocated and deallocated for each iteration of the
> loop.
>
> Also as another Artie said, if you dealing with something like an
> std::string, then definitely put it *outside* the loop.
>
> The final reason is that I like consistency; if I'm going to put
> std::string outside the loop, then I'll put an "int" outside the loop
> too.
>
> -Tomás



Furthermore, if your worried about scope, God gave us curly braces for a
reason:

int main()
{
//Some code

{
int aux;
for (int i; i < size -1; ++i)
{
//Some Code
}
}

//Some more code
}


-Tomás
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      03-06-2006
"Tom?s" <> wrote:
> The final reason is that I like consistency; if I'm going to put
> std::string outside the loop, then I'll put an "int" outside the loop
> too.


Found these quotes in the FAQ:

"Consistency is good, but it is not the greatest good" [29.5]

"The real problem is that people tend to worship consistency, and they
tend to extrapolate from the obscure to the common. That's not wise."
[10.19]

(Granted, they are talking about different situations, but nonetheless
they are good pieces of advice to keep in mind in general.)

--
Marcus Kwok
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      03-06-2006
Tomás wrote:
> Gaijinco posted:
>
>
>>I have always felt that you should only declared variables as needed,
>>and implicitily it seems many authors to encourage it, but the other
>>day a friend told me that declaring variables inside a loop wasn't good
>>practice, something like:
>>
>>for(int i=0; i<size-1; ++i){
>> int aux = array[i];
>> array[i]=array[i+1];
>> array[i+1]=aux;
>>}
>>
>>should be written like:
>>
>>int aux;
>>for(int i=0; i<size-1; ++i){
>> aux = array[i];
>> array[i]=array[i+1];
>> array[i+1]=aux;
>>}
>>
>>What do you think? Thaks!

>
>
>
> Here's how I'd to do it:
>
> for (int aux, i = 0; i < size -1; ++i)
> {
> ...
> }
>
>
> If I had to pick one of the two ways you posted, then I'd put the
> variable declaration outside the for loop. Does it make a difference?
> Probably not... but at least your safe in the thought that memory isn't
> being continuously allocated and deallocated for each iteration of the
> loop.
>
> Also as another Artie said, if you dealing with something like an
> std::string, then definitely put it *outside* the loop.


I think you've overstated my comments to an extent; I'm not sure I would
call a std::string something that involves `significant construction
overhead'. In any event, as noted elsethread, it's an optimization in
any case and not to be undertaken lightly.
>
> The final reason is that I like consistency; if I'm going to put
> std::string outside the loop, then I'll put an "int" outside the loop
> too.


It's only worth the ugliness of doing that if it's demonstrably worth
that ugliness in terms of *needed* performance.

Whenever possible localilty is *good*!

Cheers,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57