How can we improve the readability of the program

Don't mess with the days, be careful to let the days mess you up. "Soldiers Assault"-old horse

Several times I looked at the code I wrote and felt very uncomfortable. Why? Because those few letter variables really confused me, I have to remember why I wrote the code like this in the first place. I think it’s a waste of time to think about it. , So I personally compiled four tips on improving the readability of the program.

NO.1:给变量赋予独特的意义

When we define a variable, we need to give a meaningful name to the variable name, just like naming a pet to give it a special meaning. If the variable name cannot be expressed clearly, you may forget what it means when you write the code. The most important thing is that it may take a lot of time to understand it when you look back at the code you wrote later.

NO.2:写注释

Writing comments is really important! ! ! Writing comments not only makes it easier for you to read the program, but also makes it easier for others to read the program you write. If the variables you define don't give any special meaning, then writing comments is also a good choice. C language comments are divided into the following two types:

//这种注释仅限于单行
/*这种注释适用于多行

        */

NO.3:在函数中用空行分隔概念上的多个部分

Although C language does not stipulate that blank lines must be used, using more blank lines can improve the readability of the program and make your code look more beautiful.

NO.4:每一条语句各占一行

Like the third, the C language does not have such a requirement. The format of the C language is actually relatively free. You can put multiple statements on one line, or each statement can be on its own line. When many codes occupy a single line together, it will cause a lot of trouble for you to read the program and it is not convenient for you to sort out your clues. The main thing is not good-looking.

For example, does the following code look more readable and more beautiful

int main()//关于单位转换   ——写注释
{
    
    
int feet,fathoms;    —————使用有意义的变量名
              —————使用空行
fathoms=2;
feet=6*fathoms;     —————每行一条语句
printf("There are %d feet in %d fathoms!\n",feet,fathoms);
return 0;
}

I don't know if the above suggestions can help everyone. If it helps, please like it, thank you.
Public number: Programmer Bob,
a college student who is learning C language, is committed to speak C more thoroughly!
Like and follow~ Thank you

Guess you like

Origin blog.csdn.net/m0_46259251/article/details/104782238