You are on page 1of 2

Summary of 'Clean Code' By Robert C.

Martin Cheat Sheet


by CosteMaxime via cheatography.com/75716/cs/18799/

Hints that the code you're reading is a mess Design Rules (cont) Functions

Rigidity Keep config​urable data (ie: constants) at Functions should do one thing and they
No change is trivial, every change in the high levels, they should be easy to change should do it well
code add more twists and tangles. Use Enums over constants Functions should be relatively small

Complexity Functions should have descri​ptives names


Source Code Structure
As above, no change is trivial and Functions should have as few arguments
requires a lot of research. Use vertical formatting to separate your as possible (no more than 3 if possible)
code and different concepts, you should
Fragility Functions should have no side effects
read your code from top to bottom without "​‐
Changes breaking other parts of the Use explan​atory variables to explain your
jum​pin​g" over functions
code. intent / algorithm
Variables should be declared as close to
Don't use flag arguments
Immobility their usage as possible
Avoid output arguments, they're misleading
You cannot reuse part of the existing Instance variables should be declared at the
code top of the class
Objects VS Data Structures
Put statics methods on top of the package
General Rules Data structures exposes data and have no
Similar and dependent functions should be
behavior.
Follow the Boy Scout Rule : Leave the code close vertically
So, procedural code makes it easy to add
cleaner than when you found it
Balance between vertical openness and new function without changing the existing
Follow the Principle of Least Surprise vertical density . Same rules apply for data struct​ures.
Follow Standard Conven​tions , both langage horizontal density
Objects expose behavior and hide data.
related and team related Do not align your code horizo​ntally Object Oriented code makes it easy to add
Keep it simple stupid Use consistent indent​ation new classes without changing existing
Don't repeat yourself functions
Naming Rules Avoid hybrids (half object and half data
Be consistent
Use descri​ptive and intent​ion​-re​vealing structure)
Do not override safeties
variable names
Design Rules Make meaningful distin​ctions The Law of Demeter : A class should not
know about the innards of the objects it
Functions should descend only one level of Use pronou​nceable and searchable names
manipu​lates. Objects should not expose
abstra​ction, and statements in a function Avoid disinf​orm​ation and encoded names
theirs internals.
should be at the same level of abstra​ction
Avoid member prefixes or types inform​ation
Same as functions : they should do one
Use dependency injection (Hungarian Notation)
thing and they should be small
Keep your boundaries clean Avoid mental mapping
Avoid and split Train Wrecks : object​A.g
Encaps​ulate condit​ionals , try to avoid Replace Magic Numbers with Constants ​etB​().g​et​C().ge​tD();
negative condit​ionals
Keep the number of instance variables low,
Make logical depend​encies physical
if your class have too many instance
Use polymo​rphism instead of if / else or variable, then it is probably doing more than
switch / case one thing
Avoid hidden temporal couplings

By CosteMaxime Published 13th February, 2019. Sponsored by Readable.com


Last updated 14th February, 2019. Measure your website readability!
Page 1 of 2. https://readable.com

cheatography.com/costemaxime/
Summary of 'Clean Code' By Robert C. Martin Cheat Sheet
by CosteMaxime via cheatography.com/75716/cs/18799/

Error handling Comments (cont)

Error handling is one thing, don't mix error Commen​t-out code ?


handling and code DELETE IT
Use Exceptions instead of returning error
codes
Avoid using more than one langage in a
Write the try-ca​tch​-fi​nally statement first, it single source file (Html comments, Javadoc
will help you structure your code for nonplublic code)
Don't return null, don't pass null either Avoid inappr​opriate Inform​ations (change
Throw exceptions with context history, license, ...)
Avoid misleading or noise comments
Tests
Don't be redundant ( i++; // incremen
F.I.R.S.T : Fast, Indepe​ndent, Repeat​able, t i)
Self-V​ali​dating, Timely
Closing brace comments (} // end of
One assert per test function)
Keep your tests as clean as your production
code, they should be easily readable Credits
Use a coverage tool From "​Clean Code" by Robert C. Martin
Tests should be easy to run Inspired by this summary

TDD
By Coste Maxime
3 Laws of Test Driven Develo​pment, this
should ensure that you write your tests and
your code simult​ane​ously
You may not write production code until you
have written a failing unit test
You may not write more of a unit test than is
sufficient to fail, and not compiling count as
failing
You may not write production code that is
sufficient to pass the currently failing test

Comments

When to write a comment ?


Explain yourself in code, not in
comment. If it's not possible, take your
time to write a GOOD comment.

What makes up a Good comment ?


Use comments to inform , explain, clarify ,
or warn the reader

By CosteMaxime Published 13th February, 2019. Sponsored by Readable.com


Last updated 14th February, 2019. Measure your website readability!
Page 2 of 2. https://readable.com

cheatography.com/costemaxime/

You might also like