
A program has two audiences: the computer, which only needs the code to be correct, and people, who need to understand it. The second audience is the harder one. Code is read many more times than it is written, by teammates, by reviewers and by you six months from now, when you will have forgotten why you did what you did. Robert C. Martin, author of the influential book Clean Code (2008), estimated that the ratio of time spent reading to writing code is well over ten to one. Clean code is therefore not about elegance for its own sake. It is about making the next change fast and safe.
Clean code is code a competent colleague can read once and understand: what it does, why it does it and where to change it. It has no surprises. Each part does one thing, names tell the truth, and the structure matches the problem. A useful test is to imagine handing the file to a new team member. If they need you sitting next to them to explain it, it is not clean yet.
Naming is the single most powerful readability tool. A good name removes the need for a comment.
A function should do one thing, and its name should say what that thing is. When a function validates input, saves to the database and sends an email, it has three reasons to change and three ways to break. Split it into three functions and a fourth that calls them in order. The top-level function then reads like a summary of the process, and each detail lives in one place.
Other signs a function needs splitting: you need to scroll to see all of it, it has many levels of nested if statements, or it takes more than three or four parameters. Deep nesting can often be flattened with guard clauses: handle the invalid cases first and return early, so the main logic sits at the left margin instead of five levels deep.
Prefer functions without hidden side effects. A function called getUser should not also update a counter in the database. Surprises like that are among the hardest bugs to find.
Good code shows what it does; comments should explain why. "Increment i" above i++ adds nothing. "Retry three times because the payment provider sometimes drops the first request" is valuable, because the reason cannot be seen in the code.
Consistent indentation, spacing and line length let readers focus on logic instead of layout. Do not argue about style by hand: adopt your language's standard and let a formatter apply it automatically on save. Examples include Prettier for JavaScript, Black or Ruff for Python, gofmt for Go and rustfmt for Rust. Add a linter, such as ESLint or Pylint, to flag unused variables, unreachable code and likely bugs. Follow the community style guide, such as PEP 8 for Python, so that your code looks familiar to every developer in that language.
Several well-known principles point in the same direction:
Apply DRY with judgement. Two pieces of code that look alike but represent different ideas should stay separate; merging them too early creates an abstraction that must be twisted every time either idea changes.
Error handling is part of readability. Fail early with a clear message rather than letting bad data travel through the program. Catch exceptions only where you can do something useful, and never swallow them silently with an empty catch block, which hides bugs. Messages should say what went wrong and, ideally, what to do: "Config file settings.json not found in /app" helps; "Error 17" does not.
Group related code together and keep unrelated code apart. Each file or module should have a clear purpose, and its name should reveal it. Keep data access, business logic and user interface in separate layers, so a change to the database does not ripple into the screen layout. Keep files at a manageable size; when a file grows into thousands of lines, it usually contains several responsibilities waiting to be separated.
Refactoring, a term popularised by Martin Fowler's 1999 book of that name, means improving the structure of code without changing its behaviour. It is not a separate project scheduled once a year. Do it in small steps as part of normal work: rename a confusing variable, extract a function, remove duplication. A common rule is the "Boy Scout Rule": leave the code a little cleaner than you found it.
Refactoring safely requires tests. Automated tests confirm that behaviour stays the same while you change the structure, and well-named tests also serve as documentation of what the code is supposed to do.
Reviews are the best place to measure readability, because the reviewer reads your code without your explanations in their head. If they ask what something does, the answer usually belongs in the code, as a better name or a smaller function, rather than in a reply. Reading other people's clean code, for example in well-maintained open-source projects, is also one of the fastest ways to build your own taste.
Clean code is a habit, not a talent. Nobody writes it perfectly in the first draft. Experienced developers write a working version, then reread and improve it before anyone else sees it, exactly as good writers edit their prose.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions