skip to content

Clean Code & Refactoring

The craft of writing code other people can read and change: names, functions, comments, formatting, error handling, smells and refactoring. Interviewers care because most of your future work will be editing existing code rather than writing new code.

part ofSoftware design & architectureoverview, primer and where to startread it →
on this pageshow

questions

77 · 2 sections

In Robert Martin's Clean Code terminology, what is the difference between an "object" and a "data structure"?

level: juniorimportance: must knowfreq 62%
basics
~20 s

An object hides its data and exposes behaviour - you tell it to do something. A data structure exposes its data and has almost no behaviour - other code reads its fields and does the work.

open as a page

What is the difference between cyclomatic complexity and cognitive complexity as code metrics, and why was cognitive complexity introduced?

level: juniorimportance: must knowfreq 62%
basics
~20 s

Cyclomatic complexity counts the independent execution paths through code, which estimates how many tests you need. Cognitive complexity estimates how hard code is for a human to read: it penalises nesting and ignores structures people find easy.

open as a page

How do guard clauses and early returns reduce the cognitive load of a function, and when is the 'single exit point' rule still justified?

level: juniorimportance: must knowfreq 68%
basics
~20 s

A guard clause handles an invalid or special case immediately and returns, instead of wrapping the real work in an if. That flattens nesting, so the reader stops carrying conditions in their head and the main path stays at the left margin.

open as a page

In code review, why is a comment that explains WHY the code does something usually more valuable than a comment that restates WHAT the code does?

level: juniorimportance: must knowfreq 72%
basics
~20 s

The code already shows what it does; a reader can see that. It cannot show the reason, constraint, or bug that forced this approach. "Why" comments add information; "what" comments repeat it and can go stale.

open as a page

Why does Clean Code advise signalling failures with exceptions rather than returning error codes, and when might error codes still be the better choice?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Error codes force every caller to check the returned value right at the call site, mixing error checks into the happy path. Exceptions move failure handling out of the main flow, and they cannot be silently ignored, so the code reads cleaner and is safer.

open as a page

In software engineering, what is the "Boy Scout Rule" (also called the campsite rule), and what does following it look like in an ordinary day-to-day code change?

level: juniorimportance: must knowfreq 55%
basics
~20 s

Always leave code a little better than you found it. Whenever you touch a file, make one small safe improvement — a clearer name, a deleted unused line, an extracted helper — so quality slowly rises instead of decaying.

open as a page

In Michael Feathers' book "Working Effectively with Legacy Code", how is "legacy code" defined, and what is the "Legacy Code Dilemma" that follows from that definition?

level: juniorimportance: must knowfreq 60%
basics
~20 s

Feathers defines legacy code as code without tests, regardless of its age or quality. The dilemma: to change code safely you want tests first, but to make the code testable you usually have to change it first.

open as a page

What does it mean that refactoring is "behavior-preserving", and which kinds of code changes are therefore NOT refactorings?

level: juniorimportance: must knowfreq 82%
basics
~20 s

Refactoring changes how code is structured, never what it does. Callers see the same observable results before and after. Adding a feature, fixing a bug, or changing output is not refactoring — it is a behavior change, done separately.

open as a page

What is the Extract Function refactoring (also called Extract Method), what are its mechanical steps, and what signals tell you a fragment is worth extracting?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Take a chunk of code inside a function, move it into a new function whose name says what it does, and call that new function from the original spot. Behaviour stays identical; only the structure changes.

open as a page

Describe the red-green-refactor cycle of test-driven development, and explain exactly what work belongs in each of its three steps.

level: juniorimportance: must knowfreq 74%
basics
~20 s

Red: write one small failing test for behavior you want. Green: write the simplest code that makes it pass. Refactor: clean up the code and the test while all tests stay passing. Then repeat with the next small behavior.

open as a page