Matt wrote:[color=blue]
>
> This did help somewhat.. what I mean is.. if the main reason for not
> using globals is to prevent accidental change.. then shouldn't I just
> be more careful when programming?[/color]
Lot's of people thought this and all of them came to the same conclusion
eventually:
No. Simply beeing careful doesn't help.
The reason is that programs very quickly cross a 'critical mass'. Once
this critical mass is crossed, the complexity raises that fast, that
it is extremely hard or impossible to recognize all the details.
By using local variables you draw a border. The border limits the scope
of that variable. It is as if you were saying: Your influence region is
up to here but no further.
[color=blue]
> Couldn't I do just as much damage
> passing a variable local into a function and then back out?[/color]
No. Cause it you pass something to a function you want to tell the function
something: use this value or calculate and place the result here.
You are using a defined interface and that's ok.
It is like a house: if you don't make any window or doors in your house,
then you don't need to worry of getting robbed. But what good is a house
without a door and/or windows? So you need to lower the security a little
bit in order to make it useful. But this does not mean that you want to
build your house without walls :-)
[color=blue]
> The only
> difference with a global is I messed up coding...[/color]
Not really.
[color=blue]
> but isn't it ALSO
> just as bad coding practice to use the same variable locally that is
> defined in another function locally?[/color]
No, why should it?
Those 2 variables have nothing in common. They just happen to have
the same name, but this isn't a problem, they are still independent of
each other. If you are working on one function, then you are interested in
the details of that function. If there is a local variable called 'count', fine.
Then you switch and are working on another function. There is also a variable called
'count', but since you don't use globals, you know that this is a completely
different variable then the one you previously used in another function.
So you also know (since both are global), that you can change each count
without affecting the operation of the other function. You simply don't care
any longer, that there is another function which uses a local variable 'count'.
[color=blue]
> I mean if you use the variable
> once you shouldn't use it again,[/color]
That would be hard to do in real world programs. The system my team is working
on consists of roughly 1 million lines of code, there are at least 60000 up to
100000 (I estimated this numbers, since nobody knows exactly) variables and you
tell me that I shouldn't use a variable called 'count' in more then one function?
If it is a counter and I use it as a counter I will call it 'count' and not worry
about all the other 5000 counter which are used throughout the whole program.
[color=blue]
> so why not just define it globally
> and watch what you do??[/color]
Because you can't watch them. At least not when you are writing serious programs
with sizes greater then 50 lines of code.
--
Karl Heinz Buchegger
kbuchegg@gascad.at