Design principles and Landscape Design Principles

Source: Internet
Author: User
Tags repetition

Design principles and Landscape Design Principles

1.0 OCP principles

That is, the open and closed principle means that the design should be open to the extension and closed to the modification. After the software is designed, the upgrade interface and upgrade space are required.

For example, the wooden sword reduces blood by 20, the ijian reduces blood by 40, and the holy sword reduces blood by 80. If you write a method in the if else structure, it violates the OCP principle. When a new weapon is added, modify the strange If else code. The extension should be used to avoid modifying existing code.

In general, when a method contains lengthy if... Else Or switch... Case structure, and when each branch code business is similar, it often indicates that polymorphism should be introduced here to solve the problem. Here, if we regard different weapons as a Strategy, it is wise to introduce the Strategy Pattern.

Tip: Strategy mode. The English name is Strategy Pattern. It defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode makes the algorithm changes independent of the customer.

Reference: dependency injection, which is known as T2.

2.0 DRY Principle

DRY is short for "Don't Repeat Yourself. It means that in a design, everything should have and only have one representation, and the rest should be referenced. In this case, you only need to adjust this one, and all the places will be changed.

When using the DRY principle, a subtle thing is to carefully determine whether two things are the same thing. Sometimes there are situations where "two things happen to look the same now, but they cannot always be the same in the future. In this case, it is likely necessary to create a superficial repetition.

 

Other principles come from Baidu Baike --

18 principles of excellent programming

1. Avoid repeated principles (DRY-Don't repeat yourself)

The most basic principle of programming is to avoid repetition. There will always be many structs in program code, such as loops, functions, classes, and so on. Once you repeat a statement or concept, it is easy to form an abstract body.

2. Abstract Principle)

Related to the DRY principle. Remember that every important function in the program code can only appear in the source code.

3. Simple Principle (Keep It Simple and Stupid)

Simplicity is the goal of software design. Simple code takes less time, has fewer vulnerabilities, and is easy to modify.

4. Avoid Creating code Avoid Creating a YAGNI (You aren't going to need it)

Do not create new features unless you need them.

5. Do the simplest thing that cocould possibly work)

Do the simplest thing as much as possible. In programming, we must maintain a simple principle. As a programmer, I constantly reflect on how to simplify my work ?" This will help to maintain a simple path in the design.

6. Don't let me think (Don't make me think)

This is the title of Steve Krug, and it is also related to programming. The written code must be easy to read and understand, so that others can appreciate it and give you reasonable suggestions. On the contrary, if it is complicated and difficult to solve, others will always avoid it.

7. Open/Closed Principle)

The software entity (class, module, function, etc.) you write is preferably open source, so that others can expand development. However, for your code, you have to restrict others from modifying it. In other words, you can develop code based on your code, but you cannot modify your code.

8. Write Code for the Maintainer)

A good code should allow me or others to continue writing or maintaining it in the future. It may be easier for me to maintain code, but it is more troublesome for others. Therefore, make sure that the code you write is easily maintained by others. In the book, the original saying goes, "If a maintainer no longer maintains your code, he may be tempted to kill you ."

9. Principle of least astonishment)

The principle of Least Surprise is usually referenced in the user interface, but it is also applicable to written code. The Code should be minimized to surprise readers. That is to say, you only need to write the code according to the project requirements. Other gorgeous functions are useless, so as not to be self-defeating.

10. Single Responsibility Principle)

The function of a code should ensure that only a single explicit task is executed.

11. Low Coupling principle (Minimize Coupling)

Any part of the Code should be less dependent on the code in other regions. Try not to use shared parameters. Low coupling is often a sign of a perfect structure system and excellent design.

12. Maximize Cohesion)

Similar functional code should be put in one part as much as possible.

13. Hide Implementation Details (Hide Implementation Details)

Hiding the Implementation Details principle can minimize the impact on other components when other functions change.

14. The Law of Demeter)

This code is only connected to a part that is directly related to it. (For example, this part of the inherited class, including objects, objects passed by parameters, etc ).

15. Avoid Premature Optimization (Avoid Premature Optimization)

Unless your code runs slowly than you think, do not optimize it. If you really want to optimize it, you must first think about how to use data to prove that it is faster.

"Premature optimization is the root cause of all evil"-Donald Knuth

16. Code Reuse is Good)

Code reuse can improve code readability and shorten development time.

17. Separation of Concerns (Separation of Concerns)

Functions in different fields should be composed of different codes and minimum overlap modules.

18. Embrace Change)

This is the title of Kent Beck and is also considered the objective of extreme programming and agile methods.

Many other principles are based on this concept, that is, you should actively face changes. In fact, some older programming principles, such as the minimum coupling principle, are designed to make the code easy to change. Whether you are an extreme programmer or not, writing code based on this principle will make your work more meaningful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.