18 principles of excellent programming

Source: Internet
Author: User

The principles of good 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.

Author Profile: Christopher Diggins is a Canadian Senior Programmer with 25 years of programming experience. He worked for Microsoft and Autodesk and founded two profitable Internet companies.

He is one of the authors of C ++ cookbook and has compiled a programming language heron.

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.