Refactoring Workbook

Symptoms

  • A field is set only at certain times, and it is null (or unused) at other times.

Causes

This can happen when one part of an object has an algorithm that passes around information through the fields rather than through parameters; the fields are valid or used only when the algorithm is active. That the fields are sometimes used and sometimes not suggests that there may be a missing object whose life cycle differs from that of the object holding it.

What to Do

  • Extract Class , moving over the fields and any related code.

Payoff

Improves communication and clarity. May reduce duplication, especially if other places can make use of the new class.

Contraindications

It may not be worth the trouble of creating a new object if it doesn't represent a useful abstraction.

Категории