Skip to content

Code style guide

Marius Becker edited this page Oct 7, 2018 · 16 revisions

General rules

  • Before you start to work on a code example, look at other code examples in your language, if possible.
  • Space things out to improve readability. Code needs room to breath.
    • x = 3; instead of x=3;
    • int myFunction(int a, int b, int c) instead of int myFunction(int a,int b,int c){
    • for (int i = 0; i < n; i++) { instead of for(int i=0;i<n;i++){
  • Always go for readability over performance or compactness. Don't use clever shortcuts if you could express something in ways that would be easier to understand.
  • If something is not mentioned here, stick to the best practices or a common style guide for your language.

JavaScript

General

Try to write modern JavaScript code. The code examples in the Arcane Algorithm Archive don't need backwards compatibility for older browsers. Use const, let, promises, for..of, higher-level array methods and everything that you can think of. Don't hold yourself back because your code might not run in Internet Explorer 9.

Indentation

Always indent your JavaScript code with 2 spaces. The same goes for JSON.

Variable declarations

Always declare variables as const, unless you have to modify them, in which case you should declare them as let. There is no good reason to use var.

Naming conventions

Name your functions, methods, variables and properties in camelCase. Name your classes in PascalCase.

Strings

Surround strings with double quotes (").

Node or browser?

Write code that works in both, especially in Node. Use console.log() for output instead of window.alert() or modifying the DOM of a web page.

PHP

Coding standard is PSR-2.

Functions defined outside of classes have to be in written as snake_case, to keep them consistent with PHP global functions.

Variables and properties should always be in camelCase.

Python

The coding standard is PEP 8.

Clone this wiki locally