请输入您要查询的百科知识:

 

词条 Rule of three (computer programming)
释义

  1. See also

  2. References

  3. External links

{{For|the C++ principle|Rule of three (C++ programming)}}

Rule of three ("Three strikes and you refactor") is a code refactoring rule of thumb to decide when similar pieces of code should be refactored to avoid duplication. It states that two instances of similar code don't require refactoring, but when similar code is used three times, it should be extracted into a new procedure. The rule was popularised by Martin Fowler in Refactoring[1] and attributed to Don Roberts.

Duplication is considered a bad practice in programming 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.

However, choosing an appropriate design to avoid duplication might benefit from more examples to see patterns in. Attempting premature refactoring risks selecting a wrong abstraction, which can result in worse code as new requirements emerge[2] and will eventually need to be refactored again.

The rule implies 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 only two copies.

See also

  • Copy and paste programming
  • Don't repeat yourself (DRY)

References

1. ^{{cite book|title=Refactoring: Improving the Design of Existing Code|author=Martin Fowler|author2=Kent Beck|author3=John Brant|author4=William Opdyke|author5=Don Roberts|isbn=978-0201485677|year=1999|publisher=Addison-Wesley Professional}}
2. ^Sandi Metz, [https://www.sandimetz.com/blog/2016/1/20/the-wrong-abstraction "The Wrong Abstraction" (2016)]

External links

  • DRY (c2.com)
  • Once and Only Once (c2.com)

1 : Computer programming folklore

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 20:17:49