: Notebook: This article is filed under “blog”

Translated from: https://sourcemaking.com/refactoring/smells/change-preventers

The Change Preventers bad taste means that when you need to Change one piece of code, you find you have to Change something else. This makes program development complex and expensive.

  • Divergent variation
  • Parallel inheritance system
  • Shotgun modification
  • Further reading
  • The resources

Divergent variation

Divergent Change is similar to Shotgun Surgery, but in reality is completely different. Divergent changes are the changes that affect a single class. Shotgun Surgery is when multiple changes trigger corresponding changes to multiple classes.

Characteristics of the

You find that if you want to modify a function, you have to modify many unrelated functions at the same time. For example, when you want to add a new product type, you need to synchronize changes to the functions that find, display, and sort products.



Question why

Often, this divergent modification is due to poorly structured programming or “copy-and-paste programming.”

The solution

  • usingExtract ClassThe act of categorizing.

earnings

  • Improve code organization
  • Reduce duplicate code

Reconstruction method Description

Extract Class

The problem

A class does more than one thing.



To solve

Create a new class and move related fields and functions from the old class to the new class.



Parallel inheritance system

Parallel Inheritance Hierarchies are a special case of Shotgun Surgery.

Characteristics of the

Every time you add a subclass to a class, you must add a corresponding subclass to another class. This is typically the case when the class name prefix or suffix of an inheritance system is exactly the same.



Question why

The inheritance system is small at first, but as new classes are added, it gets bigger and harder to modify.

The solution

  • The general strategy is to have an instance of an inherited architecture reference an instance of another inherited architecture. If continued efforts to useMove MethodMove Field, you can eliminate the inheritance system on the reference side.

earnings

  • Better code organization
  • Reduce duplicate code

When to ignore

  • Sometimes having a parallel class hierarchy is just a way to avoid more clutter in the program architecture. If you find that trying to eliminate parallel inheritance leads to even uglier code, you should roll back your changes.

Reconstruction method Description

Move Method

The problem

There is a function in your program that communicates more with a class other than the one in which it resides: calling it, or being called by it.



To solve

Create a new function with similar behavior in the class that the function references most often. Make the old function a pure delegate function, or remove the old function entirely.



Move Field

The problem

In your program, a field is used more often by a class other than the one in which it resides.



To solve

Create a new field in the target class and modify all users of the source field to switch to the new field.



Shotgun modification

Shotgun Surgery is similar to Divergent Change, but in reality is completely different. Divergent changes are the changes that affect a single class. Shotgun Surgery is when multiple changes trigger corresponding changes to multiple classes.

Characteristics of the

Any change will require small changes in many different classes.



Question why

A single responsibility is broken down into a large number of classes.

The solution

  • usingMove MethodMove FieldTo move the same behavior from different classes into a separate class. Create a new class if there is no suitable one to hold a move function or field.
  • In general, it can be appliedInline ClassPut a list of related behaviors into the same class.



earnings

  • Better code organization
  • Reduce duplicate code
  • Are more likely to maintain



Reconstruction method Description

Move Method

The problem

There is a function in your program that communicates more with a class other than the one in which it resides: calling it, or being called by it.



To solve

Create a new function with similar behavior in the class that the function references most often. Make the old function a pure delegate function, or remove the old function entirely.



Move Field

The problem

In your program, a field is used more often by a class other than the one in which it resides.



To solve

Create a new field in the target class and modify all users of the source field to switch to the new field.



Inline Class

The problem

A class doesn’t do much.



To solve

Move all the features of this class to another class, then remove the original class.



Further reading

  • Bad code smell and refactoring
  • Code bloat with bad code smell
  • Bad taste of code abuse of object orientation
  • Barriers to code bad taste change
  • Code that doesn’t necessarily smell bad
  • Coupling of bad code smells

The resources

  • Refactoring – Improving the design of existing code – by Martin Fowler
  • https://sourcemaking.com/refactoring