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 Source Code Structure Objects VS Data Structures

Rigi​dity Use vertical format​ting to separate your Data structures exposes data and have no
code and different concepts, you should read behavior.
No change is trivial, every change in the
your code from top to bottom without So, procedural code makes it easy to add new
code add more twists and tangles.
"​jum​pin​g" over functions function without changing the existing data
Comp​lex​ity
Variables should be declared as close to struct​ures.
As above, no change is trivial and requires a
their usage as possible Objects expose behavior and hide data.
lot of research.
Instance variab​les should be declared at the Object Oriented code makes it easy to add
Frag​ility new classes without changing existing
top of the class
Changes breaking other parts of the code. functions
Put statics methods on top of the package
Avoid hybrids (half object and half data
Immo​bil​ity Similar and depend​ent functions should be
structure)
You cannot reuse part of the existing code close vertic​ally

Balance between vertical openness and The Law of Demeter : A class should not know
General Rules vertical density. Same rules apply for
about the innards of the objects it manipu​lates.
horizontal density Objects should not expose theirs internals.
Follow the Boy Scout Rule : Leave the code
cleaner than when you found it Do not align your code horizo​ntally Same as functions : they should do one thing

Follow the Principle of Least Surprise Use cons​istent indent​ation and they should be small

Follow Standard Conven​tio​ns, both langage Avoid and split Train Wrecks :
Naming Rules objec​tA.g​et​B().ge​tC(​).g​etD();
related and team related

Keep it simple stupid Use desc​rip​tive and inte​nti​on-​rev​eal​ing Keep the number of instance variables low, if
variable names your class have too many instance variable,
Don't repeat yourself
then it is probably doing more than one
Make mean​ingful distin​cti​ons
Be cons​ist​ent thing
Use pronou​nceable and sear​chable names
Do not override safeties
Avoid disinf​orm​ation and encoded names Error handling
Design Rules Avoid member prefixes or types inform​ation
Error handling is one thing, don't mix error
(Hungarian Notation)
Functions should descend only one level of handling and code
Avoid mental mapping
abstra​cti​on, and statements in a function Use Exce​pti​ons instead of returning error
should be at the same level of abstra​ction Replace Magic Numbers with Cons​tants
codes
Use depe​ndency inject​ion Write the try-ca​tch​-fi​nally statement first, it will
Functions
Keep your boun​daries clean help you structure your code
Func​tions should do one thing and they Don't return null, don't pass null either
Enca​psulate condit​ion​als, try to avoid
should do it well
negative condit​ionals Throw exceptions with context
Functions should be relatively small
Make logical depend​encies physical
Functions should have desc​rip​tives names Tests
Use poly​mor​phism instead of if / else or
switch / case Functions should have as few argume​nts as F.I.R.S.T : Fast, Indepe​ndent, Repeat​able,
possible (no more than 3 if possible) Self-V​ali​dating, Timely
Avoid hidden temporal couplings
Functions should have no side effects One assert per test
Keep config​urable data (ie: constants) at high
levels, they should be easy to change Use expl​anatory variab​les to explain your Keep your tests as clean as your production
Use Enums over constants intent / algorithm code, they should be easily readable
Don't use flag arguments Use a coverage tool
Avoid output arguments, they're misleading
Tests should be easy to run

By CosteMaxime Published 13th February, 2019. Sponsored by Readability-Score.com


cheatography.com/costemaxime/ Last updated 14th February, 2019. Measure your website readability!
Page 1 of 2. https://readability-score.com
Summary of 'Clean Code' By Robert C. Martin Cheat Sheet
by CosteMaxime via cheatography.com/75716/cs/18799/

TDD

3 Laws of Test Driven Develo​pme​nt, this should ensure that


you write your tests and your code simu​lta​neo​usly

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 info​rm, expl​ain, clar​ify, or warn the


reader

Commen​t-out code ?

DELETE IT

Avoid using more than one langage in a single source file (Html
comments, Javadoc for nonplublic code)

Avoid inappr​opriate Inform​ations (change history, license, ...)

Avoid misleading or noise comments

Don't be redundant ( i++; // increment i )

Closing brace comments (} // end of function )

Credits

From "​Clean Code" by Robert C. Martin

Inspired by this summary

By Coste Maxime

By CosteMaxime Published 13th February, 2019. Sponsored by Readability-Score.com


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

You might also like