Why Non Const Global Variables Are Evil In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Why Non Const Global Variables Are Evil In C++

Why Non Const Global Variables Are Evil In C++

BLUF: Mastering Why Non Const Global Variables Are Evil In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Why Non Const Global Variables Are Evil In C++

C++ is renowned for its efficiency. Learn how Why Non Const Global Variables Are Evil In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, you will discover the reasons why global variables are considered harmful in C++ programming:

At the global level, variables are initialized and declared, existing throughout the program's lifespan. These variables maintain their values and remain accessible during the program's runtime.

Global variables that are not const are evil because any function can modify their value. The program becomes less flexible and modular when global variables are used. It is advised against using program global variables. Use local variables in the program instead of global variables.

  • Unintentional Side consequences: As global variables are accessible and editable from anywhere in the program, it might be challenging to keep track of changes that result in unexpected side consequences. The code may become more difficult to comprehend and update as a result.
  • Namespace Pollution: Excessive use of global variables can result in namespace pollution , which is the accumulation of variable names in the global namespace, perhaps causing naming conflicts. Global variables are a part of the global namespace.
  • Dependency on Initialization Order: Global variables may be affected by the initialization order. If it is not well controlled, it may result in unexpected behavior. It can grow to be a substantial source of faults, particularly in intricate programs with several translation units.
  • Difficult to Test and Debug: Programs that use global variables may be more difficult to test and debug because any component of the code might change the state of the variables, making it challenging to identify and replicate individual problems.
  • Encapsulation and Abstraction: Fundamental concepts of object-oriented programming include encapsulation and abstraction, or the concealing of data. Encapsulation can be broken by using global variables since any section of the program can access and change the global state, making data security and integrity management difficult.

Utilizing local variables or passing variables explicitly as function parameters is commonly recommended to minimize reliance on global variables and address associated issues. Enhancing data management and diminishing the need for global variables are additional advantages of encapsulating data within classes and utilizing appropriate access modifiers like private and protected. By employing const global variables, undesired modifications can be prevented, and immutability can be enforced where necessary.

Example:

Example

#include <iostream>
// Non-const global variable
int globalVariable = 5;
// Function that modifies the global variable
void modify_Global_Variable()
{
 globalVariable = 10;
}
// Another function that uses the global variable
void print_Global_Variable() 
{
 std::cout << "Global variable value: " << globalVariable << std::endl;
}
int main()
{
 std::cout << "Initial value of the global variable: " << globalVariable << std::endl;
 modify_Global_Variable();
 print_Global_Variable();
 int globalVariable = 20; 
 // A new variable in the same namespace
 std::cout << "New local variable value: " << globalVariable << std::endl;
 std::cout << "Global variable value: " << ::globalVariable << std::endl; 
 // Accessing the global variable explicitly using the scope resolution operator
 return 0;
}

Output:

Output

Initial value of the global variable: 5
Global variable value: 10
New local variable value: 20
Global variable value: 10

Explanation:

  • #include <iostream>: You may use the std::cout to print output to the console by including the input/output stream library in this line.
  • int globalVariable = 5;: This line initializes the global integer variable globalVariable to the value 5. Any function in the program can access and modify global variables, which are defined outside of all other functions.
  • void modifyGlobalVariable: This line defines the modifyGlobalVariable method, which has no parameters and returns void. The global variable globalVariable is updated inside this method to contain the number 10.
  • void printGlobalVariable: This line defines the function printGlobalVariable. It has no parameters and yields void as its return value. The global variable globalVariable's value is simply printed to the console by this function.
  • Within the function main: std::cout << "Initial value of the global variable: " << globalVariable << std::endl; prints the initial value of the global variable to the console, which is 5.alterGlobalVariable; calls the alterGlobalVariable method, setting the global variable's value to 10. print Global Variable; prints the modified value of the global variable, which is now done by calling the print Global Variable method. Within the main function , a new local variable called globalVariable is declared with the value 20. This local variable doesn't have any effect on the global variable of the same name and is only accessible within the main method. std::endl; \< globalVariable \\ std::cout \\ "New local variable value: " prints the value of the local-global variable, which is 20.
  • std::cout << "Initial value of the global variable: " << globalVariable << std::endl; prints the initial value of the global variable to the console, which is 5.alterGlobalVariable; calls the alterGlobalVariable method, setting the global variable's value to 10.
  • print Global Variable; prints the modified value of the global variable, which is now done by calling the print Global Variable method.
  • Within the main function , a new local variable called globalVariable is declared with the value 20. This local variable doesn't have any effect on the global variable of the same name and is only accessible within the main method.
  • std::endl; \< globalVariable \\ std::cout \\ "New local variable value: " prints the value of the local-global variable, which is 20.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience