Object-oriented – How does having too many instance variables lead to duplicate code

designdesign-patternsobject-orientedobject-oriented-designrefactoring

According to Refactoring to Patterns:

When a class is trying to do too much, it often shows up as too many
instance variables. When a class has too many instance variables,
duplicated code cannot be far behind.

How does having too many instance variables lead to duplicate code?

Best Answer

Having too many instance variables is not directly related to duplicate code, or vice versa. This statement, in this generality, is false. One can throw two separate classes with no duplicate code into one class - that produces a new class with unseparated responsibilities and too many instance variables, but still no duplicate code.

But when you find a class with too many responsibilites in real world legacy code, chances are high the programmer who wrote it did not care for clean code or SOLID principles (at least, not at the time when he wrote that code), so its not unlikely you will find other code smells like duplicate code in there.

For example, the "copy-paste reuse" anti-pattern is often applied by copying an old method and making some slight modifications to it, without proper refactoring. Sometimes, to make this work, one has to duplicate a member variable and modify that variable a little bit, too. This might result in a class with too many instance variables (more precise: too many very similar looking instance variables). In such a situation, the similar instance variables maybe an indicator for repeated code elsewhere in the class. However, as you noted, this is an artificial example, and I would not conclude a general rule from it.