[C++ Notes] Develop a good code style

Today, when I was reading the operator overloading part of the C++ Primer book, I suddenly realized that even the same code, a character is not bad, but different styles will give people a different feeling.

The following is the == operator overload function for Sales_item class in the C++ Primer book. First look at the code on the book:
[C++ Notes] Develop a good code style
The following is written by myself:
[C++ Notes] Develop a good code style
No doubt, this is exactly the same code. The function of the code is not the focus of this article. In terms of coding style, which one do you think makes you look pleasing and which makes you look drowsy? Anyway, I think the code in the book looks a lot more comfortable:

  • The writing method in the book puts the inline bool on its own line, and the return value type can be clearly seen. Unlike the return value type I wrote, the function name, formal parameter list, etc. are squeezed in one line, and the focus may be buried;

  • The writing method in the book puts each sentence for equality comparison on one line, which seems to be clear at a glance, instead of writing two && on one line as I did. When reading, I have to look for == expressions everywhere.
    The above is just a simple example to illustrate the importance of coding style. There are many coding standards that need to be paid attention to, such as variable naming, comments, etc. This is not something that an article can tell clearly.

Generally speaking, large companies have their own coding style requirements, such as Huawei’s internal Huawei c language programming specifications. When you first enter these companies, these must be read, if you don’t want to review the code When it was rejected by your colleagues.

If your company does not have similar document requirements, it doesn’t matter. There is a Google-C++ programming specification on the Internet that is worthy of your taste. This is the best C++ programming specification on the earth claimed by Kai-Fu Lee. There is no one. This shows its authority.

The following is an explanatory diagram that covers most of the content of the Google-C++ programming specification, which is searched from the Internet. You can quickly grasp the key content of the specification in a short time. However, a picture cannot replace a document after all. If you have time, please read this document carefully. The download address will be given at the end of the article.
[C++ Notes] Develop a good code style
Note: The original picture is larger, save it locally and open it more clearly.
Download address of Google-C++ programming specification: https://pan.baidu.com/s/1i3gc7lF

Recommended reading:

Carefully organized | Historical dry goods article catalog
[welfare] I collected online boutique course video sharing (part 1)
[data structure and algorithm] easy to understand binary tree traversal
[data structure and algorithm] easy to understand binary search tree

Focus on server background technology stack knowledge summary sharing

Welcome to pay attention to communication and common progress

[C++ Notes] Develop a good code style

Coding

The code farmer has the right way to provide you with easy-to-understand technical articles to make technology easier!

Guess you like

Origin blog.51cto.com/15006953/2552123