Are local variables generally preferred over global?

Click For Summary
SUMMARY

Local variables are generally preferred over global variables in programming, particularly in C and embedded systems, due to their advantages in code maintainability, readability, and encapsulation. The discussion highlights that local variables simplify code modification and enhance modular programming, which is essential for large projects. While global variables may be necessary in specific contexts, such as for synchronization or interrupt-driven routines, their excessive use is associated with lower program quality and increased complexity. Overall, the consensus is that local variables should be prioritized to improve code quality and maintainability.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with concepts of variable scope
  • Knowledge of modular programming principles
  • Basic understanding of embedded systems and memory management
NEXT STEPS
  • Research best practices for variable scope in C programming
  • Explore encapsulation techniques in object-oriented programming
  • Learn about memory management in embedded systems
  • Investigate synchronization mechanisms like mutexes and semaphores
USEFUL FOR

Software developers, particularly those working with C and embedded systems, as well as programmers focused on improving code quality and maintainability.

  • #31
From all of the replies it appears that the answer is completely contextual - that we can all hopefully agree on. Bottom line in the most general sense is to program to the language's standard and more importantly to your company/industry's standard.
 
Technology news on Phys.org
  • #32
Coming from a hobbyist programmer.. I learned to code with NO examples except for the "help" section of Qbasic.. it had NO guidelines on how to code properly.. .Somehow, very surprisingly, I wrote some pretty impressive programs which didn't even use subs and functions!.. Just a WHOLE WHACK of global variables, and lots and lots of "GOTO" statements.
Then I started playing with VB6 and VB.net, and had to try to understand other people's code, nevermind code I had written a couple months earlier.

It took years, and I'm not there yet, but I have figured out how to MOSTLY eliminate global variables, use functions, classes, etc and find alternatives to "goto".

For me, there's something that I still will use global variables for.. things like an "abort" or "Cancel" flag variable that gets checked in a nested part of the program but set in the UI thread

It takes a bit of getting used to, but it is best to minimize global variables whenever possible.. There may be exceptions to the rule, but it's best to learn how to follow the rule before learning when it's correct to break it.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
9K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
7K
Replies
4
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K