Article: Global Variables Are Bad

General discussion for topics related to the FreeBASIC project or its community.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Article: Global Variables Are Bad

Post by vdecampo »

For your contemplation...(or flaming!) ;)

Global Variables Are Bad

-Vince
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Post by Lachie Dazdarian »

One of the major arguments against global variables is that they defy the basic OO concept of cohesive methods.
Yeah, some argument.

It really all depends on the style of programming. I just don't understand why some programmers are so persistent in bashing certain "styles" of programming just because they don’t have a habit of coding like that, and not because of real experiences with this specific style of program causing problems for them.

In my own personal experience, I had the most problems with other FB programs featuring excessive usage of pointers and OOPish like code. And not in the sense I wasn't able to modify them. I'm saying, they included instability and bugs that crash your program for no easily detectable reason (as it is the case with misused variable that happens to be global), often came with memory leaks and similar. I never had problems with programs that used global variables excessively.

So yeah, until global variables really manifest themselves as bad for certain someone, that certain someone has no reason to such OOP zealot preaching.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

Ah yes. I remember this lesson well. I came to agree. Now when I use them, I use them because they serve a specific function.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

I am currently using a lot of global variables in my 16,000 line game, which is only about 50% complete code-wise.

I have two types of globals:
* Globals that are pointers to a UDT, of which almost always there is almost always a single instance. (ie: similar to the "Singleton" construct)

* A few mouse and key globals that I haven't grouped yet...

99% of the bugs I have had did not have anything to do with my decision to use many globals. Most of those 99% were typos or some other simple error (like wrong bounds on a for loop) that caused memory leaks or segmentation faults. Seg faults take me a very long time to debug. :-(
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Global Variables are bad

Post by Theunis Jansen »

QB viewpoint.
When I first started BASIC I was declaring everything COMMON, SHARED: DEFINT A-Z.
Progressing I thought storing unnecessary variables under GLOBAL was possibly slowing down execution of my program and was it really necessary? I am self taught so I was trying to use logic, and my conclusions could be either correct or not.
I nevertheless started making the only absolutely necessary Variables, i.e crucial to the correct execution of my programs GLOBAL, and also started to use SUM%, Timed! etc for values/variables instead of making everything from A to Z an Integer. This actually improved the speed of execution, why I did not know but later found out that it eliminated a look-up table where it first had to check how A-Z was defined. I don't like to pass more than four Variables to a SUB/ FUNCTION and if more were required and usually if required they had already been declared SHARED for use in different SUB's so correct functioning of my SUB/FUNCTION was hopefully assured.
My opinion based on my experience and logic is thus:-
1. It boils down to the likes and dislikes of some lecturers/tutors or programmers. (Like me)
2. Unnecessary use is bad. Since it clutters up things.
3. Use it only where necessary to optimize execution speed. (?)
4. It is not outright bad because it is a necessity since having to pass every parameter is finicky and one needs a photographic memory to remember every darn variable in every darn module. Compared to remembering one or two variables.
5. In the end having to pass parameters for everything is IMHO extra unnecessary, tedious and time consuming programming and also harder to follow.
6. An #include, *.BI or whatever is applicable, should be used in the programs that are to be linked thus eliminating undeclared variables which if not used I deduce would be a contributing factor, reason, cause of bugs. (This happened to me). I prefer to have these declarations (*.BI) etc visible in my main module. When I am finished I copy and save them under the original *.BI name even if I didn't change anything. ( For just in case I forgot and did in fact change or add a definition etc.)

If nobody tears me apart or points out inaccuracies in my logic or agrees with me (already done in some respects) then I'm not gonna learn anything.

Hey Vince stick a (?) after your heading. I know you don't fully agree with Global Variables are Bad. If you don't I'm gonna send someone with a flame-thrower to warm your bottom. (I flamed him good didn't I ?)
Last edited by Theunis Jansen on Sep 17, 2010 8:50, edited 4 times in total.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Post by aurelVZAB »

I learn make basic programs on ZX Spectrum where all variables are
global and i never have problem with this...
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

Lachie Dazdarian wrote:
One of the major arguments against global variables is that they defy the basic OO concept of cohesive methods.
IMHO that is a far fetched argument. One can also change global state in a dangerous way in a OO environment.

It goes more in the direction of a functional "pure function" principle. A function only has input and a return value, and does not change state in any other way.

But that is very hard (even all practical functional languages have escapes to deal with state).

It is of course something to consider, but IMHO not a hard rule. As nearly all guidance rules.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Article: Global Variables Are Bad

Post by marcov »

vdecampo wrote:For your contemplation...(or flaming!) ;)

Global Variables Are Bad
Simple mind exercise:

I have a new program. I create my first new object. Where do I store it?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Article: Global Variables Are Bad

Post by vdecampo »

marcov wrote: Simple mind exercise:

I have a new program. I create my first new object. Where do I store it?
In your MAIN function?
Eponasoft
Posts: 264
Joined: Jul 26, 2007 2:40

