This document defines the code style rules for all byteball projects, i.e. all projects under https://github.com/byteball, not just this repo byteballcore.
All code must be easy to read with minimal contextual knowledge. Code should read like English. The name of every variable/object/function should clearly say what it is or what it does, without need to inspect its history or implementation.
Once the code is easy to read, it becomes also easy to rewrite.
- camelCase or snake_case but never a mixture within the same name.
- Variable names: the main word must be a noun.
- Function names: the main word is usually a verb. A function name must always signify an action. Usually, it is an imperative sentence
doSomethingWithSomething()
, sometimes action is expressed in other clear waysstring2array()
, sometimes action is a reaction to an eventonEvent()
. - Hungarian notation: use it when the type of a variable is not immediately evident from its name. For example,
cities
could be an array or a string of comma-delimited city names, usearrCities
orstrCities
to make the type clear. - Long names are OK, clarity takes precedence.
- Widely accepted conventional names are OK, even if they are not descriptive enough. Example: counter variables
i
,j
,k
. - Use the least abstract names possible. For example,
key
,value
is OK for the code that deals with any keys and any values, but in most cases, we know a bit more about the objects that these variables represent and can give more specific names. - The same entity must be named the same everywhere. The same name in different functions or modules. The same name as both js variable and sql column name.
- Use only snake_case for table and column names
- Elements of SQL language must be in uppercase
SELECT COUNT(*) AS c FROM table_name
- Use aliases for table and column names only when the alias is at least as clear as the original name. For example,
SELECT * FROM sales s
would replace clearsales
with cryptics
, don't do it. Aliases of table names are justified when joining a table to itselfSELECT * FROM sales AS last_year_sales LEFT JOIN sales AS this_year_sales ON ...
- When using aliases, always write
AS
. - Table names must be in plural: table
cities
, not tablecity
(unless the table can never contain more than one record). - Don't rely on fixed table structure in INSERTs
INSERT INTO tab VALUES (?,?)
, always explicitly name the columnsINSERT INTO tab (clo1, col2) VALUES (?,?)
. - In
GROUP BY
andORDER BY
use column names, not indexes.
- Variables must have as small scope as possible.
- Single assignment is preferred over mutable state.
- Avoid using array indexes explicitly, especially large ones (2 and above). Instead, assign the values to clearly named variables
let [year, month, day] = date.split('-')
- Unless you are coding a performance critical piece of code, choose a simple, clear, straightforward algorithm, even if it is not optimal.
- Avoid large blocks of code. Sometimes it makes sense to split them into several clearly named functions, even if the functions are called only once.
- Avoid circular references, keep a clear hierarchy of using code and provider code.
- We use tabs for indent