Code principles and specifications that every good programmer should follow

This article was originally created and published by the Grapevine technical team

Reprinted, please indicate the source: Grape City official website , Grape City provides developers with professional development tools, solutions and services to empower developers.

 

What is a good programmer?

First of all, we will ask this question first. If you ask this question to 10 people, although the answer may be different, but one thing should be consistent. For me personally, a good programmer should be a person who can fully understand the requirements and can propose feasible solutions to show the results to the end users through teamwork. When it comes to teamwork, it involves code maintainability, so how do you manage a huge code base? If you allow team members to submit random code, it will be difficult to complete bug fixes or new features in the project.

If you want to achieve the goal of maintainability, then each member of the team should guarantee to submit clean and maintainable code. Then you should let your team members follow certain coding principles. Following these principles can make your collaboration with others easier. So what rules should team members follow?

Scouting principles

Boy Scouts is an educational practice system for minors in American society. Children who join the Boy Scouts must learn and abide by some rules, and then obtain various medals. One of the rules is the principle of cleaning activities before leaving the campsite, which is concise and clear:

Leave the campground cleaner than you found it.

Suppose a child comes to a campsite and unfortunately finds two rubbish on the ground, and then he also makes a rubbish in his daily activities. Then when he left, he not only had to clean up his rubbish, but also deal with the two rubbish left by the earlier children. Instead of looking for who lost it. Attention should be focused on creating a better environment for the next camper.

Putting this principle into software production means making check in more tidy than when checking out, at least not making the code worse.

(The picture comes from the network)

Avoid duplication

Try to reduce the output of repeated code, methods and classes in the development process of the project. The basic purpose of most design patterns is to reduce code duplication and encapsulate the reused code as much as possible to improve code reusability and maintainability One of the best ways of sex.

Functional independence

Function independent means that the function or method is as simple as possible, and the function is as independent as possible.

In other words, a method is best to do only one thing. If you think your code is too complicated, what should you do? Pumping method.

The most common mistake that junior programmers make is that there are many kinds of work to be done in one method, which may bring disaster in the life cycle of the software.

Simple and understandable code is better than "smart" code

As one of the smartest groups in the society, programmers often produce some dazzling code blocks when writing code. This part of the code block will be seen after a period of time. It exists in the program like a mystery, although it is very Simple, but not easy to read.

For example: some people like to use trinocular operations instead of if-else in their programs. Although there is no problem in using trinocular operators themselves, when there is a nesting situation, it is a nightmare for the maintainer behind. For example, the following code:

1

(A>B?(A>C?A:C):(B>C?B:C))

In fact, the above code is equivalent to, obviously the following format is easier to understand

1

2

3

4

5

if(A>B){

    (A>C?A:C)

}else{

    (B>C?B:C)

}

Dimit's Law

The Dimit rule was proposed by Lan Holland at the Northeastern University project called Dimit in the fall of 1987. It requires that an object should have the least understanding of other objects, so the Dimit rule is also called the principle of least knowledge. Its purpose is to reduce the coupling between classes and to avoid the occurrence of a change in one class due to excessive coupling and an impact on another class.

I.e.

The YAGNI principle means that you only need to include the necessary functions of the application during development, and do not try to add any other functions you think may be needed. In the development process, in order to meet the possible future needs, some functions are developed in advance. We usually spend a lot of time on the development of these over-designed functions, but this design may not be used in the next two or three years. More energy should be spent on the development of more important functions, the planning of future assumptions should be properly assumed, and later function iterations and code maintenance should be accelerated.

to sum up

Although many principles and norms are mentioned above, these norms need to be practiced at work for a long time to have a deeper understanding. I hope you can learn some basics from this article. Finally, I hope everyone can write beautiful and standardized code.

 

Published 568 original articles · praised 460 · 1.23 million views

Guess you like

Origin blog.csdn.net/powertoolsteam/article/details/105699557
Recommended