Show Menu

Clean Code Cheat Sheet by

Design Rules

SRP single respon­sib­ility principe
OCP open closed principe
LSP Derived class should be substi­tutable for the base class.
Layer of abstra­ction

Sandi Metz's Rules

100 Class can be no longer than 100 lines of code
5 Method can be no longer than 5 lines of code
4 Method should have no more than 4 parameters
only 1 Controll should instan­tiate only 1 object

Basic Rules

meaningful name for class, method, variable and constant
avoid using suffix for service name
remove the unused method and variable
remove the unnece­ssary variable and constant
prefer to use DateTi­meI­mmu­table for handling date
return value directly and earlier, if possible
short method, and just do one thing
set the lowest visibility to class, constant, variable, method
docblock must bring additional inform­ation
declare always the return value for method
avoid to use @template annota­tion, prefer to use $this-­>render
prefer to use $this-­>re­nde­r('­@Ap­pBu­ndl­e/i­nde­x.h­tml­,tw­ig');
define route in routin­g.yml file
define the Permission via @IsGra­nted(), @Secur­ity­("is­_gr­ant­ed(­)")
prefer to use @Cache­(sm­axa­ge=­"­300­") for caching
DRY: do NOT repeat yoursefl
self-d­ocu­menting code


T: terse, simple as possible
E: expres­siv­e(s­elf­-do­cum­ented)
D: done one thing

Symfony Specific



watch for side effects

DTO/DAO Object

validate the property with constraint annotation
always final class definition


Read operation via Repository
Write operation via Entity­Manager
Remove default value from doctrine annotation in Entity (Example: default values like type="s­­tr­i­n­g", length­­=255, nullab­­le­=­f­alse)
define public setter­/getter if really necessary


Remove the auto-g­ene­rated comments
Do not handle invalid platform, if MySQL is assumed due to full control of enviro­nment


Entity, ValueO­bject, DTO, DAO will not be mocked
Use Alice & faker for complex object
validation should not be mocked, prefer to use Valida­tio­nBu­ilder


Infr­ast­ruc­ture consists of validator, EventL­ist­ener, DBAL EnumType
Busi­nes­sPr­ocess consists of essential operations in business flows
Symfony classic direct­ories: Contro­ller, Command, Entity, Form




No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          FREQUENTLY USED DX CODES Cheat Sheet
          C Reference Cheat Sheet
          Java + OOP concept Cheat Sheet

          More Cheat Sheets by vikbert

          Onion Architecture + Symfony Cheat Sheet