Original link: https://yousazoe.top/archives/bf0a0e19.html
introduction
Google often releases open source projects, which means accepting code from other code contributors. But if the code contributor’s programming style is inconsistent with Google’s, it will cause a lot of trouble for code readers and other code committers. Google has therefore published its own programming style guide to make Google’s programming style known to anyone who submits code.
C++ is the main programming language for most of Google’s open source projects. As every C++ programmer knows, C++ has many powerful features, but this power inevitably leads to complexity, making code more prone to bugs and harder to read And maintenance.
The purpose of this guide is to navigate the complexities of C++ by elaborating its considerations. These rules keep the code manageable while still making efficient use of C++’s language features.
Style, also known as readability, is the convention that guides C++ programming. Using the term “style” is a bit of a misnomer, because these conventions go far beyond source code file formatting.
One of the ways to make code manageable is to enforce code consistency. It is very important that any programmer can quickly read your code. Keeping a consistent programming style and following conventions means that it is easy to follow the rules of “pattern matching” Infer the meaning of various identifiers. Creating common, required idioms and patterns can make code easier to understand. There may be good reasons to change certain programming styles in some cases, but we should still follow the principle of consistency and try not to Do this.
Another point of this guide is the bloat of C++ features. C++ is a huge language with a lot of advanced features. In some cases, we restrict or even prohibit the use of certain features. This is done to keep the code clean and avoid these features Various problems that can be caused. The guide lists such features and explains why their use is restricted.
Open source projects led by Google are all compliant with this guideline.
Note: This guide is not a C++ tutorial, we assume that the reader is already very familiar with C++.
head File
Usually each .cc
file has a corresponding .h
file. There are some common exceptions, such as unit test code and .cc
files that only contain the main()
function.
Correct use of header files can make a huge difference in code readability, file size, and performance.
The following rules will guide you through various pitfalls when using header files.
scope
kind
function
Fantastic tricks from Google
Other C++ Features
naming convention
Notes
Format
Rule exceptions
concluding remarks
Use common sense and judgment, and be consistent.
When editing code, take a moment to look at other code in the project and familiarize yourself with the style. If the if
statement in other code uses spaces, you should use it too. If the comments are enclosed in a box with an asterisk (*) , then you should do the same.
The point of a style guide is to provide a general programming convention so that people can focus on implementing the content rather than the presentation. We are showing a general style guide, but local style is also important, if you are in a file The newly added code is far from the original code style, which destroys the overall beauty of the file itself, and also disrupts the reader’s rhythm when reading the code, so try to avoid it.
Alright, enough about coding style; the code itself is more fun. Enjoy!
This article is reprinted from: https://yousazoe.top/archives/bf0a0e19.html
This site is for inclusion only, and the copyright belongs to the original author.