Recently, a reader encountered the following issue:
Upon joining the company, I inherited a “mess” left by a former colleague, where there are many global variables…
They asked me: What are the drawbacks of having too many global variables? How can we avoid them, and how should we manage global variables?
What are the drawbacks of having too many global variables?
Those who have worked on projects should understand that having too many global variables can lead to numerous problems.
Here are some potential drawbacks of having too many global variables:
1. Poor code readability
With an overwhelming number of global variables, especially when each source file contains all variables, the readability of the code becomes significantly impaired.
If combined with inconsistent naming and arbitrary definitions, the readability of the code is even worse.
2. Difficult code maintenance
As the number of global variables increases, variable names from different modules may conflict or become confused, making the code difficult to understand and maintain. Additionally, global variables complicate the dependencies in the code, making them hard to track and understand. This increases the learning curve for new developers and makes modifications and debugging more challenging.
3. Poor portability
Global variables are often closely tied to specific hardware or system configurations, and if each file is calling global variables, this severely limits the code’s portability.
4. Memory management issues
Having too many global variables can lead to memory leaks and fragmentation, among other issues.
Memory leak:If the lifecycle of global variables is not appropriately managed, it may result in memory leaks, especially in resource-constrained microcontroller environments.
Memory fragmentation:Frequent allocation and deallocation of memory related to global variables can lead to memory fragmentation, reducing memory utilization efficiency.
5. Potential bugs
As the number of global variables increases, the likelihood of bugs also rises. Multiple functions or modules may access and modify global variables simultaneously; without proper synchronization mechanisms, this can lead to data inconsistency and unpredictable behavior.
A function’s modification of a global variable may affect other unrelated functions, making it difficult to locate and fix errors due to these implicit side effects.
6. Adverse to modular design
If global variables are interspersed throughout various modules, it not only undermines the independence of these modules but also increases the coupling between them, reducing the reusability and maintainability of the code.
Generally, well-designed modular code will either have no global variables or very few.
7. Increased debugging difficulty
During unit testing or global project testing, managing the state of global variables becomes complex. Testers need to ensure that global variables are in the correct state before each test. If modifications to global variables can occur in multiple locations in the code, it becomes challenging to pinpoint the root of the problem during debugging.
8. More drawbacks
The above are common drawbacks; there may be others that can be discussed in the comments.
How to avoid having too many global variables?
Given the numerous drawbacks of having too many global variables, how can we avoid them?
1. Use static local variables
In some cases, static local variables can be used to replace global variables, thereby avoiding modifications from other locations.
2. Use pointers and references
Within functions, you can access and modify the values of external variables through pointers or references without declaring them as global variables.
3. Use function parameters
Within functions, it is advisable to use local variables to store temporary data instead of relying on global variables.
Pass the required data through function parameters and obtain results through return values rather than directly accessing or modifying global variables.
4. Encapsulation and modularization
Encapsulate related variables and functions within structures or classes and access and modify them through interfaces.
Divide the code into multiple modules, each responsible for specific functions, and interact with other modules through interfaces.
5. Regularly optimize code
A good project requires regular maintenance and optimization. For instance, optimize data structures and algorithms, reduce unnecessary global variables, and even periodically refactor parts of the module code.
6. Increase review teams
Generally, large companies have dedicated departments for code reviews, conducting regular assessments of code and emphasizing the dangers of using global variables while encouraging team members to seek alternatives.
Through team collaboration and discussion, finding best practices together can also reduce the use of global variables.
Finally, if you have any original articles related to electronic design or other relevant topics, please feel free to submit them to us. We will select the best ones for publication, and you will receive a reward for your article!

Due to recent changes in the WeChat public platform’s push rules, many readers have reported not seeing updated articles in a timely manner. According to the latest rules, it is recommended to frequently click “Recommend Reading, Share, Collect, etc.,” to become a regular reader.
Recommended Reading:
-
The new Kirin chip is here, Huawei is selling out!
-
Domestic chip companies are going bankrupt! Fully owned intellectual property!
-
Another major factory has shut down! Thousands of layoffs…
Please click 【View】 to give the editor a thumbs up
