Skip to main content

An instantiated object should be "ok"

I've been QA'ing quite a bit of work recently and one common theme I've noticed across both Java and C# projects I have been looking at is that we occasionally open ourselves up unessacarily to Exceptions by the way objects are being created.

My general rule of thumb (which I have seen mentioned in a Pluralsight video recently but also always re-iterate in various Robust Software talks I have done) is that you shouldn't be able to create an object and then call a method or access a property that then throws an exception. At worst, it should return null (I'm not going to moan about that now).

I've created an example below. We have two Dojos, one is good and one is bad. The bad dojo looks very familiar though. It's a little class written in the style that seems often encouraged. In fact, many classes start life as something like this. Then as years go on, you and other colleagues add more features to the class and it's instantiation becomes a second class citizen. One reason for this I guess is that changing how an old class is instantiated can often involve some significant refactoring.

Below is a series of classes that describes people that can attend a Dojo. Either a Sensei or a Student. They're not really pertinent to the problem.

The Bad Dojo looks like a nice little class initially doesn't it. It's small, succint and extremely simple. It's tempting to leave it like this as it has no methods on it.

The issue is that we want to use the list of students in the summary, when we use this class we start to run into problems.... can you spot what will go wrong?

Because BadDojo.Summary uses Students without checking to see if it's initialized, you run the chance of getting a null reference exception. In the example above Students definitely will be null.
There's lots of ways around this, one is to always check to see if reference properties are null in the first place. This can often be prudent. My initial preference is to always make sure the class is constructed in a safe way in the first place through forcing the consuming class to instantiate the object with the values it needs. You will often need to do both.

The Good Dojo below allows itself to be created either with or without students but always insists in a Sensei. If no students are supplied it initializes it's own internal list of students to ensure it's in a useable state. To me this is the best way to operate as you can then worry less about someone in the future forgetting to check if Students is null first. if(this.Students != null ....)
Now we can use the Summary property safely!

Pfft all obvious you fool!

Well yes this seems obvious now doens't it. Check your codebase, are you definitely adhering to this! In a language like F# we are less susceptible to these issues but we work with what we have.

Another side effect of being strict about how our object is created is that we have made it Immutable. I think I've already posted about this. But I will probably create another post on this soon based on these classes.


Popular posts from this blog

Creating star ratings in HTML and Javascript

I'd searched around a little for some shortcuts to help in doing this but I couldn't find anything satisfactory that included the ability to pull the rating off again for saving. I'd ended up coming up with this rather cheeky solution. Hopefully it helps you too! This is my first post in a while (I stopped blogging properly about 8 years ago!) It's strange coming back to it. Blogger feels very crusty and old by todays standards too.

Make your objects immutable by default

More about the Good Dojo In my post last week , I discussed creating objects that are instantiated safely. Please go back and read if you are interested. At the end of the post, I mentioned that I'd also written the class so it was immutable when instantiated. This is important!!! I feel like a broken record in repeating this but I am sure at the time of writing your code, you aren't modifying your object all over the place and so are safe in the belief that protecting against mutability is overkill. Please remember though, your code could be around for a hell of a long time. You aren't writing your code for now... you are writing for the next fool that comes along (including you) . Nothing is more upsetting that coming back to fix a bug on some wonderfully crafted code to say "Who has butchered my code?!", but often you were involved at the start of the process. You made the code easy to modify, allowing objects to be used / reused / modified without thi