How Bad Are Singletons?

后端 未结 7 2065
傲寒
傲寒 2021-01-02 22:41

So ....

There are obviously many questions that all have been asked about Singletons, Global State Variables, and all that great stuff. My question is,

7条回答
  •  旧时难觅i
    2021-01-02 23:32

    People discourage the use of global variables because it increases the likeliness of bugs. It's so much easier to make a mistake somewhere if every function in your program accesses the same global variables, and it's much harder to debug. It's also much harder to test for.

    I imagine they're still used so often because programmers are lazy. We don't want to spend time up-front making the code all organized and pretty, we just want to get the job done. It's so much easier to just write a global function/variable/whatever than to modularize it, and once you've started down that path it's too much of a pain to go back and refactor. Maybe that's the reason: They started out that way and simply never turned back.

提交回复
热议问题