The Clean Code Talks - "Global State and Singletons"
Top Comments
All Comments (34)
-
@Synergy9k Whatever method needs this 'current time of day' should have the time of day passed into it; so that you can run your test with a known time of day. Simply having deep areas of your code pulling various outside values without explicit control is asking for testing trouble.
-
What are you going to test it against?
-
But what if I have a method that, say, gets the current time of day? Even given the exact same input, it will, of course, return different values depending on external conditions. What's wrong with that? It seems perfectly intuitive and testable to me!
-
There is a ton of great design information in this talk.
-
I think you guys are saying the same thing. :p
If x == y && f(x) != f(y), f is dependent on global state. If f modifies state, it has a side effect.
-
@magwo Well from a theoretical point of view there are basically two concepts: functional and logic orientated. So I guess all existing languages have to be described with one of these two (lambda calculus is used as the theoretical background for most programming languages), which explains why my professor said in a theoretical lecture that global state (or even mutable objects) are basically side effects. :)
-
@blenderpanzi This is true in a purely functional language. In object-oriented languages with mutable objects, side effects do not really imply global state. Objects are created in local scopes, and if two objects are created equally, you should get the same result from obj1.f(x) as obj2.f(x), as well as obj1.f(y) and obj2.f(y) if x==y. Unless you have global state, like static variables, random generators (that are commonly based on the global variable known as the clock) and so on.
-
@magwo AFAIK: if x == y && f(x) != f(y) then there is a side effect. Having global state (or just object oriented programming with mutable objects) you get such a situation, because there is a hidden parameter. But maybe I did understand something wrong.
i learned - next time I test a credit card system - I will use charge(1) :)
deathbyaclown 2 years ago 15
God I learned this the hard way at my internship. It DOES drive you crazy...
spleenblender 2 years ago 9