Variables should not be exposed in the header files. Proper encapsulation of a module requires data hiding. All internal data should only be in private variables inside the .c/ .cpp source code files.

Impact

Data is visible to clients and they can alter its contents. This may lead to incorrect functionality.

Characteristics

Global variables defined in header files which are exposed to clients.

Example(s)

  • Here DEFAULT_QUUX is exposed to b.c file also which is not intended.
// header.h
extern int DEFAULT_QUUX; // Data exposure

/* some other function declarations */

// a.c
#include "header.h"
int DEFAULT_QUUX = 5;
int main() {
  Blip b;
  b.do_stuff(); // makes DEFAULT_QUUX = 6
  Qlip q;
  q.do_things(); // reads DEFAULT_QUUX
}

// b.c
#include "header.h"
int fun() {
  return 0;
}