tags:

views:

155

answers:

6

Hi, as a beginner I read everywhere to avoid excess use of global variables. Well how to do so? My low skill fails. I am ending up passing tons of structures and it is harder to read than using globals. Argh my code is mess any good book/article recommendation that guides trough this problem/application structure design?

+1  A: 

I'd suggest Structured Design by Yourdon and Constantine. An old book by computer standards (it has examples involving tapes!) but very sound on the problems you are having.

Brian Hooper
A: 

Here are two options that you could use to improve your situation:

  1. For read-only structures, have functions that can control access to the data with a const pointer:

    struct my_struct;

    const my_struct* GetMyStruct(void) const;

  2. Limit the exposure of a global structure by declaring it static. This way it will only have file scope:

    static mystruct myStructInstance;

waffleman
+6  A: 

Depending on what your variables are doing, global scope might be the best scope. (Think flags to signal that an interrupt has arrived, and should be handled at a convenient time in the middle of a compute loop.)

Small utility programs can often feel much cleaner by using global variables (I'm thinking especially of small language parsers); but this makes it much harder to integrate the small utility programs into larger programs in the future. There are always trade-offs.

But chances are good the "correct" data organization will not feel quite so cumbersome. If you post code here, someone may be able to suggest cleaner layout, but the real problems come when code grows beyond easily-understood small samples.

I have a LOT of favorite programming style books, but I think the best I know of to address this situation is The Elements of Programming Style, by Kernighan and Plauger. It's quite old, and difficult to find, but short, sweet, and well worth finding used somewhere.

It's not as short, it's not as sweet, but still well worth finding Code Complete, 2nd edition. It's much more detailed, provides much more code, and provides much more diversity involved in designing software. It's excellent, but might be more intimidating.

There's nothing like studying the masters: the code in Advanced Programming in the Unix Environment, 2nd Edition is phenomenal, well worth every hour of study.

And, of course, there's always experience, but that takes time to acquire. Learning lessons from your own mistakes tends to stick much stronger than learning lessons from other people's mistakes. So keep at it. :)

sarnold
The Kernighan and Plauger book is great (our work has a copy, think I saw it going on ebay for over £100 recently!). A newer book (covers C/C++ and Java) is The Practice of Programming by Kernighan and Pike. Like the other Kernighan books it is short, sweet and easy to dip in and out of.http://en.wikipedia.org/wiki/The_Practice_of_Programming
fluffyben
A: 

If your program it the sort of "small" project where global variables don't feel so bad, but you think you might need to integrate it into a larger project in the future, a very simple solution is to add a single context pointer argument to each function and store all your "global" variables in there. If you always name it the same thing, you can even do stuff like:

#define current_filename   context->current_filename
#define option_flags       context->option_flags

etc. and your code will look virtually identical to how it would have looked with globals, except that you'll be able to have multiple instances of it in a single program, integrate it into a library, and so on with minimal fuss. Just keep those defines in a private header used by your source modules, not the public interface header.

R..
A: 

@PeterK Problem is that structure as itself is always presented in C books a as container that can be declared/passed many times to different funtions and that is a thing that may confused me and I never thought to use it as a simple one instance global container (and that may make my code more readable).

I am writing 3 phase motor control application to control 1 motor. Based on what all you wrote please check if my current ideas of solving problem is right:

  1. Pack some global information in structure according to function ex. (sInverterState, sButtonsState, sInverterParameters etc.)
  2. If I write menu UI I can use static variables in C file and don’t care about passing structs when I have only 1 LCD. I don’t want to make it look like GTK++.
  3. Writing reetrant code is not for me yet and its overdoing for this purpose.
  4. Get proper education in IT field.

I may end up with lots of globals but at least they are nicely packed and readable.

Thank you for all books recommendations.

Regards

dummyprogrammer
It sounds like you have good instincts. If you put all the data related to your motor into one structure, you can pass a pointer to `struct motor` to the functions that need it, and _easily_ move to multiple motors in the future by storing an array of `struct motor` structures, and easily pass a pointer to the correct element in the array in the future. There's no need to use an array of `struct motor` yet, but when the time comes, it'll be easy to make that transition. If it is all global data, it'll require re-writing all the functions to use parameters instead.
sarnold