The code will not deceive, but sometimes the comments will

Comments play a very important role, but if you can, try not to use comments and write more descriptive code. This is because comments are often ignored when you modify the code. So I say this: Comments sometimes deceive (have errors) because the code around it has changed, but it has not been modified.

9 programming principles that can only be deeply understood by experiencing

Source: unsplash

There are three ways to write code:

 

· Use comments in the code.

· Write the comments in a separate file.

· Write self-explanatory code.

 

Let me elaborate on the last point. Here is the meaning of writing more descriptive code:

 

· Design the code base so that it is easy to find and has a reasonable structure.

 

· Don't try to save the initials. Use full names when naming variables, classes, and functions, such as windowManager without wm and readFileToString without rf. This way of naming can be very helpful when you or someone else looks at the code after a few months to understand its meaning.

 

· Extract as many functions as possible and let them perform their duties. Name it according to its function. For example, create a function to read a file into a string and name it readFileToString(String fileName). You can understand its function without reading the code carefully.

 

Ideally, your code consists of a series of such function calls that read like human language. Only when needed, readers will go to deep understanding, these codes themselves have an explanatory effect.

Guess you like

Origin blog.csdn.net/fuli911/article/details/108703674