Design patterns learning: KISS, YAGNI principle

KISS principle: try to keep it simple

  • Keep It Simple and Stupid.
  • Keep It Short and Simple.
  • Keep It Simple and Straightforward.

Code readability, maintainability is very important to measure code quality standards.
The code is simple enough, it means easy to read, bug more difficult to hide.

The fewer lines of code, the more "simple" it?

The same function, complex technology piece of code used, such as regular code amount happened; piece of code using a simple code amount tools second; a code own implementing logic instead of tools, up to the code amount;
a regular increase of the code maintenance costs, and write bug-regularization is not an easy task. Is not a simple code
to their full implementation logic, readability is not high, is not a simple code
using simple tools, a moderate amount of code that is simple code.

Code logic complexity it is violating the KISS principle you?

For complex problems, solutions with sophisticated methods, does not violate the KISS principle. To see whether the KISS principle, is in terms of specific issues.

How to write the code to meet the KISS principle?

  • Do not use colleagues may not understand the technology to implement the code. For example, the previous example of a regular expression, as well as some programming languages ​​are too advanced grammar.
  • Do not re-create the wheel, to be good at using tools already have libraries. Experience has shown himself to achieve these libraries, the bug probability will be higher maintenance costs are relatively high.
  • Do not over-optimization. Do not over-use of some clever but useless (for example, bit operations instead of arithmetic, complex conditional statements instead of if-else, use some too low-level functions, etc.) to optimize the code, sacrifice readability of the code.

In doing development, we must not over-designed, simple things do not feel there is no technical content. In fact, the more simple method can solve complex problems, the more it can reflect a person's ability.

YAGNI principle, do not over-design

For example, our system is temporarily only Redis to store configuration information, it could be used ZooKeeper. According to YAGNI principle, not previously used in ZooKeeper, we do not need to write this code in advance. Of course, this does not mean we do not need to consider the expansion of the code. We still have to set aside a good extension points, until the time of need, to go to achieve ZooKeeper to store configuration information in this section of code.

As another example, we do not need to rely on the introduction of advanced development package in the project. For Java programmers, we often use Gradle or Maven to manage the dependent library (library). I found that some of my colleagues in order to avoid the development of library package deletion or modification Maven Gradle profiles frequently mention went to projects in the introduction of a large number of common library package. In fact, such an approach is contrary to the principle of YAGNI.

We can see from just the analysis, YAGNI principles are not about children with KISS principle. KISS principle talking about the "how-to" questions (try to keep it simple), and the principle YAGNI said "want it" questions (do not currently needed).

Guess you like

Origin www.cnblogs.com/jinlin/p/12073143.html