# Formatting

# Brackets

We adhere to the one true brace style (OTBS) for brackets. OTBS means that the opening brace for a code block follows its corresponding statement or declaration on the same line.

Bad

if (get(isStrongholdLocked))
{
    // ...
}
else
{
    // ...
}
       
function getStrongholdStatus(): StrongholdStatus
{
    // ...
}

Good

if (get(isStrongholdLocked)) {
                             ^
} else {
       ^
}
       
function getStrongholdStatus(): StrongholdStatus {
                               ^ 
}

The only circumstances where curly brackets may be omitted are when...

  • An anonymous callback or lambda function is being defined, e.g.

    Bad (excessive)

    account.messages.filter((message) => {
        return !message.confirmed
    })

    Good (concise)

    accounts.messages.filter((message) => !message.confirmed)
                                          ^--
  • Handling a (simple) case inside of a switch statement, e.g.

    switch (ledgerDeviceStatus) {
        case Disconnected:
            // do something simple
            break
        case AppOpen: {
                      ^
            // do something complex
            break
        }
        ^
        default:
            // handle default case
            break
    }

# Commas

Unless the file in question is of JSON format, we should use trailing commas wherever possible in the code. In short trailing commas follow the last item in a larger array, despite not having a following item. The reason for this is to make code more editable when we need to move, add, or remove data from said array.

Bad

const anArray = [
    "string1",
    "string2",
    "string3"
]

Good

const anArray = [
    "string1",
    "string2",
    "string3",
             ^
]

⚠️ JSON does NOT support trailing commas; only ES5+ code.

# Quotes

We use single quotes throughout the codebase.

Bad

enum ProfileType {
    Stronghold = "stronghold",
    Ledger = "ledger",
}

Good

enum ProfileType {
    Stronghold = 'stronghold',
    Ledger = 'ledger',
}

We use double quotes only in the following places:

  • JSON metadata (NOT supported)
  • Rust source code (NOT syntactical)
  • HTML elements attributes (purely stylistic as single quotes are supported here)

# Semicolons

We are choosing to NOT use semicolons anywhere in the code (unless it is SCSS/CSS code as it is hard-requirement of the syntax).

# Spacing

Spacings are another consideration to make when writing clean code.

They are used in the following places:

  • After a comment

    Bad

    //Some useful comment

    Good

    // Some useful comment...
      ^
  • Before and after parentheses for if statements

    Bad

    if(get(isStrongholdLocked)){
        // ...
    }

    Good

    if (get(isStrongholdLocked)) {
      ^                         ^
    }
  • Before and after parentheses for for loops

    Bad

    for(int i = 0; i < messages.length; i++){
        // ...
    }

    Good

    for (int i = 0; i < messages.length; i++) {
       ^                                     ^
    }
  • Before and after parentheses for switch statements

    Bad

    switch(ledgerDeviceStatus){
        // ...
    }

    Good

    switch (ledgerDeviceStatus) {
          ^                    ^ 
    }
  • Inside import statements

    Bad

    import {isStrongholdLocked,getStrongholdStatus} from '@common/stronghold'

    Good

    import { isStrongholdLocked, getStrongholdStatus } from '@common/stronghold'
            ^                   ^                   ^
  • Inside object definitions

    Bad

    const myObject = {num: 1, str: 'one'}

    Good

    const myObject = { num: 1, str: 'one' }
                      ^                  ^ 
    
    const myObject = { ...anotherObject, field: 'data' }
                      ^                               ^ 

⚠️ Do NOT use tabs for indentation; use spaces instead.