Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily โ€“ by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

  1. Keep configurable data at high levels.
  2. Prefer polymorphism to if/else or switch/case.
  3. Separate multi-threading code.
  4. Prevent over-configurability.
  5. Use dependency injection.
  6. Follow Law of Demeter. A class should know only its direct dependencies.

Understandability tips

  1. Be consistent. If you do something a certain way, do all similar things in the same way.
  2. Use explanatory variables.
  3. Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.
  4. Prefer dedicated value objects to primitive type.
  5. Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class.
  6. Avoid negative conditionals.

Names rules

  1. Choose descriptive and unambiguous names.
  2. Make meaningful distinction.
  3. Use pronounceable names.
  4. Use searchable names.
  5. Replace magic numbers with named constants.
  6. Avoid encodings. Don't append prefixes or type information.

Functions rules

  1. Small.
  2. Do one thing.
  3. Use descriptive names.
  4. Prefer fewer arguments.
  5. Have no side effects.
  6. Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.

Comments rules

  1. Always try to explain yourself in code.
  2. Don't be redundant.
  3. Don't add obvious noise.
  4. Don't use closing brace comments.
  5. Don't comment out code. Just remove.
  6. Use as explanation of intent.
  7. Use as clarification of code.
  8. Use as warning of consequences.

Source code structure

  1. Separate concepts vertically.
  2. Related code should appear vertically dense.
  3. Declare variables close to their usage.
  4. Dependent functions should be close.
  5. Similar functions should be close.
  6. Place functions in the downward direction.
  7. Keep lines short.
  8. Don't use horizontal alignment.
  9. Use white space to associate related things and disassociate weakly related.
  10. Don't break indentation.

Objects and data structures

  1. Hide internal structure.
  2. Prefer data structures.
  3. Avoid hybrids structures (half object and half data).
  4. Should be small.
  5. Do one thing.
  6. Small number of instance variables.
  7. Base class should know nothing about their derivatives.
  8. Better to have many functions than to pass some code into a function to select a behavior.
  9. Prefer non-static methods to static methods.

Tests

  1. One assert per test.
  2. Readable.
  3. Fast.
  4. Independent.
  5. Repeatable.

Code smells

  1. Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes.
  2. Fragility. The software breaks in many places due to a single change.
  3. Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort.
  4. Needless Complexity.
  5. Needless Repetition.
  6. Opacity. The code is hard to understand.
@abelosorio

This comment has been minimized.

Copy link

abelosorio commented Jul 23, 2018

Great job! I've read Clean Code and this summary is very helpful and a quick reference do-not-forget list.

๐Ÿ‘

@hopeogbons

This comment has been minimized.

Copy link

hopeogbons commented Sep 7, 2018

Good read, in light speed! ๐Ÿ‘

@Sasso0101

This comment has been minimized.

Copy link

Sasso0101 commented Sep 10, 2018

Thanks, really good summary of the book ๐Ÿ‘

@johnny-de-mavenn

This comment has been minimized.

Copy link

johnny-de-mavenn commented Sep 20, 2018

Thank you. Very helpful summary.

@tbrunel

This comment has been minimized.

Copy link

tbrunel commented Oct 7, 2018

Good summary ! Thanks.

@Hugo-Hache

This comment has been minimized.

Copy link

Hugo-Hache commented Oct 26, 2018

Really useful, thanks! ๐Ÿ‘

@Indrani123

This comment has been minimized.

Copy link

Indrani123 commented Nov 27, 2018

good information for clean code

@hdrdavies

This comment has been minimized.

Copy link

hdrdavies commented Nov 28, 2018

๐Ÿ‘

@olx-thessel

This comment has been minimized.

Copy link

olx-thessel commented Dec 7, 2018

Good bless you <3

@OscarBastardo

This comment has been minimized.

Copy link

OscarBastardo commented Dec 27, 2018

Good stuff thank you ๐Ÿ‘

@ivanrododendro

This comment has been minimized.

Copy link

ivanrododendro commented Jan 14, 2019

Useful.

I did a cheat sheet, it's quite ugly but it's a start :)
clean_code_summary_cheat_sheet

@miguelcarrasco94

This comment has been minimized.

Copy link

miguelcarrasco94 commented Jan 16, 2019

Thanks, ccode is an excellent book!

@robert197

This comment has been minimized.

Copy link

robert197 commented Jan 29, 2019

Thank you for this summary. ;)

@CarlHybird

This comment has been minimized.

Copy link

CarlHybird commented Feb 11, 2019

Hmm, this sounds different from how various other people on the net have interpreted the book, which is

  1. Never even learn what the comment character is in your language, as comments are evil.
  2. Bugfixes are documented only in github, as no other source control system has ever existed, and everywhere always has perfect internet.
  3. Function parameters are documented only in the unit tests. There are no unit tests.
  4. To remove three explanatory comments, you should simplify a 30 line function into a series of three classes spread over five directories, that requires cmakefiles.txt to be doubled in size...
    I may exaggerate a little...
    Thanks for the useful summary!
@jatezzz

This comment has been minimized.

Copy link

jatezzz commented Feb 17, 2019

Thanks for the summary

@yehudalev

This comment has been minimized.

Copy link

yehudalev commented Mar 18, 2019

Thanks! Very helpful!

@grnde

This comment has been minimized.

Copy link

grnde commented Apr 4, 2019

Great Job! Thanks for this summary.

@baxterj

This comment has been minimized.

Copy link

baxterj commented Apr 8, 2019

Great summary! Our designer decided to produce some posters based on your words. You can get them here: https://kingadesign.com/clean-code-poster-free-download?fbclid=IwAR2p9y7Rf5gyH586sXYxp2dgoA3kWvRlLO5jfgeHlj77k1Y94qM1grjdW84

@j611062000

This comment has been minimized.

Copy link

j611062000 commented Apr 10, 2019

Thanks for your summary!

@dwirandyh

This comment has been minimized.

Copy link

dwirandyh commented Apr 15, 2019

Thank you very helpful

@voku

This comment has been minimized.

Copy link

voku commented Jun 13, 2019

Good summary

@hoanghn1

This comment has been minimized.

Copy link

hoanghn1 commented Jun 20, 2019

I don't know why this summary is a bit different than the "Clean code" book that I've read. I don't see the "Understandability tips" chapter, the "Objects and data structures" is different and I don't see the "Error Handling" chapter, "Boundaries" chapter, "Class" chapter, "System", "Emergence", "Concurrency", "Successive Refinement", "JUnit Internals", "Refactoring" chapters. Does anyone know the answer?

@moderatemisbehaviour

This comment has been minimized.

Copy link

moderatemisbehaviour commented Jul 1, 2019

I created a linkable static site of these clean code rules with full explanations if anyone is interested :)

https://moderatemisbehaviour.github.io/clean-code-smells-and-heuristics/

@scrpatlolla

This comment has been minimized.

Copy link

scrpatlolla commented Aug 3, 2019

Thank you! Worth it.

@urkuder

This comment has been minimized.

Copy link

urkuder commented Aug 20, 2019

Great Summary,Thanks!

@vuongnt

This comment has been minimized.

Copy link

vuongnt commented Oct 9, 2019

Great Summary,Thanks!

@chanhosuh

This comment has been minimized.

Copy link

chanhosuh commented Oct 13, 2019

I would change "one assert per test" to "one concept per test". Uncle Bob makes a fairly big deal about how the former is a misconception.

@ridaamirini

This comment has been minimized.

Copy link

ridaamirini commented Oct 26, 2019

Thank you for the summary ๐Ÿ”ฅI was searching for something like that ๐Ÿ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You canโ€™t perform that action at this time.