Code Style
When writing code for Cryb, we would like you to follow these guidelines so we can keep our code clean and consistent across all projects.
This applies for any JavaScript/TypeScript-based project. For Rust, we stick to rustfmt
/clippy
default rules.
These rules are generally enforced via our @cryb/eslint-config
package.
If there's a mismatch between the guidelines outlined here, please open an issue.
- Indent size should be two spaces
- Files should end in
LF
with a newline, encoded inUTF-8
- No semicolons unless required
- Use single quotes instead of double quotes
Use const
when the variable isn't mutable. Otherwise, use let
. Never use var
.
Ensure that there is a space between different types of variable declarations. There should be room to breathe.
Example:
const product = 'Cryb'
let mutableVariable = 'Yo'
Please view the following examples for how variables should be named.
const name = 'William'
const appName = 'Cryb'
interface Config {
deleteOnExit: boolean
}
div.profile-wrapper {
background-color: red
}
DELETE_ON_EXIT=1
export default {
delete_on_exit: process.env.DELETE_ON_EXIT
}
Use the ===
operand for comparing values.
Statements should be written on new lines, away from the same line as the if
statement.
Always have the brackets up against the statement and away from the curly braces.
Don't use brackets if they are not needed.
Example:
const product = 'Cryb'
if (product === 'Cryb')
console.log('Hey look, it\'s Cryb!')
else if (product === 'Some other product name') {
console.log('Oh, it\'s not Cryb.')
destroyService(product)
} else
return
Sort imports by the following in terms of priority:
- Vendor Packages
- Models
- Schemas
- Interfaces
- Utils
Please also sort by line width.
Example:
import fs from 'fs'
import User from './models/user'
import IUser from './models/user/defs'
import StoredUser from './schemas/user.schema'
import { extractUserId } from './utils/helpers.utils'
Method chaining is fine—infact we recommend it—but to prevent long lines, please split your method calls up into multiple lines.
Example:
// ❌ This is too long.
functionCall().then(() => console.log('hey!')).catch(err => console.log('error!')).finally(() => console.log('finally!'))
// ✔️ This is better and is easier to read.
functionCall()
.then(() =>
console.log('hey!')
)
.catch(err =>
console.log('error!')
)
.finally(() =>
console.log('finally!')
)
// This is my demo method.
const myMethod = () => 'Hello World!'
/**
* This method is for demo purposes.
**/
const myMethod = () => 'Hello World!'