Skip to main content
gamedev.net
🔒 Locked

2 really basic questions

Started by biohaz 3 replies 700+ views
Original Post
biohaz
biohaz
I have had 2 basic questions that I have always avoided, and did it my way, so I may as well ask them to see what is correct Snippet 1: use this:
 
int variable;
for(int y =0; y < 100; y++)
{
     variable = [anything];
}

or this:
  
for(int y =0; y < 100; y++)
{
    int variable = [anything];
}

Snippet 2: use this:
 
int variable = [anything];
int variable2 = [anything] + 1;
    
both are used throughout a entire class


or this:
 
int variable = [anything];

used throughout the class, but when we come to a place that needs what was variable2, just have variable + 1


Note these are just samples, I am using them in much more complex terms as well as simple ones like this. in both cases, I always went for the top case for both snippets, but in some cases the bottom one can be cleaner. What is faster/better/stronger?
Palidine
Palidine
in example 1. Both are correct for different situations:

a. this is correct if you want to use variable outside the scope of the for loop (i.e. outside the curly braces) or if you want the value to persist between iterations in the loop. variables only exist in the scope within which you declare them. So in a, variable is available both inside and outside the for loop.

b. this is correct if you only use variable inside the for loop. because it's declared within the for loop, it is only available inside the for loop. it should also be noted that it is a different variable with each iteration of the loop. i.e. values put into variable on y == 1 will not exist in y == 2

-me
biohaz
biohaz
ok, I was assuming that the variables are not used out of scope, It is alright to allocate for it each loop, or do I have it wrong?
Way Walker
Way Walker
Quote:
Original post by biohaz
ok, I was assuming that the variables are not used out of scope, It is alright to allocate for it each loop, or do I have it wrong?


If it's just an int or other POD type, you're golden, it should be optimized out. Otherwise, it's going to reconstruct (but probably not reallocate) the object each time. This may or may not be what you want. Also, if you're accumulating something in the variable (e.g. each new step has some dependence on the last step other than the loop's index variable) you should place the variable outside the loop (Since if it's in the loop you're technically handed a new variable each time so you shouldn't assume anything about its value).

As for the second, I'd say it depends on context. Is "variable + 1" expensive? (e.g. if variable is a class that's overloaded operator+()) Does "variable + 1" clearly express what's going on? Is "variable + 1" used often? Usually, I'd say it's better (more readable, faster, etc.) to use a second variable, but there are times I'd use just the one variable.
M2tM
M2tM
Sample 1 has been discussed well already.

Sample 2 has also been talked about, let me chime in. Basically, if you do it often enough that it's computationally expensive, or if it's confusing as the +1 looks like a "magic variable" being added to a defined value use the second variable.

If it's a temp counter that's only used once or twice then maybe the + 1 is more convenient.
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.