else if's and global variables

Reply

Join Date: Jun 2008
Posts: 170
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

else if's and global variables

 
0
  #1
Oct 30th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,896
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 301
Lerner Lerner is offline Offline
Posting Virtuoso

Re: else if's and global variables

 
0
  #2
Oct 30th, 2008
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 5:45 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,281
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 356
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven

Re: else if's and global variables

 
0
  #3
Oct 30th, 2008
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 170
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: else if's and global variables

 
0
  #4
Oct 30th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,820
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 213
vmanes's Avatar
vmanes vmanes is online now Online
Posting Virtuoso

Re: else if's and global variables

 
0
  #5
Oct 30th, 2008
Originally Posted by cam875 View Post
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.
Don't own a gun but I'm going to join the NRA.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,281
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 356
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven

Re: else if's and global variables

 
0
  #6
Oct 31st, 2008
Nice explanation vmanes.
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: else if's and global variables

 
0
  #7
Oct 31st, 2008
Originally Posted by ddanbe View Post
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.
Originally Posted by ddanbe View Post
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.
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,281
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 356
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven

Re: else if's and global variables

 
0
  #8
Oct 31st, 2008
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++.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: else if's and global variables

 
1
  #9
Oct 31st, 2008
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,281
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 356
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven

Re: else if's and global variables

 
0
  #10
Oct 31st, 2008
OK grumpier you win, but that doesn't mean I lost my faith in the fact that globals...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1028 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC