Methods should use most fields of a class

From CSSEMediaWiki
Jump to: navigation, search
Most of the methods defined on a class should be using most of the data members most of the time. --Riel's Heuristic 4.6, Arthur Riel 1996

This heuristic says that if methods of a class are not really using the majority of the fields in a class then that is an indication that the class may capture more than one key abstraction. If this happens, it also implies that the class is probably not very cohesive and combines data and behavior that do not really fit together.

In an extreme case, half of the methods of a class may use half of the fields, while the other half of the methods uses the remaining fields. This is clearly a violation of the heuristic and occurs because two separate concepts have been combined into one class. This class can be split in two, each capturing one of the two concepts.

This idea is similar to that used by the software metric LCOM (Lack of cohesion in methods) which is a way of measuring the cohesion in a class. It is essentially a measure of how many of the fields are used by which methods.

See also

Personal tools