Worried your code will baffle the next reader, or you next year? Learning how to write clean code comes down to clear names, small functions and consistent formatting. Start there.
This guide is part of our wider look at software development best practices.
| Item | Value |
|---|---|
| Function length Robert C. Martin warns against | 100 lines |
| Function length Martin says is hardly ever justified | 20 lines |
| Python PEP 8 indentation | Four spaces |
| Python PEP 8 line length | 79 characters |
These figures come from Microsoft Learn and Codacy. Style guidance can change, so check the sources before you rely on the figures.
What clean code actually means
Clean code is code that is easy to read, understand and maintain, and it still does its job. As Codacy paraphrases Martin Fowler: “Anybody can write code that a computer can understand. Good programmers write code that humans can understand.”
The term was made popular by Robert C. Martin, known as Uncle Bob, whose book Clean Code appeared in 2008, according to Codacy. It is not gospel, though. A Loyola Marymount University course note points out that the book dates from 2008, when Java was king, and recommends reading a critical review alongside it.
So treat the habits below as a starting point. Agree on them with your team rather than adopting any single author’s rules wholesale.
Name things so the code explains itself
Good naming does more work than any comment. The Microsoft guide contrasts a bare x with maximumLoginAttempts, and notes that a search and replace on a single-letter name becomes painful.
Martin puts it this way, as quoted by Codacy: “A name should tell you why it exists, what it does, and how it is used.”
Some practical naming habits, drawn from the LMU notes:
- Make names pronounceable, so write point rather than pnt.
- Use nouns for things and properties, and verbs for actions.
- Abbreviate only for tiny-scope variables or well-known acronyms.
- Stay consistent with capitalisation, and do not mix camel case and underscores within the same kind of identifier.
Keep functions small and focused
The single responsibility principle says a function should have a single purpose and do it well. Codacy illustrates this by splitting a combined payment-and-invoice routine into separate processPayment and generateInvoice functions.
On length, Martin is blunt, as quoted by the Microsoft guide: “Functions should not be 100 lines long. Functions should hardly ever be 20 lines long.” Treat that as a direction to aim for, not a hard limit.
Small functions also make nested logic easier to untangle. The same Codacy guide suggests replacing a tangle of chained conditions with a well-named helper such as isConditionMet.

Comments, formatting and repeated values
Comments should explain why, not what. The Fellow guide says comments “should tell us why things are happening”, and the Microsoft guide warns that comments go stale as code evolves because “describing what the code does is the responsibility of the code itself”.
For formatting, follow your language’s style guide. Codacy notes that Python’s PEP 8 recommends four spaces for indentation and lines limited to 79 characters. The Fellow guide gives a looser rule of around 80 characters per line, so side-by-side comparisons stay readable.
Repeated values deserve special care. The LMU notes advise avoiding hard-coded literals, or magic numbers, except for things like 0, 1, and sometimes 2. Use these habits instead:
- Give every important value a named constant.
- Keep each fact in a single place, so a change never needs edits in several files.
- Do not obsess over repetition in tests, where clarity can matter more than reuse.
Let tools and teammates catch what you miss
Automation handles the boring parts of style. If you are wondering what is a linter, it is a tool that flags style and quality problems, and the LMU notes advise using both an auto formatter such as Prettier and a linter.
People catch what tools cannot. Work through a code review checklist so reviewers check naming, structure and tests the same way every time.
The Fellow guide suggests a simple test of readability: ask a teammate to explain your code back to you. If they misread your intent, that is useful feedback about the code, not about them.
Tests protect all of this. The LMU notes say unit tests should be independent of each other, cover boundary cases such as zero and empty collections, and never hit an external system.
Frequently asked questions
What is the simplest way to start writing clean code?
Show answer
Begin with names. Rename vague variables and functions so each says what it holds or does, then split any function that does more than a single job.
Is the Clean Code book the final word?
Show answer
No. The LMU notes call the book reasonable in places but not gospel, and suggest reading other views such as a critical review.
How long should a function be?
Show answer
As short as it can be while staying clear. Martin says functions should hardly ever reach 20 lines, as quoted in the Microsoft guide.
Should I avoid comments completely?
Show answer
Not completely. Skip comments that restate the code, and keep those that explain intent or the reason behind a decision, as the Fellow guide recommends.



