Minideas
← All topics

Clean code and refactoring

Write code that’s easy to read, then tidy it right away, so the next change is easy too.

written once×1read again and againoften by you, months later
1 / 10

Code is read far more than it’s written

You write a line once, but people read it again and again: teammates, reviewers, and you six months later. Clean code is code the next reader can understand quickly.

time →time to add a featurepays off heremessy codekept tidy
2 / 10

Why bother? Mess slows you down

In messy code, every new feature takes longer than the last. Code that’s kept tidy costs a little more at first, then stays quick to change.

input1. make it work42input2. make it right42sameresult
3 / 10

Make it work, then make it right

First get the code working. Then, while it’s still fresh in your head, tidy it without changing what it does: the same input still gives the same answer. That’s refactoring, and it takes minutes, not a separate project months later.

d = 7?what's d?daysUntilDue = 7says what it is// skip Sundays: the bank is closeda comment says why, not what
4 / 10

Names that say what they mean

A variable called d makes every reader guess; daysUntilDue doesn’t. A good name often makes a comment unnecessary. When you do write one, explain why the code does something, not what it does.

one giant functioncheck ordertake paymentsend receiptone job each
5 / 10

One job per piece

A giant function that checks the order, takes the payment and sends the receipt is hard to follow and risky to change. Split it into small pieces that each do one thing and are named for it, like the steps of a recipe.

ABCcopied 3 timesfixed one, missed twoone shared piecefix once, fixed everywhere
6 / 10

Say it once

If the same logic is copied into three places, a bug gets fixed in one and stays in the other two. Put it in one place and use it everywhere: the DRY principle, don’t repeat yourself. Only merge things that mean the same, not things that just look alike.

just in caseevery reader carries itwhat's needed nowadd more when it's real
7 / 10

Build what you need now

Extra options added “just in case” make code bigger, and every reader has to work through them, though most never get used. Build for today’s need and add more when it’s real. The principle has a name: YAGNI, you aren’t gonna need it.

small changerun testspasssavefailundorepeat, many times a day
8 / 10

Tidy in tiny steps, with tests

Rename something, or pull a chunk out into its own function, then run the automated tests. If they pass, save and take the next step. If anything fails, undo just that step. Many tiny steps are safer than one big leap.

AIwrittenin secondsstill read andchecked by you
9 / 10

When an AI writes the code

An AI assistant can write code in seconds, but a person still has to read it, understand it and answer for it. It copies the style of the code around it: tidy code gets tidy suggestions, messy code gets more mess. Ask it to make it work, then make it right, in small steps with tests, as you would yourself.

visit 1visit 2visit 3visit 4each time you pass by, fix one small thingno big rewrite needed
10 / 10

Leave it a little cleaner

Nobody writes a perfect first draft, so clean code is a habit, not a finish line. Each time you touch older code, fix one small thing. Like fixing a house one room at a time while you live in it, there’s no need to tear it down and rewrite.

Swipe the card, or use the arrow keys.

Go deeper

Each card stack gives you the idea. These open resources go into the detail.