Why are global variables bad, in a single threaded, non-os, embedded application

后端 未结 13 1635
既然无缘
既然无缘 2020-12-01 05:20

Most of the objections I see to using global variables make sense since they refer to issues of multiple threads, thread safety, etc.

But in a small, single threaded

13条回答
  •  [愿得一人]
    2020-12-01 05:49

    The short answer is that global variables have historically been the source of subtle bugs. But then (putting on my functional programmer hat) all variables have been sources of subtle bugs.

    Basically, a global variable means that you can't look at a piece of code using the variable's value, and know what it's going to do without knowing what every other piece of code setting that variable's value has done in the past.

提交回复
热议问题