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

What are the methods of code specification

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the methods of code specification". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Code specification

Variable naming

Here I would like to talk briefly about the issue of norms.

"the authoritative C++ specification is mainly Google". I downloaded a Chinese version for you and replied: googlec++ programming specification in the background of "Code Random Recorder" on the official account.

"specific norms should be based on their own team style", integration into the team is the most important.

Let me first talk about the naming of variables.

The mainstream has the following three variable rules:

Nomenclature of small hump and big hump

Underscore nomenclature

Hungarian nomenclature

Small hump, the first letter of the first word is lowercase, and the other words are capitalized. For example, the int myAge; Big Hump method capitalizes the first letter of the first word. For example: int MyAge; generally speaking, java and go both use humps, and C++ functions and structures are also named after big humps, "as you can see in the solution, my C++ code style is small hump, because the default function given on leetcode is named after small humps, so I do as the Romans do.

Underscore naming is that every logical breakpoint in a name is marked with an underscore, such as int my_age. "underscore naming has become popular with the advent of the C language. If you have seen UNIX advanced programming or UNIX network programming, you will find that this naming method is widely used."

The Hungarian nomenclature is: variable name = attribute + type + object description, for example: int iMyAge, which is popularized by a Hungarian programmer within Microsoft and then to Windows developers around the world.

In the era when there is no IDE, this naming method can well remind developers of the meaning of traversing. For example, if you see iMyAge, you will know that it is an int variable without looking for its definition. The disadvantage is that once the attribute of the variable is, then the name of the variable will be changed throughout the project, so it will be difficult to maintain the code.

"at present, IDE is very developed, there is no need to mark variable attributes, IDE will help us identify, so basically no one uses the Hungarian nomenclature", although I do not use the IDE,VIM method.

I made a summary as shown in the picture:

Programming style

Horizontal blanks (code spaces) often see that some students' codes are piled together, which seem to be laborious, or there are spaces between them, some do not have spaces, and some are very inconsistent, and some students even omit all spaces in order to simplify the code.

If you pay attention to the code style on my solution, my spaces have a unified specification.

"all my C++ code is strictly in accordance with the Google C++ programming specification, so the code looks refreshing."

Let me give you some examples:

There must be a space around the operator, for example

I = I + 1

The delimiter (, and;) has no space in the first bit and keeps the space in the second bit, for example:

Int I, j; for (int fastIndex = 0; fastIndex

< nums.size(); fastIndex++) 花括号和函数保持同一行,并有一个空格例如: while (n) { n--; } 控制语句(while,if,for)前都有一个空格,例如: while (n) { if (k >

0) return 9; n Murray;}

The following is the strength buckle 283 that I just wrote. Move zero code, you can take a look at the overall style, pay attention to the details of the spaces!

Class Solution {public: void moveZeroes (vector& nums) {int slowIndex = 0; for (int fastIndex = 0; fastIndex < nums.size (); fastIndex++) {if (nums [fastIndex]! = 0) {nums [slowIndex++] = nums [fastIndex];} for (int I = slowIndex; I < nums.size () ITunes +) {nums [I] = 0;}

Of course, I am not saying that we must follow the Google specification, the code style is actually uniform, there is no strict saying who is right and who is wrong.

This is the end of the content of "what are the methods of code specification?" thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 272

*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