Clean Code PHP
- use meaningful names
- use searchable names (not numbers)
- avoid nesting deeple, return early
- avoid mental mapping (no $i)
- don’t add uneeded context ($carMake)
- typehint to avoid nulls (string $thing)
- use !== and ===
- don’t use flag arguments - have a differnet function instead
- avoid side effects - have one service which writes to a file - don’t use mutable data types
- don’t change globals
- don’t use Singletons
- Encapsulate conditions
- avoid negative conditionals
- avoid conditionals - use polymorphism instead
- avoid type checking - type hint instead
- remove dead code
- use getters and setters
- use private by default
- prefer composition over inheritance - have a class instead of extending (Employee has EmployeeTaxData)
- avoid fluent
- prefer final classes - use an interface
- Never more than one reason for a class to change - class should just do one thing.
- Open for extension - closed for modification
Chapter 3 Functions
- Smaller than that.
- 3 to 4 lines long!
- blocks within if,else and while should be one line long
- probably a function call
- Adds documentary value - function has descriptive name
- FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY.
- one level of indentation
you need another function if you can extract another function with a new name.
switch could be a factory
- Remember Ward’s principle: “You know you are working on clean code when each routine turns out to be pretty much what you expected.”
- avoid 3 or more arguments