Principles of Object Oriented Programming 2
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
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.
Latest blog posts
LGBT+ as an important part of Diversity, Equity and Inc...
A psychologically safe environment for all of our employees. Irrespective of their gender, religion, ethnicity, race, or sexual orientation. That is one of the core values of our organization. Read what our CEO Sasa had to ...Read more
Konteh 2023: Impressions
After two years of online gatherings, the student job fair Konteh was finally in person on the 22nd and 23rd of March. We got a chance to meet with a lot of young bright minds, excited to learn and expand their knowledge.Read more