Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Linux Kernel coding style

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of Linux kernel coding style analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Linux kernel coding style analysis article. Let's take a look.

Why align the code vertically?

Take a simple example:

Int robert_age = 32; int annalouise_age = 25; int bob_age = 250; int dorothy_age = 56

In comparison, the following writing is easier to read:

Int robert_age = 32; int annalouise_age = 25; int bob_age = 250; int dorothy_age = 56

As long as we scan it with our eyes, we can see that the "bob_age" number is abnormal. I can easily identify that these variables are numbers without having to compare them one by one.

This coding style is not widely accepted. So, I need to explain some of the benefits of this style.

Readability of code

90% of programming activities are designed to solve problems. The other 90% is used to understand how the code solves the problem.

Reading code is not much different from reading a novel. We hope that the author can write his ideas clearly, without the need for platitudes, but at the same time abide by the grammar of the language used.

In fact, this point is highly emphasized in the Linux kernel coding style. The name you give to the variable is as important as its function.

Take a look at the following code:

Var thinG=doIt (thestuff,MORE_sTuff); / * LOL! * /

Even if you understand this code very well, it is very difficult for you to read it.

Var totalBill = apply_tax (initialBill, taxRate)

We can make this code clearer by giving meaningful names, spaces, and case sensitivity to variables. This means that the next programmer who takes over our code can understand it more easily.

Why use Monospace fonts?

The debate about whether the code editor should use monospace fonts or proportional fonts has never stopped and will never stop, and each faction has its own preferences.

Some pagans will tell you that proportional is the best font-ignore these people.

Ultimately for readability. What is the easiest way to help us understand the code? That's why IDE has code highlighting, so you can easily tell whether "foo" is a function, a variable, a constant, or a comment. Anything that can help us understand the code is good!

Problems with the editor

Interestingly, many of the criticisms I encountered were not about the vertical alignment of the code, but about the failure of my code editor.

Doing so will destroy the readability and effectiveness of using diff to compare version differences. For example, if there is only one variable on one line that has bug, you can change a lot of lines to align the variables, affecting the view of key changes. There are also diff technologies that can ignore whitespace, but at least the loss outweighs the gain.

Andreas van Cranenburgh

... And...

If you have 50 lines of code to align vertically, you align all the values to the furthest line, and add a line with a larger distance, you need to modify 50 lines. I encountered such a problem and finally found that it was wrong to do so.

Scrollaway

They're right-- in some cases. But the point is that they won't use better tools.

I remember a trick provided by Elastic Tabstops-- how to automatically align code blocks:

The right tool can easily accomplish such a task. Computers are used to do these boring, repetitive things for us-CPU's job is cheap, and we can "waste" CPU's work to make our code more readable.

There are plenty of examples in the Linux kernel code to show that vertically aligned code can make it more suitable for human reading.

Vertical alignment code doesn't work in all cases-- but in most cases, its benefits are great.

Code is the medium through which we express our thoughts. If your tools make it more difficult to understand these ideas, then the tools need to be changed, not us.

This is the end of this article on "Linux kernel coding style analysis". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Linux kernel coding style analysis". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report