A rigorous code style can give a good impression and better legibility, and in actual development, it is often encountered that it is your own code, and after a period of time you don’t know it at all. Therefore, for beginners, it is necessary to establish good programming habits, which includes code style, such as indenting the first line, adding spaces after keywords, camel nomenclature, etc., the Google open source project styleguide provides code styles including C++, C#, Java, Python and other 10 programming languages, and you can easily write high-quality code by referring to imitation.
Example:
C++ style guide
Header file
All header files should be protected by #define to prevent multiple inclusions, and for uniqueness, they should be based on the full path in the project’s source tree
#ifndef FOO_BAR_BAZ_H_#define FOO_BAR_BAZ_H_…#endif // FOO_BAR_BAZ_H_
Naming conventions
Use a name that describes the object’s purpose or intent
class MyClass {public:int CountFooErrors(const std::vector<Foo>& foos) {int n = 0; Given a finite scope and context for (const auto& foo : foos) {…++n; } }return n; }void DoSomethingImportant() {std::string fqdn = …; The well-known abbreviation for fully qualified domain name }private:const int kMaxAllowedConnections = …; Be clear in context};
Function declarations and definitions
The return type is on the same line as the function name, and the parameters should be on the same line as much as possible
ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) {DoSomething(); …}
Too many parameters can wrap lines
ReturnType LongClassName::ReallyReallyLongFunctionName( Type par_name1, // 4 spaces indented Type par_name2, Type par_name3) { DoSomething(); } 2 spaces indented…
}
- Java style guide
Package name
Package names are all lowercase, and consecutive words are simply concatenated together (no underscores)
Correct: com.example.deepspace Error: com.example.deepSpace or com.example.deep_space
Class name
Class names are nomenclated with large humps, usually nouns or noun phrases
Character or ImmutableList
The naming of test classes begins with the name of the class they are testing
HashTest or HashIntegrationTest
Method name
Method names are nomenclature with small humps, usually verbs or verb phrases
sendMessage or stop
- JavaScript style guide
Local variable declarations
Local variables are not usually declared at the beginning of their containment block or block-like construction;
Each local variable declares only one variable, and does not use let a = 1, b = 2; Such Statements
Attribute annotations
The property type must be documented, and the description of the private property can be omitted if the code provides sufficient documentation
/** My class. */class MyClass {/** @param {string=} someString */constructor(someString = ‘default string’) {/** @private @const {string} */this.someString_ = someString; }}
Other: