Jump to content

Rule of three (computer programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gojko~enwiki (talk | contribs) at 01:10, 17 January 2008 (page created). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

"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 you are allowed to copy and paste the code once, but that when the same code is replicated three times, it should be extracted into a new procedure. The rule was introduced by Martin Fowler in Refactoring[1] 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. If the code exists in only one place, then it can be easily changed there.

See also

References

  1. ^ Refactoring: Improving the Design of Existing Code by Martin Fowler (ISBN: 978-0201485677)