Best Practices and Patterns

The Code Review That Saved Our SaaS

Thursday 4:15 PM, quarterly review. Our CTO reviewing pull requests from the last 3 months: 347 PRs merged.

He stopped at PR #183: "Who approved this?"

// The offending code
function processPayment(data: any): any {
  const amount = data.amount;
  const userId = data.userId;
  return chargeCard(amount, userId);
}

"This went to production?"

It did. And it caused the May 12th incident: $47,000 in failed payments because data.userId was sometimes a number, sometimes a string, and our payment processor expected strings.

CTO's decree: "We need standards. Now."

I was tasked with creating our TypeScript Best Practices Guide. Two weeks of research. Analysis of 200,000+ lines of our production code. Interviews with every senior dev.

Result: A checklist that catches 90% of issues before code review. Our bug rate dropped 61% in the next quarter.

Here's everything we learned.


Project Structure

File Naming


Naming Conventions

Interfaces vs Types

Variables and Functions


Type Safety Best Practices

Rule 1: Never Use any

Rule 2: Strict Null Checks

Rule 3: Use Type Guards


Function Best Practices

Explicit Return Types

Avoid Optional Parameters Hell


Error Handling

Custom Error Classes

Result Type Pattern


Async/Await Best Practices


Linting with ESLint

Installation

Configuration


Formatting with Prettier


Common Patterns

1. Builder Pattern

2. Factory Pattern

3. Repository Pattern


Code Review Checklist

Our 90% bug-catch checklist:

Type Safety

Naming

Structure

Testing

Documentation


Your Challenge

Refactor bad TypeScript code using best practices:


Key Takeaways

  1. No any - use unknown or proper types

  2. Explicit returns - always specify return types

  3. Strict null checks - handle null/undefined explicitly

  4. Type guards - narrow unions safely

  5. Descriptive names - code is read more than written

  6. Error handling - typed custom errors

  7. ESLint - catch issues automatically

  8. Patterns - builder, factory, repository

  9. Code review - checklist catches 90% of bugs


What I Learned

PR #183 went to production with any types. $47,000 in failed payments.

That code review led to our TypeScript Best Practices Guide. 90% of issues now caught before merge. Bug rate down 61%.

The standards we implemented:

  1. No any - enforced by ESLint

  2. Explicit returns - required in code review

  3. Type guards - for all union types

  4. Naming conventions - automated checks

  5. Error handling - custom typed errors only

Before standards, TypeScript was inconsistently applied. Some files had perfect types. Others had any everywhere. Code quality was a lottery.

After standards, every file follows the same patterns. New devs know what's expected. Code review is faster. Bugs are rare.

The lesson: TypeScript without standards is JavaScript with extra syntax. The compiler can only help if you let it.

Enforce best practices. Make them automatic. Watch your bug rate plummet.


Next: Building a Real-World CLI Tool

In the next article, we'll build a complete TypeScript CLI tool from scratch. You'll learn how I built a deployment automation tool that saved our team 15 hours per week.

Last updated