当前位置:网站首页>Programming principles

Programming principles

2022-06-13 09:43:00 Wudi 98

The design principle is to improve code maintainability , Help build flexibility 、 The cornerstone of a stable system . It mainly includes General design principles and S.O.L.I.D principle

General design principles It is a basic principle that we should adhere to and abide by in any development .
S.O.L.I.D principle It is more suitable for supporting object-oriented high-level languages , Some principles may not apply to current front-end development , But it is also beneficial for us to understand its ideas .

General design principles (Common Design Principles)

Keep It Simple and Stupid (KISS)

● The code should be easy to read and maintain .
● Don't over design a plan , The simplest solution to meet the needs is the best solution .

Don’t Repeat Yourself (DRY)

Don't repeat , Repetitive logic should be pulled apart for reuse , Repeated operations should be instrumented .

Tell, Don’t Ask

Also called “ Hollywood principle ”, This principle is about encapsulation and responsibility allocation . When interacting with other modules , It should tell the module what you want to do directly, not by judging the status of that module , Recall operation .

You Ain’t Gonna Need It (YAGNI)

Only the functions required by the application , Don't add functions you think you need .
TDD Is a kind of compliance YAGNI Development model .

Separation of Concerns (SoC)

Separate functions into independent function modules , These modules have their own independent behavior and data , To improve code reusability 、 Maintainability and testability .

S.O.L.I.D Design principles

Principle of single responsibility (Single Responsibility Principle - SRP)

This heel SoC Close , It specifies that each object should focus on only one function point 、 There is only one reason to change , Avoid Swiss Army knife objects .

Open and closed principle (Open-Closed Principle - OCP)

Open to expansion , Closed to modification .
Open to expansion , It means when there is a new demand or change , You can extend existing code , To adapt to the new situation . Closed to modification , It means that once the design is complete , You can do your job on your own , Add requirements without modifying their internal implementation .

Richter's principle of substitution (Liskov Substitution Principle - LSP)

Any subclass can replace the parent class without any modification .

Interface separation principle (Interface Segregation Principle - ISP)

Group similar interface methods to form a new interface , Avoid implementing a huge interface .

The principle of Dependence Inversion (Dependecy Inversion Principle - DIP)

The program depends on the abstract interface , Don't rely on concrete implementation , In short, it is interface oriented 、 Abstract oriented programming .

原网站

版权声明
本文为[Wudi 98]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130938399162.html