Skip to content

Coding Style

Michael Keilman edited this page Feb 23, 2024 · 4 revisions

This is non-exhaustive documentation of the coding style we use while developing Sirepo.

The style is ever evolving as we learn more about the problem we are solving and the languages we are using. If you see something that does not conform to a style described here it is likely that our style has evolved and it is time to refactor that code!

Python

Our Python style inherits from the pykern coding style.

Imports

Do not use aliases for imports (ex. import numpy as np). Aliases lead to confusion about where an import comes from and what the alias should be.

We no longer support py2 so if you see from __future__ you can remove them.

JavaScript

Strings

We support ES6 so use template strings when you see fit. In the case of multiline strings they are preferred over our older "join" syntax. Thus

const s = `
    foo
    bar
`;

not

const s = [
    'foo',
    'bar',
].join('');

Arrow Functions

We use arrow function notation when functions are assigned to a variable or used as anonymous arguments:

const f = (a, b) => {...};

fWithFunctionArg(() => {...});

If a function does nothing but return an expression, we prefer the simpler

const f = (a) => 2 * a;

except when the expression is complex or is an object literal

const f = (a, b, c) => {
    return [
        (-b + Math.sqrt(b**2 - 4 * a * c)) / (2 * a),
        (-b - Math.sqrt(b**2 - 4 * a * c)) / (2 * a),
    ],
}

const g = () => {
    return {x: 'X'};
}

Note that although parentheses are optional for single-argument functions, we use them to ensure consistency and to simplify searches for functions; const f = (a) => 2 * a;, not const f = a => 2 * a;

Clone this wiki locally