Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not sure how that is any different besides construction of an object rather than a file with globals?


A singleton object can encapsulate the global state, converting global variables to private fields. How would this be different? Because a counter singleton can for example disallow directly setting the count field, only allowing the count to be incremented through a method.


You don't need objects for that kind of encapsulation, just don't export your variables across module / unit boundaries.


Yes good point. The module/unit acts as the singleton instance, in a sense, though that might be the incorrect way to put it.

In any case, I think variables that are “global” but encapsulated in this way lose the potential for harm we associate with a global variable the whole program may be directly reading and writing.


I think the suggestion is to encapsulate global mutable state behind a strict interface (if you want global mutable state)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: