Programming principles that every programmer must follow

Source: Internet
Author: User

Address: http://kb.cnblogs.com/page/112293/

Good programming principles are closely related to good system design principles and technical implementation principles. The following programming principles have made me an outstanding player in the past few years.ProgramPersonnel, I believe that these principles can greatly improve the programming capability of any developer and enable him to develop programs with stronger maintainability and fewer defects.

Do not repeat yourself(Dry-Don't repeat yourself)-- This is perhaps the most basic principle in programming and development, that is, to tell you not to repeatCode. Many of our programming structures exist to help us eliminate duplicates (such as loop statements, functions, classes, and so on ). Once there is a recurrence in the Program (for example, a long expression, a lot of statements, but all to express the same concept), you need to refine the code, abstract.

Http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Extract Action Principle)-- Related to the "do not repeat yourself" principle, which means that "any piece of functional code in a program isSource codeThe file must be unique ."

Http://en.wikipedia.org/wiki/Abstraction_principle_ (programming)

Keep simple-- Simplicity (Avoiding complexity) should always be your top priority. Simple programs make it easy to write, produce fewer bugs, and make it easier to maintain and modify.

Http://en.wikipedia.org/wiki/KISS_principle

Do not develop features that you do not currently use-- Unless you really need to use it, do not easily add those functions that are not used in a mess.

Http://en.wikipedia.org/wiki/YAGNI

Run the program in the simplest way.-- You have a very good question during development. You need to ask yourself, "How can we easily run the program ?" This helps us keep the program simple during design.

Http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html

Don't let me get started-- This is actually the title of Steve Krug's book about Web interface operations, but it also applies to programming. The main purpose is that the program code should be able to be understood and understood with the minimum effort. If a program requires too much effort for readers to understand, it may need to be further simplified.

Http://www.sensible.com/dmmt.html

Principles of openness and Closure-- Entity items (classes, modules, functions, etc.) in the program should be open to the extended behavior and closed to the modified behavior. In other words, do not write classes that are allowed to be modified by others, or write classes that can be extended by people.

Http://en.wikipedia.org/wiki/Open_Closed_Principle

Write programs for maintainers-- Any program worth writing will be worth your maintenance in the future. It may be maintained by you or by others. In the future, when you have to maintain these programs, your memory for the code will be basically the same as a stranger, so you 'd better keep writing programs for others. One way to help you remember this principle is to "Always remember when writing a program. The person who wants to maintain the program you write in the future will be highly violent, and know where you live ".

Http://c2.com/cgi/wiki? Codeforthemaintainer

Minimum unexpected Principle-- The minimal unexpected principle is usually used in the user interface design, but this principle is also applicable to programming. The program code should try not to surprise readers. That is to say, the coding rules and common habits should be followed, and the organization and naming should be conducted according to the accepted habits. Otherwise, we should try our best to avoid regular programming actions.

Http://en.wikipedia.org/wiki/Principle_of_least_astonishment

Single Responsibility Principle-- A code component (such as a class or function) should only execute a single preset task.

Http://en.wikipedia.org/wiki/Single_responsibility_principle

Minimize Coupling-- A code snippet (code block, function, class, etc.) should minimize its dependency on other code. This goal is achieved by using as few shared variables as possible. "Low coupling is a sign of reasonable structure and excellent design of a computer system. combining it with high aggregation features will be of great significance for the realization of important objectives such as readability and maintainability ."

Http://en.wikipedia.org/wiki/Coupling_ (computer_programming)

Maximize cohesion-- Code with similar functions should be placed in the same code component.

Http://en.wikipedia.org/wiki/Cohesion_ (computer_science)

Hide Implementation Details-- Hide to minimize the impact on other program modules that use this component when you modify a program component.

Http://en.wikipedia.org/wiki/Information_Hiding

Law of Demeter)-- The Program component should only have a relationship with its immediate relatives (for example, the inheritance class, the objects contained in it, and the objects passed in through the parameter entry .)

Http://en.wikipedia.org/wiki/Law_of_Demeter

Avoid Premature Optimization-- Optimization can be considered only when your program has no other problems but is slower than you expected. Optimization can be considered only after other work is done, and you should optimize it based on experience. "We should not consider minor performance improvements. optimization should be a 97% Performance Improvement: Early optimization is the root of all evil,"-Donald knuth.

Http://en.wikipedia.org/wiki/Program_optimization

Code reuse-- This is not a very core principle, but it is as valuable as other principles. Code reuse can improve program reliability and save your development time.

Http://en.wikipedia.org/wiki/Code_reuse

Separation of duties-- Functions in different fields should be managed by completely different code modules to minimize the overlap between such modules.

Http://en.wikipedia.org/wiki/Separation_of_concerns

Embrace change-- This is the subtitle of Kent Beck's book. It is also one of the basic principles of extreme programming and agile development methods. Many other principles are based on this idea: we welcome changes in the face of changes. In fact, some classic software engineering principles, such as minimizing coupling, are designed to make the program more vulnerable to changes. Whether or not you adopt the extreme programming method, this principle is of great significance to your program development.

End

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.