logo

Programming Languages - Coding Conventions

Last Updated: 2024-01-21

"Coding Conventions" = naming, formatting, program construction, etc.

It's important to know the established conventions, so that programs you write will be easy for other programmers to understand.

Coding Convention Quick Lookup

C++ Java JavaScript Python Go
Formatter clang-format google-java-format prettier yapf gofmt
Indentation 2 2 2 4 tab
Line Length 80 100 80 80 no
Package Name my::foo_bar com.foo.mypackage mypackage
Class Name ClassName ClassName ClassName ClassName
Method Name MethodName methodName methodName method_name MyFoo/myFoo
File Name file_name.cc ClassName.java _ or - file_name.py No _
Constant kConstName CONST_NAME CONST_NAME CONST_NAME MixedCaps

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

Formatter: prettier

Go

Formatter: gofmt. There's only one way to format Go code, enforced by gofmt.

No Line Length restrictions (you may see very long lines in go code, expecially function signature).s

Go source code uses MixedCaps or mixedCaps (camel case) rather than underscores (snake case) when writing multi-word names.

Words in names that are initialisms or acronyms should have the same case, e.g., URL or url but NOT Url. (This is different from Google's C++ guide.)

Function and method names should not use a Get or get prefix, unless the underlying concept uses the word “get”. (This is also quite different from other languages.) Call your string-converter method String not ToString.

One-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun: Reader, Writer, Formatter, CloseNotifier etc.

Google style guide: https://google.github.io/styleguide/go/

Kotlin

file name: ClassName.kt.

Rust

https://rust-lang.github.io/api-guidelines/naming.html