Avoid null: Use an Optional type instead

Did you know that “null” is a concept that had to be invented?   C.A.R. Hoare created it in 1965 as part of the Algol W language.  I think adding “null” to programming languages was a giant step in the wrong direction.  But you don’t have to take my word for it.  C.A.R. Hoare agrees with me and calls it his Billion Dollar Mistake. Since “null” had to be invented and Algol W wasn’t the first programming language, that means that there must be programming languages without the concept of “null”.  

But, how do they avoid this?  For example, what if you get some data from a database and the row is not there?  If a language doesn’t have “null”, how could it represent this state?  The answer is with the Optional type. In this article I’m going to explain in detail why you should prefer the Optional type (AKA the Maybe type) over “null”. But before I explain this, I want to explain what I dislike about “null”.

Continue reading

The hashcode, explained

On StackOverflow, I was asked what the Java hashCode method does, but I did not have enough room to answer.  I decided to write a blog article about the topic.  The  hashCode method is a part of the Object  class.  This means that every single class in Java has a hashCode  method.  To explain what it does, I’m going to use an analogy:

Continue reading

Banning break and continue from your loops will improve code quality

For as long as I can remember programming, I have always avoided the  break  and continue  keywords in my loops.  Before I explain why I avoid them, I want to explain what they are.  Here’s an example:

A continue  will jump to the top of the loop and reevaluate the condition.  A break  will exit the loop.

What I don’t like about these keywords is that when you want to refactor this code, the continue s and break s step on your toes.  Here’s an animated GIF demonstrating why that is:

Continue reading