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 pageshowhide
explore
- Writing Clean Code42 questions
- Meaningful Naming6 questions
- Small Functions6 questions
- Comments and Documentation6 questions
- Formatting and Structure6 questions
- Cognitive Complexity & Readability6 questions
- Clean Error Handling6 questions
- Boundaries, Objects & Data Structures6 questions
- Smells & Refactoring35 questions
- Code Smells6 questions
- Refactoring Discipline6 questions
- Refactoring Catalog Techniques6 questions
- Technical Debt Management5 questions
- Boy Scout Rule & Incremental Improvement6 questions
- Working with Legacy Code6 questions
- Backend Developerroleanchors this topic
- Full Stack Developerroleanchors this topic
- Java Backend Developerroleanchors this topic
- Kotlin Backend Developerroleanchors this topic
- Software Design & Architectureskillanchors this topic
- Forward Deployed Engineerrole
- Game Developerrole
- Server-Side Game Developerrole
- Software Architectrole
questions
77 · 2 sectionsIn Robert Martin's Clean Code terminology, what is the difference between an "object" and a "data structure"?
basics
~20 sAn 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.
What is the difference between cyclomatic complexity and cognitive complexity as code metrics, and why was cognitive complexity introduced?
basics
~20 sCyclomatic 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.
How do guard clauses and early returns reduce the cognitive load of a function, and when is the 'single exit point' rule still justified?
basics
~20 sA 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.
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?
basics
~20 sThe 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.
Why does Clean Code advise signalling failures with exceptions rather than returning error codes, and when might error codes still be the better choice?
basics
~20 sError 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.
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?
basics
~20 sAlways 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.
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?
basics
~20 sFeathers 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.
What does it mean that refactoring is "behavior-preserving", and which kinds of code changes are therefore NOT refactorings?
basics
~20 sRefactoring 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.
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?
basics
~20 sTake 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.
Describe the red-green-refactor cycle of test-driven development, and explain exactly what work belongs in each of its three steps.
basics
~20 sRed: 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.