Post by Eponasoft »

Pretty lame arguments, this document is... almost as lame as the ages-old GOTO debate (the most hideously pathetic argument ever devised) or that moron who actually thinks that including other sources into your main source somehow magically screws up compilation by "misaligning" the code (wtf, it's not even logical). I feel five points of IQ stupider just by reading it.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Article: Global Variables Are Bad

Post by marcov »

vdecampo wrote:
marcov wrote: Simple mind exercise:

I have a new program. I create my first new object. Where do I store it?
In your MAIN function?
So second, some other function from some library wants to register itself with that class. A specialized class registers with the abstract class factory so to speak.


P.s. did you really think I didn't expect that answer ? :-)
Last edited by marcov on Sep 12, 2010 19:06, edited 1 time in total.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Article: Global Variables Are Bad

Post by vdecampo »

marcov wrote:So second, some other function from some library wants to register itself with that class. A specialized class registers with the abstract class factory so to speak.


P.s. did you really think I didn't expect that answer ? :-)
It's funny how every argument made FOR using globals includes circumstances that are probably appropriate for the use of globals. The argument against globals does not preclude their use when necessary, it only points out that those circumstances are fewer than most programmers would like. It also points out the benefits of using encapsulated modules for improving code reuse, scalability, and debugging. I fail to see why there is so much emotion generated by these facts.

-Vince
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Article: Global Variables Are Bad

Post by marcov »

vdecampo wrote:
It's funny how every argument made FOR using globals includes circumstances that are probably appropriate for the use of globals. The argument against globals does not preclude their use when necessary,
I also don't preclude killing of globals when they are dangerous.

So what we are bickering about is the fairly grey area inbetween.

And then the question is if the ways to avoid globals in this grey are are worse or not.

For this, one only has theory, and hardly any research that actually correlates this to mistakes (contary to e.g. strong typing, word vs symbol oriented syntax) etc.
it only points out that those circumstances are fewer than most programmers would like. It also points out the benefits of using encapsulated modules for improving code reuse, scalability, and debugging. I fail to see why there is so much emotion generated by these facts.
No facts. Opinion. Those authors express an expectation, a fact is a measurement, under controled, repeatable circumstances.

IOW, an argumentation how something "could" work and what the cause "could" be, vs a proof by measurement *THAT* it works.

If not, show me actually original research that proves your point. I bet that even when you find it, the border conditions will not apply to FB.

And to be honest, this is what grates me again and again in these kinds of discussions. People quote sources without any checking up on them. They just shop till they find sb authoritative to confirm their already existing beliefs.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

marcov wrote:If not, show me actually original research that proves your point.
I point to the FreeBASIC graphics library. It uses globals for many variables which makes multi-threading graphics in FB impossible. That is not opinion, that is FACT! If it was written as completely encapsulated functions, there would be NO problems multi-threading graphic calls.

-Vince
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

global variables are bad (?)

Post by Theunis Jansen »

I must be winning this one no-one seems to have shot me down.(Unless I'm the moron referred to - I faff at times but I think that so far each post says GLOBALS are necessary.
Somewhere someone said for, FBC to work correctly Globals are still used.
(*)
Okay if it is item 6 of my post causing problems I must point out that I declared my convention i.e "QB" before saying what I said. To qualify: In QB you have a limitation of 64 Kb per program. It is however possible and recommended that if you write a larger program you create a second program/Module (or more - My one program consists of two modules and four CHAINed progs. If you do not declare all the variables from your different programs in all your Modules your program doesn't run and compiling is not possible. The modules are automatically linked by QB4.5 when compiling. All you have to do is click if it is a stand-alone, a BRUN program or a QLB (Quick Library), QB then does the rest. Placing the declarations COMMON SHARED etc from all files in one *.BI and including it in every *.BAS program/module, must be done for compilation and correct sharing of variables. This is required because one module may call a sub/Function in the other. QB4.5 saves the modules as separate *.BAS files and uses a MAK file to load them into its IDE. If u press F2 The Main module with its SUBs appears first and then every other module with it's subs/functions. So don't try to link two separately compiled QB4.5 OBJ files where calls to the other module is made by both modules without the necessary *.BI appearing in each prog. It's like trying to access a library without the necessary Include statement.

@Vince After looking at "Is my tutor mad" or something to that affect I think you are a sneaky one trying to get info to get a punch in with your tutor/lecturer/Prof. Here in SA my daughter is doing her Masters and she has a Supervisor. In our schools there aren't any teachers only Facilitators. So different places (computer languages) use the same thing under a different guise (name). (Or am I faffing again ?)

Some things don't need GLOBALs but others do. Nice example you gave but GLOBALly seen how about mine regarding FBC ?

(*) The edit regarding item 6 was added at 01:13 SAST
Last edited by Theunis Jansen on Sep 17, 2010 9:04, edited 3 times in total.
Post Reply