Skip to main content

Summary

The big picture of clean code

This page summarizes the clean code principles covered in this documentation.

Checklist

Points to check when writing code:

Naming

  • Do variable and function names express their intent clearly?
  • Are you using searchable names?
  • Do you follow the naming conventions (camelCase, PascalCase, UPPER_SNAKE_CASE)?
  • Do you use full words instead of abbreviations?

Functions

  • Does each function do only one thing?
  • Are there two or fewer arguments (use an object when there are more)?
  • Do you keep side effects to a minimum?
  • Do you reduce nesting with early returns?

TypeScript

  • Are you avoiding any?
  • Are the type annotations appropriate?
  • Are you using unknown together with type guards?
  • Are you using utility types appropriately?

Class design

  • Does each class have a single responsibility?
  • Do you choose composition over inheritance?
  • Do you abstract with interfaces?
  • Are you mindful of high cohesion and low coupling?

General

  • Is the code self-documenting?
  • Have you removed unnecessary comments?
  • Have you turned magic numbers into constants?
  • Is the design easy to test?

Continuous improvement

// Clean code is never perfect on the first try
// Continuous refactoring is what matters

// The Boy Scout Rule
// "Leave it cleaner than you found it"
// When you touch the code, improve it a little and move on

const boyScoutRule = {
principle: "Leave it cleaner than you found it",
action: "When you touch the code, improve it a little and move on",
benefit: "The codebase gradually becomes cleaner"
};

Finally

The goal of clean code is to write code that is readable, maintainable, and resilient to change.

Things to remember
  1. Code is read more often than it is written → Prioritize readability above all.
  2. Your future self is also a reader of your code → Write code that the you of six months from now can understand.
  3. Aim for continuous improvement rather than perfection → Stack up small refactorings.
  4. Make the TypeScript type system your ally → Use types to prevent bugs and to serve as documentation.
  5. Principles are guidelines, not absolute rules → Judge by the situation and reach agreement with your team.

💡 Clean code is a skill, a habit, and a mindset. Make it your own little by little through daily practice.


This documentation is a reworking of "Introduction to Clean Code" for TypeScript.