0

I encountered a sourcefile with 6k lines, which completely works on global variables and has a lot of functions that operate void func(void) but alter the global variables. Some of the globals are structs with structs of structs and pointers to functions, making it quite hard to get a good grasp even of the data.

The code is hard to read, hard to maintain and hard to test, because the functions have no real interface description but work "everywhere".

Now I am wondering are there reasons to code this way (completely working on globals instead of using pointers as arguments)? Is it possibly faster or smaller or are there other benefits that could make this more useful than well-structured functions with pointers?

Kami Kaze
  • 2,069
  • 15
  • 27
  • IMO there is not much benefit abusing globals. The program may run slighly faster and it may be slightly smaller, but this is probably not relevant. – Jabberwocky Apr 14 '16 at 07:12
  • Extensive use of globals results in (as you say) code that is harder to test and maintain. Globals have their use and purpose, but i can not see any advantage in using them the extensive way you put forth – Henningsson Apr 14 '16 at 07:22
  • it think, the code has grown from a small programm with one global and then evolved into your complex legacy. No one took the time and effort to refactor it, BECAUSE it just worked (ncars). – Peter Miehle Apr 14 '16 at 07:34
  • I would suspect that some of it has grown. But as it used for a communication protocol, a big part of it have been there from the start. And new implemented functions could still have used pointers to the variables to make it more readable. – Kami Kaze Apr 14 '16 at 07:37

2 Answers2

4

First of all, there is never a reason to use global, non-constant variables in any application. Global, as in available to the whole program.

There might however be some cases where static file scope variables make sense. Note that the static keyword blocks the variable from being global, it is only available to the translation unit (meaning c file plus included headers) where it is declared.

Some examples where such designs make sense:

  • When designing autonomous code modules (call them classes, ADTs or whatever) and you can be sure that only one instance exists, it might be convenient to use static file scope variables as "poor man's private". As soon as you provide some sort of API to the caller, you will almost always have a need for private, internal variables.

    This practice common in embedded systems single-core programming. Less so in desktop programming, where re-entrant code is much more important.

  • Special cases. When sharing variables between the caller and an interrupt service routine or a callback function, parameter passing might not be an option. And then you have no other choice but to use static file scope variables.

    (Such variables should also be declared as volatile to prevent compiler optimizer bugs, in cases where the compiler doesn't realize that the ISR/callback is executed.)


The only reason why you would ever write a file larger than 2k LOC is because you are writing some sort of library that for some reason needs to be delivered as a single file. For example, if you are writing one of the more complex library files of the standard C library.

If that is not what you are doing, there is never any excuse to write a 6k LOC file, period. Let alone to fill it with complex data types.

Even when writing something like the first case above, there is nothing stopping you from splitting the file in several. Most often it is actually the "main.c" that escalates beyond 2k LOC, and that one should be easy to split.

So unfortunately, I suspect that the code you are maintaining is written by a beginner. It is not unusual, pretty much everyone who works as professional programmers write such monsters during their first 5 years or so. Because sound program design is something you mostly learn through experience.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

There is no real benefit to doing it this way. Any marginal performance gains which could be made by using global variables will almost certainly be made by the compiler anyway when it does its optimizations, and code written as you describe can take hours upon hours to modify due to its illegibility and non-conformance with standards.

nhouser9
  • 6,730
  • 3
  • 21
  • 42