Kalpana Kalpana (Editor)

Rule of three (computer programming)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that the code can be copied once, but that when the same code is used three times, it should be extracted into a new procedure. The rule was introduced by Martin Fowler in Refactoring and attributed to Don Roberts.

Duplication in programming is a bad practice because it makes the code harder to maintain. When the rule encoded in a replicated piece of code changes, whoever maintains the code will have to change it in all places correctly. This process is error-prone and often leads to problems. Triplication has an even higher cost because it makes maintenance harder yet. If the code exists in only one place, then it can be easily changed there.

However, refactoring code to eliminate duplication takes time, which might be better spent on other tasks. Additionally, choosing a good design to refactor code into becomes easier when there are more examples of duplicated code to see patterns in. With three examples of similar code, it is easier than with two examples to see what parts of the code should be abstracted and what parts should be the same in all cases.

The rule proposes that the cost of maintenance certainly outweighs the cost of refactoring and potential bad design when there are three copies, and may or may not if there are two copies.

As Charles Petzold puts it, "Three or more? Use a for!"

References

Rule of three (computer programming) Wikipedia