Polyglot CheatSheet - Coding Conventions
Coding Convention Quick Lookup
C++ | Java | JavaScript | Python | |
---|---|---|---|---|
Formatter | clang-format | google-java-format | prettier | yapf |
Indentation | 2 | 2 | 2 | 4 |
Line Length | 80 | 100 | 80 | 80 |
Package Name | my::foo_bar | com.example.mypackage | ||
Class Name | ClassName | ClassName | ClassName | ClassName |
Method Name | MethodName | methodName | methodName | method_name |
File Name | file_name.cc | ClassName.java | _ or - |
file_name.py |
Constant | kConstName | CONST_NAME | CONST_NAME | CONST_NAME |
Extra Notes
C++
Google C++ Style Guide https://google.github.io/styleguide/cppguide.html
Formatter: clang-format
and set style
to Google
. https://clang.llvm.org/docs/ClangFormatStyleOptions.html VSCode: Clang-format › Language › Cpp: Style
Google recommends kConstName
for constants to avoid conflicts with macros.
namespace name: all lower-case, with words separated by underscores.
Java
Google Java Style Guide https://google.github.io/styleguide/javaguide.html
Formatter: https://github.com/google/google-java-format
Java pakcage name: no word separators (-
or _
), only lowercase letters and digits.
Download and run
$ java -jar ~/lib/google-java-format-1.13.0-all-deps.jar --replace $(git ls-files | grep \.java)
Each source file can contain one public class. The source file's name has to be the name of that class.
Python
It's official: PEP 8 — the Style Guide for Python Code.
- Single Leading Underscore (
_var
): for internal use. (a hint to the programmer only, not enforced by the Python interpreter, except in wildcard imports. - Double Leading Underscore (
__var
): private methods in a class context. Enforced by the Python interpreter. - Single Trailing Underscore (
var_
): to avoid naming conflicts with Python keywords. - Double Leading and Trailing Underscore (
__var__
): special methods defined by the Python language. E.g.__init__
. - Single Underscore (
_
): for temporary or insignificant variables (“don’t care”).
JavaScript
Format: single quote; but use double quote in jsx tags to be consistent with HTML. (e.g. <div className="sidebar" />
)
Airbnb Style Guide https://github.com/airbnb/javascript/tree/master/react
Formatter: prettier
Go
Formatter: gofmt
Kotlin
file name: ClassName.kt
.