Code Smells and Anti-Patterns

NoseWhat is an Anti-Pattern?

If you know what a pattern is, then you probably already know about anti-patterns, even if you didn’t know it! In a nutshell, a design pattern is an observed good method to solve a re-occurring problem. You have probably observed patterns in your own development.

For instance, maybe you have an object that needs to be accessed by multiple threads, but the integrity of the application requires that there be no more than one instance. In the countless times that this problem has been solved in the past, most “good” solutions have taken a similar form… what we now call the Singleton pattern.

Anti-patterns are similar, but antithetical. Over time, developers have been solving problems poorly. Many of these “bad” solutions have taken similar forms, which we can identify as various anti-patterns.

Why Do I Care?

Patterns and anti-patterns are both important to study. Some of the concrete benefits of learning about patterns is to * help you make better design decisions * solve problems faster and better (since someone has likely already identified a good solution) and * identify potential bugs in existing code. Some of the softer benefits include * increased awareness of good and bad practices * more analytic view of code and * better refactoring skills.

Basically, a better understanding and application of design patterns and anti-patterns is one of the tools that can help you become a better developer.

How Do I Find or Identify Anti-Patterns?

This is where code smells come in. Sometimes when I open the fridge, there is a bad odor. Maybe it is strong, maybe not, but it is an indication that there is something rotten inside. Now I need to investigate to find out what it is. Likewise, a code smell is a sign that there may be a bigger problem that has not been identified yet.

Some important things to remember about code smells:

  • Usually pretty subtle, and almost never cause compile errors.
  • Some can be identified with static code analysis.
  • Not usually the problem, but simply an indicator that there may be a bug or bigger problem.
  • May not indicate a programming error… instead may indicate a problem with process, architecture, requirements, etc.

Code smells are not the only way to identify anti-patterns, but in my experience, they have  been very reliable.

So What Now?

Hopefully you have been inspired, or at least had your interest piqued for learning more about patterns, anti-patterns and code smells. I’ll be posting more information about them on my blog from time to time to help you out and hopefully keep you interested.

Leave a Reply