About code alignment and about code alignment

Source: Internet
Author: User

About code alignment and about code alignment

Click "ZTE developer community" at the top to follow us

Read an original article by a front-line developer every day

1. Why code alignment?

Programmers do not just write programs, but programs are just a way to implement business. However, it is not that easy to make the way to implement the business more artistic. What is art? Art is not complex. Art is simple, clear, and clear. As we can see a painting, we can feel the beauty. This is the shortest embodiment of the so-called art. In programs, I think art is to write simple, clear, and clear code. The code must be arranged neatly, just like writing an article or designing a flat work. At least keep the equal sign alignment and keep the variable naming standard. If the programming level is higher, you can use some simpler methods to implement functions faster and faster. The second step is to implement a function between each module, like building blocks, independent of each other, and then to maintain functional reusability of the module. Every piece of compliant building block can finally build a beautiful palace.

Code is always maintained like an article. Do you still remember the unsung annoyance when you look at other people's code. Because you see the messy code and the code without comments. You think this must be a code written by a fool. However, when you write your own code, you are immersed in your own sense of accomplishment in implementing the function. You have forgotten to press the tab key several times with a few more spaces.

When you press a few spaces, In order to alignment the codes. My colleague asked you if you have obsessive-compulsive disorder. You can say that I have obsessive-compulsive disorder and I am proud of it.


2 code alignment

Is it a Tab? Or space? How many spaces are there when spaces are used?


2.1 differences between Tab and Space

Tab and space are actually two different symbols, but they have different meanings in programming alignment. A Tab can take the location of a blank space, but a space has only one space.

In the Keil development environment, the Tab and space characters can be displayed. You can see that the Tab and space are edited alternately. Originally, the Tab symbol occupies 2 spaces, the Code (and comments) is messy on the 4-separated empty Tab.

In the code that is outrageous, You can see tabs with three spaces. The preceding examples show that tabs with three spaces are not used.

Many tools can automatically typeset the Code, such as IAR, as long as you select the code to be aligned, Ctrl + T. However, the annotation alignment behind the code is not so well handled. It is even more confusing to use the Tab + space hybrid mode.


2.2 investigation on Tab and Space

Someone analyzed the use of tabs and spaces for code alignment and the use of spaces for popular projects (with a high number of stars) in multiple languages on GitHub, as well as the usage of spaces.



With regard to code alignment, the Code Editor supports tabs and spaces, so I personally think that both methods are acceptable, just look at the one I am used.

Code alignment is actually very easy to handle. It is okay to select an automatic alignment tool, but the comments behind the Code are not so easy to alignment (especially when Tab and space are mixed ), personally, I am used to alignment comments behind the code, so I basically don't need a Tab character.

Personal suggestion: Use a space character for alignment, which occupies 2 or 4 spaces (commonly used ). Advantages: 1. Convenient cross-platform use; 2. Alignment annotation.


2.3 Tab settings

A neat and aligned code is really important!

The status quo is: A piece of code is maintained by multiple people, and multiple editors are used in combination to surprise the code,

The key reason is that the tab and Space key are used together, And the tabs are displayed differently by various compilers!

How can this problem be solved? Of course, the tab is canceled. All tabs are replaced by four spaces! How?

You do not need to use the tab key during encoding. It is impossible to press four spaces in a row! It brings even worse efficiency!

It is impossible to use a unified compiler!

What should we do?

Yes! When you press a tab, the compiler automatically replaces it with four spaces!

So first, set the tab width of all compilers to 4, and the tab is automatically converted to a space.

Source Insight

Open [Options]-> [compile net Options] And enter 4 in [Editing Options]-> [Tab width] in the lower right corner.

Open [Options]-> [Export net Options] and select [Editing Options]-> [Expand tabs] in the lower right corner.

 

PS3

Go to [settings]-> [program settings] and select [edit settings]. In [tab width], enter 4.

Open [settings]-> [program settings] and select [edit settings]. Do not select [real Tab].

 

EditPlus

Open [Document]-> [Tab Indent] And enter 4 in the edit box under [Tab ].

Open [Document]-> [Tab Indent] and select [Insert spaces instead of tab].

 

Notepad ++

Choose [settings]-> [tab settings]-> [size] And enter 4.

Select [settings]-> [tab settings]-> [Replace with spaces]

 

UE

Go to [advanced]-> [configuration]-> [edit] And enter 4 in [tab width ].

Open [advanced]-> [configuration]-> [edit]. Select [use spaces instead of tabs].

 

VC

[Tools]-> [options]-> [tabs]-> [tab size] 4


[Tools]-> [options]-> [tabs]-> [indent size] 4

[Tools]-> [options]-> [tabs]-> [insert spaces] Select


3 How to align the Automatic Code Layout

As you can see, you may have a deep understanding of the Code's typographical alignment and want to adjust your code. Do you want to adjust your code one by one? Of course you don't need it anymore. Here is a good example.


3.1 First align the code

1) VC Method

Suppose there is test. c

Step 1: Use VC to open the file, for example:

Step 2: CTRL + A select all the code.

Step 3: press CTRL + F8 to see that the image is not fully aligned.


2) VI Method

If you have not installed the VC, use this method.

Step 1: Open the test. c file. Isn't there anyone? Check it online!

Step 2: Enter the command "1G = G" in browser Mode"

Note: G must be in upper case, and then changed:

Step 3: Remember to save the file

Enter the ": wq" command.

 

3) verify whether the tab key is used.

After alignment of the Code, do not be excited because the tab key is used for alignment of the above two methods.

Open the file with UE,

Select [view]-> [display tabs and spaces]. The following information is displayed:

">" Indicates tab, and "-" indicates space.

We can see that many tabs are generated during automatic alignment.

 

3) One-time tab conversion to Space

Currently, I only find that UE has this function.

Select [format]-> [convert all tabs into spaces]

5) manual adjustment

After the above processing, you need to take a look at the entire code. In some cases, problems may occur, mainly in the following aspects:

L some of the open source code styles are not ideal.

L if a line of code is too long, you need to manually wrap it.

 

For example, the following code is the most popular in open-source code.

After automatic alignment with VC and VI, the following situations may occur:

Maybe you want this, right? What should you do? Manually tune it! I don't know if there are any better tools in one step.

3.2 Merge settings

Now it's done. Remember this time, submit a simple verification and then submit the ticket to the database, so that you can get the aligned code from the latest version next time.

But you must pay attention to it when writing data into the database. space must be used as the difference for receiving data. Otherwise, the code that you have worked hard to print will not be aligned from the database next time.

 

Choose [view]-> [Option] Red Circle.

Check if you have not selected the database. In this way, merge thinks that you have not changed anything.

The selected items are as follows: Yes, just adjust a few spaces. Put these spaces into the database! What are you waiting?

3.3 S Ource Insight Advanced Settings

1)Draft View selected

Select [View]-> [Draft View]

If no code is selected, the code that is originally very alignment does not have a tab key. It looks like the following, and the Code itself is not alignment.

The selected items are all aligned at once.

2) how to align {} And if

This is often the case when writing code.

You need to manually move the four spaces {} to the left. This is depressing.

In [Options]-> [events Options], click Auto Indent. Select Smart on the left and remove the two check boxes on the right. You can!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.