| | |
else if's and global variables
![]() |
•
•
Join Date: Jun 2008
Posts: 170
Reputation:
Solved Threads: 3
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?
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?
•
•
Join Date: Jul 2005
Posts: 1,896
Reputation:
Solved Threads: 301
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.
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
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!
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
•
•
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?
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.
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.
Nice explanation vmanes.
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.
•
•
•
•
Perhaps some configuration state of the program.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
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.
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Share global variables between classes (C++)
- Username/Password help, OrderBy help, and Global variables (Visual Basic 4 / 5 / 6)
- C- Syntax to allocate Global variables to consecutive memory locations (C)
- Global Variables in Functions (Python)
- Global Variables not so 'global' (Visual Basic 4 / 5 / 6)
- Global Variables (C++)
- Global v/s Public (Visual Basic 4 / 5 / 6)
- Global Variables help (C)
Other Threads in the C++ Forum
- Previous Thread: Word Location in Text File
- Next Thread: Append and Read file at same time
Views: 1028 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int integer lazy link linker list loop loops map math matrix member memory network newbie number object objects opengl operator output parameter pointer pointers problem program programming project qt random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets






