Peter Morlion logo


Coding pet peeves

This was a blog post I had lying around, waiting to be posted. This post on code smells on the All Your Base Are Belong To Us blog reminded me of it. So here goes.

Everyone has coding pet peeves. Pieces of code they can’t resist the urge to change when they see it. Of course, there will be exceptions, or cases where it’s impossible to change the code, or just not worth it. But not that often. Apart from wrapping all your methods in try-catch blocks, here are two other of my coding pet peeves.

Helper classes

Helper classes are classes whose names end in ‘Helper’. In my line of work (hospitals): PatientHelper, AnamnesisHelper, etc. Sometimes they might end in ‘Utilities’. They all have several things in common, starting the the too general name.

The Single-Responsibility-Principle requires that a class has one responsibility. This, to me, implies that it shoud have a clear and unambiguous name. Helper or Utilities is not clear.

The biggest danger with classnames like this, is that these classes can become too big, with all sorts of methods dumped in them.

There are several ways you can get rid of these classes. Remove them method by method. I’ve been able to move methods to repositories, domain objects, controllers, extension methods, etc. All classes that are loud and clear in what they do (extension methods are a bit special).

One file per class

Why would you put multiple classes in one file? Usually, it’s because it’s a small class, an enum probably. The advantage is laziness, I believe. The advantage of one class-one file, is that you can easily see all classes in your IDE of choice. Which is a small advantage, maybe. But it is a pet peeve, so it doesn’t always have to be fully rational, no?

Also, files are cheap, so no need to ration them.

A bigger advantage becomes apparent when you’re using source control. When opening the log file, you can more easily search for changes to a class, because a class and a file are the same. You don’t have to inspect the diff to know if your class changed, or it was just something added to the enum.

Pet Peeves

I guess everyone has their personal style of coding, and their personal coding pet peeves. What are yours? What small things make you cringe when looking at code?

Leave a Reply

Your email address will not be published. Required fields are marked *