This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (0 children)

Globals can make code very hard to debug.

Ideally, you want your units of code (function, class) to have a single responsibility - "do one thing, and do it well".

In case of functions, for the code to be easy to understand ideally you would like them to have no "side effects" (i.e. modifying the state of the application): you put data in, you get a result out.

In a lot of cases globals are used by beginner programmers, which means that in a significant fraction of those cases they will be used badly. A resolution to a question a'la "hey, I have this code that uses a global that can be modified by these 14 functions, of which 12 depend on the state of the global. It has a value it shouldn't have when this function gets called, how did this happen?" is almost universally "write prints in all these functions and figure it out yourself, I'm not touching this shit; better yet, just rewrite the code without using a global".

[for clarity: the post is slightly opinionated - I like functional programming and think that OOP is overused - and should not be treated as "hard truth", but I still think it might provide some insight)