This page looks best with JavaScript enabled

Introducing Side Effects Into C# Properties - The Dangers of Being Clever

 ·   ·  ☕ 2 min read

    Side Effects are one of those things we don’t always consider when writing code. Sometime even basic maintenance can introduce them. A recent Reddit thread about an interesting interview question brought up one of the interesting examples of how side effects can make your code do very weird things.

    A side effect, in the simplest case is when a method or action causes something to happen other then the described method or action. If I turn on my water faucet using turnOnWater() and water comes out of the faucet that’s the intended effect, but if calling that function also causes neighbor.waterPressure to drop then we’ve introduced a side effect in our code. Side effects can take a lot of forms and this is just one example of them.

    This video explores a bit more on the theory side and is a bit different then some previous videos. I’d love to know what you think and if you’d like more videos on topics like this!

    The Stack Overflow question we reference can be found here which includes some solutions in Javascript:

    The reddit discussion of the question is here:

    The AtomicInteger we briefly bring up is actually a Java type (whoops) which you can learn more about in the JavaDocs here:

    A close alternative to the atomic types in Java is the Interlocked class which doesn’t expose the same kind of side effects that we were covering here (double whoops):


    Sam Wronski
    Sam Wronski
    Maker of things, currently helping build cloud things @ Google. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!