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

后端 未结 13 1589
既然无缘
既然无缘 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 06:10

    Keeping most variables local and reducing their scope helps promote better programming practices. It also reduces the risk of mistakenly changing a variable and having to trace through functions looking for unintended or non-obvious consequences. I believe it also makes the code easier to read and maintain since most of the variables relevant to a particular section of code are defined and initialized nearby. Otherwise I seem to be constantly referring back to the section of code defining all the golbals. I do use some globals, but mainly for passing configuration information down to sub modules.

    0 讨论(0)
提交回复
热议问题