943,922 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1258
  • C++ RSS
Oct 30th, 2008
0

else if's and global variables

Expand Post »
If you have multiple If statements happening to check the logic on something is it better to just use all else if's to make your code execute faster?

and I understand that people always say that it is better programming to keep your variables as far from being global as possible, but why is this? what does that actually improve within the code of the program?
Similar Threads
Reputation Points: 12
Solved Threads: 4
Junior Poster
cam875 is offline Offline
170 posts
since Jun 2008
Oct 30th, 2008
0

Re: else if's and global variables

IMO, serial ifs should be used if you want every condition to be checked. if/else/else ifs are used if you want to stop once one of the conditions is positive and don't need to assess any further conditions.

Global scope means any program/code can open your code and use/change the value stored in the variable---maybe even remove it! You can help protect the integrity of the data by using only it in the appropriate scope for which it is needed. Windows code uses global variables quite extensively, so it can be done with a fair amount of success and safety, but it isn't consider "proper" form when there are other, safer, options.
Last edited by Lerner; Oct 30th, 2008 at 6:45 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 30th, 2008
0

Re: else if's and global variables

You can also use a switch case statement instead of multiple if / else if
Globals are to be avoided at all cost. They can get you in great trouble. In fact OO was partly invented to avoid globals!
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 30th, 2008
0

Re: else if's and global variables

so your saying that if you have data as globals another separate program is allowed to manipulate that data easier? I thought it didnt matter where it was in RAM?
Reputation Points: 12
Solved Threads: 4
Junior Poster
cam875 is offline Offline
170 posts
since Jun 2008
Oct 30th, 2008
0

Re: else if's and global variables

Click to Expand / Collapse  Quote originally posted by cam875 ...
so your saying that if you have data as globals another separate program is allowed to manipulate that data easier? I thought it didnt matter where it was in RAM?
No, that's not what's being said. Programs are in separate memory chunks. Only if you take special actions to allocate some shared memory will different programs have access to common data.

Global variables are visible to all functions in your program. Because all functions can change the global variable, there's always the danger/temptation for all function TO CHANGE the global variable. In anything beyond a trivial program, you quickly lose control over who should change the variable, under what conditions it should change. You also lose the ability to easily use any function in another program if some global variable must also be replicated.

A function should act like a "black box". You know what goes in (the input parameters) and you can see what comes out (the return value or any parameters passed by reference). What the function does inside itself is of no concern to any other function in your program. Global variables add an unknown factor to this clean input/output picture.

To say that global variables should be avoided "at all costs" is a bit extreme - as are almost all absolute statements. Sometimes there may be some variable which a large number of functions need access to, and some may change it, and all need to be able to know the current state of that. Perhaps some configuration state of the program. So sometimes, a global makes sense to use. When that is the case, DOCUMENT THE HECK out of it - state clearly what functions should be able to change it, under what circumstances.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 31st, 2008
0

Re: else if's and global variables

Nice explanation vmanes.
Quote ...
Perhaps some configuration state of the program.
Would not call that a global variabele in the sense that is meant here. This would typically be stored in a resource or config file.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 31st, 2008
0

Re: else if's and global variables

Click to Expand / Collapse  Quote originally posted by ddanbe ...
You can also use a switch case statement instead of multiple if / else if
Only if the behaviour sought is comparing an integral variable against a fixed set of values. It is not possible to use a switch statement with non-integral types, or have a case that is not a compile-time constant.
Click to Expand / Collapse  Quote originally posted by ddanbe ...
Globals are to be avoided at all cost. They can get you in great trouble.
Like all dogmatic statements, this is correct sometimes and incorrect sometimes. Global variables are useful (without causing trouble) in some circumstances and not in others.
Click to Expand / Collapse  Quote originally posted by ddanbe ...
In fact OO was partly invented to avoid globals!
That's not true. It happens that a lot of well-designed OO designs or code will not use globals, but a lot of well-designed programs (OO or not) do not use globals. Eliminating globals, however, was not even a minor goal in creating the OO design or coding techniques.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 31st, 2008
0

Re: else if's and global variables

What's dogmatic about the fact that globals are to be avoided at all cost? As I stated you can get in great trouble with them, indicating that this don't has to be so. Please use gobals at will if you like, there is no law against them! I like C# very much(among other laguages) simply for the fact that C# has no header files with possible globals, still I can do the same things with it as with C or C++.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 31st, 2008
1

Re: else if's and global variables

Click to Expand / Collapse  Quote originally posted by ddanbe ...
What's dogmatic about the fact that globals are to be avoided at all cost?
Dogma (n) : A firmly held belief based on faith, and often stated as fact.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 31st, 2008
0

Re: else if's and global variables

OK grumpier you win, but that doesn't mean I lost my faith in the fact that globals...
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Word Location in Text File
Next Thread in C++ Forum Timeline: Append and Read file at same time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC