sinpro.dev
Docs
Company Information
About
  • Company Information
    • Portfolio
    • About
    • Services
    • Events
  • Dev Environment
    • macOS Settings
    • Chrome Extensions
    • VSCode Workspace Settings
    • VSCode Workspace Extensions
    • Global NPM Packages
    • npm-check-updates
    • SvelteKit
    • Customize Zsh
    • ChatGPT Prompts
    • Tailwind CSS
    • Warp
    • Keyboard Shortcuts
  • Assets
    • Assets
    • JPG/PNG to AVIF
    • TTF to WOFF2
  • Clean Code
    • Clean Code
    • Format
    • Quality
    • Variables
    • Functions
    • Objects and Data Structures
    • Classes
    • Concurrency
    • Error handling
    • Comments
  • Code Style
    • TypeScript Config
    • Prettier
    • ESLint
    • Stylelint
  • Testing
    • Vitest
    • Playwright
  • Git
    • Git Branches and Commits
    • Git Hooks
    • Git User Profiles
    • Git for Windows
  • GitHub
    • GitHub Issues
    • GitHub Pull Requests
    • GitHub Repository Settings
    • GitHub Branch Protection
    • GitHub Actions
  • Code Quality
    • SonarCloud Coverage
  • Server
    • SSH
    • PM2
    • Caddy
    • Updating the server
    • ngrok
  • Team sinProject
    • Our Team Policy
    • Equipment and Supplies
    • Books
    • Slack
    • Locales
    • Funny Apps
    • Docs History
  • Talk
    • Talk
    • Creating a Project
    • App Structure

Clean Code

Functions

Edit this page

Use early return pattern

Bad
function foo(): void {
  if (argument1.is_valid()) {
    if (argument2.is_valid()) {
      if (argument3.is_valid()) {
        // DO SOMETHING
      }
    }
  }
}
Good
function foo(): void {
  if (!argument1.is_valid()) return
  if (!argument2.is_valid()) return
  if (!argument3.is_valid()) return

  // DO SOMETHING
}

Functions should do one thing

Bad
function email_clients(clients: Client[]) {
  clients.forEach((client) => {
    const client_record = database.lookup(client)
    if (client_record.is_active()) {
      email(client)
    }
  })
}
Good
function email_clients(clients: Client[]) {
  clients.filter(is_active_client).forEach(email)
}

function is_active_client(client: Client) {
  const client_record = database.lookup(client)
  return client_record.is_active()
}

Function names should say what they do

Bad
function add_to_date(date: Date, month: number): Date {
  // ...
}

const date = new Date()

// It's hard to tell from the function name what is added
add_to_date(date, 1)
Good
function add_month_to_date(date: Date, month: number): Date {
  // ...
}

const date = new Date()

add_month_to_date(date, 1)

Don’t use flags as function parameters

Bad
function create_file(name: string, temp: boolean) {
  if (temp) {
    fs.create(`./temp/${name}`)
  } else {
    fs.create(name)
  }
}
Good
function create_temp_file(name: string) {
  create_file(`./temp/${name}`)
}

function create_file(name: string) {
  fs.create(name)
}

Avoid negative conditionals

Bad
function is_email_not_used(email: string): boolean {
  // ...
}

if (is_email_not_used(email)) {
  // ...
}
Good
function is_email_used(email: string): boolean {
  // ...
}

if (!is_email_used(node)) {
  // ...
}

Remove dead code

Bad
function old_request_module(url: string) {
  // ...
}

function request_module(url: string) {
  // ...
}

const req = requestModule
Good
function request_module(url: string) {
  // ...
}

const req = requestModule
Variables Objects and Data Structures
© sinProject. v0.73.0
On this page
  • Functions
  • Use early return pattern
  • Functions should do one thing
  • Function names should say what they do
  • Don’t use flags as function parameters
  • Avoid negative conditionals
  • Remove dead code