Jan/087
Variable Life Time
We all use loads of variables in our code and we want to ensure that it is easy to follow their usage. One way to improve is to think about the variable life time.
Variable life time means that you should keep your variable declaration and initialization as near as possible to it’s first usage. Let’s take a bad example :
int x;
int y;
for (x = 0; x < 10; x++)
{
}
for (y = 0; y < 20; y++)
{
}
Now the problem in there is that the y – variable is not declared near to it’s usage. The x – variable in other hand is in the right place, it is as near as possible to it’s first usage. Now let’s try to make it better :
int x;
for (x = 0; x < 10; x++)
{
}
int y;
for (y = 0; y < 20; y++)
{
}
See the difference? Now code is easier to read and understand, but to really see the power of this you’ll have to try it in a bit more complex case.
Enjoy this article?
Leave a comment
No trackbacks yet.
2:41 pm on January 11th, 2008
I have a problem with your example. Both x and y live beyond the scope they’re used in. I’d go with:
for(int x = 0; x < 20; x++)
{
}
Declaration and initialization at the same time, and no useless x alive anymore after the loop, either. Still a magical number but oh well. It’s ‘just an example’ I guess.
2:56 pm on January 11th, 2008
Can you do that with C-88?
I’m trying to write my examples so that they are compatible with C-88->/C++.
Yeah, the purpose of this example was not to show magic numbers.
5:39 pm on January 11th, 2008
If something is specific to C, then mark it as such. C and C++ are different languages, that encourage different approaches. A lot of C++ beginners are essentially writing C with classes because they’re never taught what alternative approaches C++ provides.
For C, this would be a nice tip. I’d be even better if you included anynomous scopes in it, since that’s also usefull for controlling variabele lifetimes. If that wasn’t around in C-88, then just mention since what version it was introduced. But for C++, I find this post subtly deceptive.
6:33 pm on January 11th, 2008
The main point is to show what does the variable life time mean in code. You can use any language you want to.
I probably just edit all to pseudo – C as I don’t want to raise any language fights around here.
There is a scope after x – loop.
6:41 pm on January 11th, 2008
Alright, changed to pseudo stuff.
And thanks a lot for the feedback again!
7:14 pm on January 11th, 2008
C or C++ specific examples aren’t bad, it’s just that whenever you post something specific, it’s good practice to mention it.
Perhaps something like: this is how you would do it in C: _C EXAMPLE CODE_, though in C++ you can also do: _C++ EXAMPLE CODE_. The core idea is still the same, except that some languages are more restricting than others when it comes down to the actual implementation.
Hey, after all, posts can be put into multiple categories at the same time, so why not also slap a C or C++ tag on specific posts?
7:56 pm on January 11th, 2008
True.
I’ll stick with the pseudo code for now and if I’ll need to do more specific I’ll tag it.
Thanks!