|
Use of globals
I recently a seat booking C program for a college assignment. I've been told since by the tutor that my use of globals is bad, and I should use local variables, passing them as function parameters to any functions that need them. To me, that looks messy. While I understand the overuse of globals is a bad idea because of memory use and function portability, I think they're necessary sometimes to avoid unnecessarily large parameter lists. Personally I'd have preferred to program the thing in C++ and put the whole thing in a class anyway, but this was a C only project. I was just wondering what your opinions were. The two globals are
There are about 10 functions in my program, many of them using the two globals above. If you want the rest of the source, look here.
www.elf-stone.com
Edited by - benjamin bunny on May 17, 2001 6:44:56 PM
____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux
If you''re plagued with long function calls, you could add IO container classes. It helps prevent breaking interfaces also. So a call to (in VB-esque pseudo code):
becomes
Hope that helps.
Epolevne
|
becomes
|
Hope that helps.
Epolevne
The first variable should be avoided if possible or atleast be placed inside its own module. The second is more of constant and that is ok. I would however prefer a "const" in front of it.
I learnt from ''Code Complete'' that if your function lists are long you probably have a bad design. Try breaking up what your doing in a manner that stops you from having to pass the extra params.
Personally I have no globals in my 160 file application. Almost no static''s, and nothing extern''ed.
My personal limit is around 5, they say 7+ is bad. If you need more that that you should either break up the code or pass in a struct. For example rather than passing in all the attributes of a screen like height,width, colours, fullscreen, vsync, I make a class(struct in C) and pass that in by reference(pointer in C).
In C I found it harder to avoid globals than in C++ as C++ has member vairables which in my mind are just globals with a scope of the class (ie the are shared amongst many functions like a global might be).
HTH
Personally I have no globals in my 160 file application. Almost no static''s, and nothing extern''ed.
My personal limit is around 5, they say 7+ is bad. If you need more that that you should either break up the code or pass in a struct. For example rather than passing in all the attributes of a screen like height,width, colours, fullscreen, vsync, I make a class(struct in C) and pass that in by reference(pointer in C).
In C I found it harder to avoid globals than in C++ as C++ has member vairables which in my mind are just globals with a scope of the class (ie the are shared amongst many functions like a global might be).
HTH
Chris Brodie
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement