Principles of Object Oriented Programming 2

Nemanja Malocic Date 03-Jun-2017
workshop-with-zoran-horvat_news-photo.jpg

Table of contents

    Last weekend we held one more amazing workshop with Zoran Horvat about the Principles of object oriented programming. The main focus of this workshop was eliminating null references from our code, and shifting our thought process towards using objects instead of null reference. There are two main reasons for this. Firstly null is not an object and it forces us to check for nulls, and secondly, statistics have shown that around 50% of all exceptions in production code are null reference exceptions.

    The first part of the workshop was aimed at eliminating null reference and branching around it using polymorphism. The idea was to create an empty object or null object instead of returning null, where that object doesn’t have any functionality so we don’t have to branch around null to avoid executing code. Another problem that we have is that, if we just return null, higher level loses part of the information that can be shown to the user if that is necessary. So, instead of just returning null or empty object, we can create special case objects, that will carry the information about what happened and why. This way we can skip branching, so we have only one line of code execution, increase the readability of our code, and decrease the probability of errors.

    This is all good, but this may possibly lead to the explosion of classes and in some cases we can’t avoid null reference with conventional techniques of programming. So, the second part of the workshop was aimed at the implementation of Option type and applying it so we reduce the cyclomatic complexity. In functional languages, this is part of the framework, but inside C# we need to create our own implementation.

    The idea behind option is that we have Option and that we can have value or we don’t have value, but we shouldn’t care, and we execute operations against Option and don’t need to branch.

    The workshop was definitely a mind-blowing one, since the final solution resulted in oneliners with chained calls.

    This was our second workshop with Zoran Horvat and we will definitely organize a few more events similar to this until the end of this year.




    nemanja-maloc-ic-_authorsphoto.png
    Nemanja Malocic Software Developer

    Born to a family of lawyers, Nemanja knew from a young age that it was his destiny to leave the life of litigation for a life of the imagination.