principles
The Principles of Good Programming
KISS
Keep It Simple, Stupid!
Most systems work best if they are kept simple rather than made complex.
Why
- Less code takes less time to write, has less bugs, and is easier to modify.
- Simplicity is the ultimate sophistication.
- It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away.
Resources
YAGNI
YAGNI stands for "you aren't gonna need it": don't implement something until it is necessary.
不要为了未来可能的需求而过分设计,在大部分情况下,未来可能的需求不会发生。所以,Do the Simplest Thing That Could Possibly Work
Why
- Any work that's only used for a feature that's needed tomorrow, means losing effort from features that need to be done for the current iteration.
- It leads to code bloat; the software becomes larger and more complicated.
How
- Always implement things when you actually need them, never when you just foresee that you need them.
Resources
Keep things DRY
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
Each significant piece of functionality in a program should be implemented in just one place in the source code. Where similar functions are carried out by distinct pieces of code, it is generally beneficial to combine them into one by abstracting out the varying parts.
Why
- Duplication (inadvertent or purposeful duplication) can lead to maintenance nightmares, poor factoring, and logical contradictions.
- A modification of any single element of a system does not require a change in other logically unrelated elements.
- Additionally, elements that are logically related all change predictably and uniformly, and are thus kept in sync.
How
- Put business rules, long expressions, if statements, math formulas, metadata, etc. in only one place.
- Identify the single, definitive source of every piece of knowledge used in your system, and then use that source to generate applicable instances of that knowledge (code, documentation, tests, etc).
- Apply the Rule of three.
Resources
Related
- Abstraction principle
- Once And Only Once is a subset of DRY (also referred to as the goal of refactoring).
- Single Source of Truth
- A violation of DRY is WET (Write Everything Twice)
网友评论