Code Smells : Change Preventers

If you ever have been in a situation when you need to make change in one place, but had to make changes in many places too, then Change Preventers is a code smell you should be vary off. Change Preventers is result of poor structuring of code and can be broadly categorized into 3.

Divergent Change

Symptoms of Divergent Change are obvious when you have to make changes in several unrelated methods when you need to make a single change in a class. The solution for the code smell lies in splitting up the class. We could segregate the different behaviors of the class using Extract Class or in scenarios where, different classes have same behavior, we could employ Extract SubClass or Extract SuperClass whichever seem appropriate for the particular scenario.

Shotgun Surgery

Another closely related Change Preventer is known as Shotgun Surgery, which is almost opposite to Divergent Change. With Divergent Change, many changes were required to a Single Class, while with Shotgun Surgery, a single change is made to several classes. This is a resultant of a single responsibility split among many classes and can happen due to ‘over-refactoring’.

The resolution lies in bringing together all associated methods to form a single class that has the responsibility rather than distributing it over multiple classes. Refactoring techniques like Move Method and Move Field can be employed for the purpose.

Parrallel Inheritance Hierarchy

If you find your self in situations where you intend to create a derived class, but also end up having to create sub classes for another class, then Parallel Inheritance Hierarchy. This eventually would result in making changes harder as related changes would be spread across the codebase.

To remove such duplication, we would need to employ refactoring techniques like Move Method and Move Field after creating a reference of the dependent class in your original class. Once all the methods and fields has been moved, you can get rid of the reference and duplicate class.

One thought on “Code Smells : Change Preventers

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s