Duplicated Code

9 08 2011

Duplicated code is the smelliest of code smells and the most pervasive. It can be present in explicit or subtle form. Explicit duplication is present where identical code (copy and paste style) is found. Subtle duplication is found where structures or processing steps exist that appear different but are essentially the same.

The simplest duplicated code problem is when you have the same expression in two methods of the same class. Then all you have to do is Extract Method and invoke the code from both places. More complex examples exist such as duplicate processing logic simply because types have different interfaces, duplicate code in constructors, algorithms that are the same except for an object creation step. In these cases refactorings like Chain Constructors, Introduce Polymorphic Creation with Factory Method and Unify Interfaces with Adapter can help you stay DRY (don’t repeat yourself).

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: