Skip to content

Latest commit

 

History

History
111 lines (76 loc) · 2.9 KB

File metadata and controls

111 lines (76 loc) · 2.9 KB

Code Quality Rules

Validate after every code change

Run the project's check/lint command after modifying any .ts, .tsx, .js, .jsx file. Skip for docs-only or non-code tasks.

ESLint autofix workflow

  • Use ESLint autofix only for auto-fixable issues (import sorting, etc.)
  • Never sort imports manually

DRY — extract repeated values

  • Extract repeated strings into constants
  • Extract repeated logic into functions
  • If the same value appears 2+ times, it should be a named constant
// Bad
if (status === 'completed') ...
if (status === 'completed') ...

// Good — or better, use a const array + derived type
const isCompleted = status === 'completed'

No magic numbers or strings

Every meaningful value should be a named constant or derived from config.

// Bad
const fee = amount * 0.003
setTimeout(retry, 5000)
if (tokens.length > 50) paginate()

// Good
const feeRate = 0.003
const fee = amount * feeRate

const retryDelay = convertDuration(5, 's', 'ms')
setTimeout(retry, retryDelay)

const maxTokensPerPage = 50
if (tokens.length > maxTokensPerPage) paginate()

Descriptive error messages

Error messages should say what was expected and what was found.

// Bad
ensurePresent(token, 'token')
throw new Error('Invalid input')

// Good
ensurePresent(token, 'selected token for trading pair')
throw new Error(`Expected positive amount, got ${amount}`)

Readable code over comments

Write self-documenting code. Comments only when necessary:

  • Complex business logic not obvious from code
  • Non-obvious algorithmic decisions
  • Temporary workarounds/TODOs with context

Never comment to explain what code does (should be self-explanatory) or restate variable/function names.

// Bad
// Check if user is active
const isActive = user.status === 'active'

// Good — self-explanatory, no comment needed
const isActive = user.status === 'active'

// Good — comment explains non-obvious WHY
// Solana requires 2 extra confirmations during high congestion periods
const confirmations = baseConfirmations + 2

Reuse shared utilities

Before creating local helpers, check for existing shared utilities in the project's shared/utils or lib directories.

When creating new utilities: search shared first, make generic, place in appropriate category folder.

No console.log in production code

  • Remove all console.log debugging before committing
  • Use proper logging utilities if the project has them
  • console.error and console.warn are acceptable for genuine runtime warnings

Avoid boolean parameters

Boolean params are unclear at the call site. Use object params or separate functions.

// Bad — what does `true` mean?
renderToken(token, true)

// Good — self-documenting
renderToken({ token, showPrice: true })

// Good — separate functions for distinct behavior
renderTokenWithPrice(token)
renderTokenCompact(token)