2

With 'global variables', I mean

  • Variables on namespace level
  • Static data members in classes
  • Static variables in functions

In a big C++ project I would like to have a mechanism (like a compiler option) to prevent such data in most parts. A compiler error or warning would be nice. Of course, at one point you have to instantiate your classes.

Conceptually we have the notion of "runnables" which is the code encapsulation. While provided by the platform project they can be 'instantiated' and connected by the downstream project. Unfortunately, since usually there was only one instance of a type, devs used a lot of globals or statics. Needless to say this isn't good practise and you run into problems when doing two instances of a class later.

It's ok to have only one build preventing this (we have GCC, Clang, VS and GHS). I guess a linker option isn't applicable as the executable is linked in the downstream project and they instantiate the 'runnables' on namespace level. Another idea would be to search the object (.o) files if they contain something for the data segment, but I'm uncertain how to do that.

Borph
  • 131

3 Answers3

16

In a big C++ project I would like to have a mechanism [...] to prevent such data in most parts.

There is such a mechanism. It is called code reviews - especially when done by the experienced guys in your team. Use that, and you will sucessfully avoid any unnecessary global variables in your project.

(And yes, I am aware that is probably not the answer you like to hear, but I am under the impression you are looking for a technical solution to a people problem - that almost never works).

When you just enforce a rule like "no globals" (or "no goto", or "every function needs a standard comment") by some tool, some developers will find a way to circumvent it - usually in some horrible manner which follows that rule literally, but does not increase any maintainability. If you teach all your devs why globals are not a good idea, which alternatives exist, and in which exceptional cases globals are acceptable, then you don't need a tool to enforce the rule. A code review is probably the best instrument we have to teach this knowledge and make sure such rules are applied in a way which makes sense.

See also: How would you know if you've written readable and easily maintainable code?

Doc Brown
  • 218,378
3

Some things can definitely be enforced by the compiler. Some other, unfortunately, cannot.

A good alternative to compiler warnings / errors is using a static analysis tool, configured appropriately according to your desires.

All "automated" solutions must be used as a tool during (the preparation of) a peer review.

As it was already suggested, the problem of global variables is more of a "mentality" problem, not related to syntax or anything. Keep reading about how you can deal with it (most important, using a static analysis tool and peer reviews, based on internal company regulations).


However, all the above will work only once there are some rules implemented at the workplace.

  • There must be some "development process" implemented, specifying things like:
    • how requirements will be written, to which level of detail;
    • how the source code must be written;
    • how everything will be verified and tested.
  • regarding the "how the source code must be written", there must be a coding guideline about it implemented in the company.

There are many coding guidelines available, some of them freely available, some for some fee. You will find many of them on the internet. Study them and see what fits best to your environment.

A good starting point is the MISRA coding guidelines. They were created to be used by the automotive industry, so they tend to be very strict. However, they will give a good insight about how to think.

You will also find interesting information (not necessarily regarding global variables) on the PC LINT site. I sometimes "relax" reading the "Bug of the month" section.

virolino
  • 604
3

In the new bright world of C++ Core Guidelines (endorsed by no less than Bjarne Himself and current WG21 convener Herb Sutter) - there is Clang-tidy, and its avoid-non-const-global-variables option: https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables.html

In general, preventing such things automatically is MUCH cheaper than looking for these things in code reviews, as suggested by other answers.