Anti-Patterns and Worst Practices – You’re Doing it Wrong!

When shown ideal code, I think developers understand why it is favorable. When it is regarding Separation of Concerns (SoC) or Single Responsibility Principle (SRP) the consensus is something along the lines of “of course, that makes sense”. But not always do these same developers actually practice the techniques or principles. It’s always nice to see good code, but I think it’s also important to look at bad code from time-to-time.

Just as there are best practices, there are also worst practices. When you see some of these worst practices, you should do your best to acknowledge them, avoid them and try to remove them from your code, even if you have to do it little by little. The side effects of these are probably going to cause you a lot of frustration and pain in the future if they stick around for too long.

Here’s the four I’ve decided I’m going to single out over the next week:

This anti-pattern is quite simple. You’ll see an “arrowhead” form when your code has conditionals and loops nested within each other, over and over. The reason that this is bad is because it increases the cyclomatic complexity of the code. The higher the cyclomatic complexity, the more it is prone to bugs and defects, thus the harder it is to ensure the code works as expected.

A Heisenbug is named after Heisenberg’s Uncertainty Principle. Which states that the state of an object cannot be accurately measured, because any attempt to do so will change the result. In software, this can manifest itself when breaking the Command-Query Separation design guideline.

In the software world, you’re probably going to come across a class with this name at some point. What does it do? Nobody knows, that’s why it exists. When you see a Utils (or Utility) class, its existence is probably due to laziness or lack of domain knowledge.

About Chris Missal

Oh hey, I'm a Senior Consultant for Headspring in Austin, TX. I've been working in software professionally since 2006 and I really, really love it. I'm mostly in the Microsoft world, but enjoy building computer things of all sorts (to be vague). When I'm not slinging code, I'm probably out and about slinging discs, bowling balls, or good beer with great friends.

Can you explain a bit more on “Utils” class. I have seen it in many places and sometimes created in new projects to put common utility static functions.

Thanks
Kiran

http://www.elegantcode.com Chris Brandsma

There will be prayer in school so long as there are tests, and as long as there is a UI to be coded, there will be a utils class. :) Fix up databinding issues, common setups for UI elements, etc.

Actually, I’ve found that Extension Methods get me around much of the utils behavior I need. But there is still a problem with a single, monster util class (I favor several smaller util classes). So it really isn’t a utils class, but more of a utils folder (sometimes also called helpers).

But then again, if I’m still off my rocker, I’d love to find out why. :)

Frederico Pinto

@Kiran

Ask yourself “What is the responsability of this util class?”.
If it has a single responsability then odds are it’s badly named.
If it has more than one responsability then… well… you know…

There will be a post on the Utils class coming up that explains the problems I find with it. :)

Kiran

Thanks Chris, looking forward to it.

http://realfiction.net Frank Quednau

Just looked into StructureMap and Automapper. I find e.g. the ReflectionHelper. I would think that such things are utilities – procedural items whose code is not directly related to any of the project’s abstractions and yet has its merits in avoiding code repetition and boilerplate code while providing clean labels to those bits.

http://www.paulbatum.com Paul Batum

Can you give an example of a CQS related heisenbug? All the ones I’ve run into were related to concurrency.

How or where would you implement a method that converts an enum to it’s integer value and vice versa? Or an enums string name to it’s enum value?

Dan

I’m very interested in learning more about these anti-patterns. Thanks for doing this!

Cartel1

I too am totally at sea on the negative comments made about the Utils class. I also think discussions on coding best practices are a good thing for the most part, but really can get out of hand pretty quickly when it takes on a “Ten Commandments” tone – i.e. Thou shalt not ….

We all know that for all practical points and purposes, these commandments might not be followed because not all circumstances are black and white – i.e. there are the gray areas, which have to be dealt with, and for the most part represent what happens in the real world.

So I think discussions such as this should be encouraged, but centered around what tools can be implemented to assist today’s already overworked and underpaid programmers. Programmers normally like to take the path of least resistance in turning out code quickly, even though they are aware of some of commandment breaches that may result. So I would suggest, apart from blaming it all on lazy, commodity programmers, let’s talk about the tools that help these lazy programmers to produce better code so that they won’t have any excuses for their poor quality work.

sboulay

“In the software world, you’re probably going to come across a class with this name at some point. What does it do? Nobody knows, that’s why it exists. When you see a Utils (or Utility) class, its existence is probably due to laziness or lack of domain knowledge.”

Are you talking about a class literally named “Utils”. What about StringUtils, DateUtils, RequestUtils etc? I would hardly consider those ” laziness or lack of domain knowledge”.

I wish I had chosen the Utils class to be part 1. This seems to be the most interesting to people. You can have a utils-like class. Frank brought up the ReflectionHelper class in Structuremap. That’s a good example of “doing it right” I suppose; all the functionality is related to reflection. The post isn’t going to necessarily be all about this type of behavior being bad, but the abuse that comes from it.

Like Cartel1 points out, the Utils Class anti-pattern is usually a result of taking the path of least resistance and being lazy, that’s how it is abused.

All I can tell about your StringUtils is that it does something with strings. It’s not much better than a class that is called Utils, which I have no idea what it would do.

http://www.bennadel.com Ben Nadel

@Chris,

Maybe it would be better to use a concrete example when it comes to Utils. Let’s say we have a language that doesn’t have a “trim” method for strings (ex. Javascript). In that case, someone might wrap trim() up into a Utils object or global function (which is just part of the Window object). How would something like this be better organized?

Like I said, I wish I had designated Utils as Part 1, but there’s a post coming. :) It (hopefully) explains it better than my short single paragraph summary above.

http://www.rodrigoasensio.com Rodrigo Asensio

I had made an entire set of utilities classes starting from a toolkit.
Toolkit.network().ping(host);
Tookit.strings().padLeft(blabla);
Toolkit.validation().isValidEmail(email)
Toolkit.java().newInstance(packageName, className)

and that is the way my utilities works. they are well documented and ordered (they are like 22 classes providing common funcionality)

inampaki

This is Really Cool n Helpfull for me

http://www.lennybacon.com/ Daniel Fisher

1) Since extension methods are in place there mustn’t be Util-Classes!

2) When you talk about what is already there – take what is in the platform: System.Diagnostics

@Daniel
I agree with point 1, extension methods can greatly reduce the number of utility methods you need. I try to resort to these last though. They’re cool, but something about them still feels weird to me.