Show Menu
Cheatography

Enablon Clean Code Cheat Sheet (DRAFT) by

Enablon Clean Code

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Principles

Single Respon­sib­ility
A class should have only one reason to change
Open / Close principle
Software entities (classes, modules, functions, etc.) should be open for extension but closed for modifi­cation
Liskov Substi­tution Principle
Subtypes must be substi­tutable for their base types
Interface Segreg­ation Principle
Clients should not be forced to depend on methods they do not use.
Dependency Inversion Principle
High-level modules should not depend on low-level modules. Both should depend on abstra­ctions. Abstra­ctions should not depend upon details. Details should depend upon abstra­ctions
Command Query Separation
A method should either be a command or a query
Do not Repeat Yourself (DRY)
Every piece of knowledge must have a single, unambi­guous, author­itative repres­ent­ation within a system

General

Boy Scout Rule
Leave the campground cleaner than you found it
Keep it Simple, Stupid (KISS)
Simpler is always better. Reduce complexity as much as possible
Positive Condit­ionals
Positive condit­ionals are easier to read than negative condit­ionals
Don’t Be Arbitrary

Design

Prefer Polymo­rphism To If/Else or Switch­/Case
Prefer compos­ition over inheri­tance
Prefer Dedicated Value Objects to Primitive Types
Instead of passing primitive types like strings and integers, use dedicated primitive types: e.g. Uri instead of string
Vertical Separation
Variables and methods should be defined close to where they are used
Encaps­ulate Condit­ionals
if (this.S­ho­uld­BeD­ele­ted­(ti­mer)) is preferable to if (timer.Ha­sEx­pired && !timer.Is­Rec­urrent)
(-) Method with Too Many Arguments
Prefer fewer arguments. Maybe functi­onality can be outsourced to a dedicated class that holds the inform­ation in fields
(-) Method with Out/Ref Arguments
Prevent usage. Return complex object holding all values, split into several methods
(-) Obscured Intent
Too dense algorithms that lose all expres­siv­eness
(-) Magic Numbers / Strings
Replace Magic Numbers and Strings with named constants to give them a meaningful name when meaning cannot be derived from the value itself
(-) Dead code / dead comment
Delete unused things. You can find them in your version control system