Beautify your code

Source: Internet
Author: User
I remember reading an article a long time ago, saying how to write code can make your code more beautiful. I saw some comments about code beautification on the Forum a few days ago, so I also wrote something ......

A good programmer is not only good but also beautiful in writing code. Anyone who has learned it can understand it. In order to better exchange code, we need to beautify our code.

Code beautification mainly utilizes indentation, spaces, and line breaks. Some editing software provides the automatic indent function.

From the basics, such as writing functions:
Space Application:
Void test (void)
{
}
If there is no parameter, write a void in the parameter list to let you know that this function has no Parameter
Void test (const char * filename, Int & file_len)
{
}
If there are multiple parameters, the second parameter can be left blank. If not, for example:
Void test (const char * filename, Int & file_len)
It's awkward ......
Definition of variables:
Int name;
Char path [256];
Long * pdata = NULL;
The basic type + space + variable name, and I think the line feed should also be defined when multiple variables of the same type are defined, for example:
Int A, B, C; I think this method of defining variables is a bit inappropriate. 1. It is not easy to add comments. 2. It is not easy to understand.
If so:
Int A = 0; // This test variable
Int B = 100; // ^
Int c =-3; // ^
Does it make it easier to understand?
For example, when performing arithmetic operations:
Int result = a * B + c-d;
Appropriate spaces will also produce beautiful effects.
There are also function calls:
Printf ("make our code beautiful! Your code line = % d ", 10 );
Fopen (filename, "rb ");
Add a space before the second parameter. I think this is more beautiful.
There is also the use of indentation: for example, define a function
Before beautification:
Void loadbitmap (lpdirectdrawsu *** ce lpddstemp, const char * filename, bool bcolorkey)
{
If (lpddstemp = NULL ){
Return;
}
File * fp = fopen (filename, "rb ");
If (FP = NULL ){
Return;
}
}
After beautification:
Void loadbitmap (lpdirectdrawsu *** ce lpddstemp, const char * filename, bool bcolorkey)
{
If (lpddstemp = NULL ){
Return;
}

File * fp = fopen (filename, "rb ");
If (FP = NULL ){
Return;
}
}
How is it? Is it easier to understand?
Learning proper indentation will make the code more elegant

Appropriate line breaks can also make it easier for people to understand the code.
For example:
Void test (void)
{
Int;
Int B;
Int C;
// Space makes people realize that the definition of variables is over and other code is written.
Cin>;
Cin> B;
Cin> C;
Cout <"A + B + C =" <A + B + C <Endl;
}
As long as you care, your code will be more beautiful. Believe me!
Make your code more beautiful!

I wanted to write something on the Forum. It was depressing. My document was too bad ......

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.