Skip to content

Latest commit

 

History

History
37 lines (22 loc) · 1.56 KB

functions.md

File metadata and controls

37 lines (22 loc) · 1.56 KB

Back to technical skills

Functions

Functions should be small

Argument objects

When a function seems to need more than two or three arguments, it is likely that some of those arguments can be wrapped into a class of their own. In such cases, group them within a class:

// Bad
function makeCircle(double $x, double $y, double $radius): Circle;

// Good
function makeCircle(Point $center, double $radius): Circle;

Have no side effects

Side effects are lies. Your function promises to do one thing, but it also does other hidden things. Sometimes it will make unexpected changes to the variables of its own class or do unexpected behaviour. Sometimes it will make them to the parameters passed into the function or to system globals. In either case they are devious and damaging mistrusts that often result in strange temporal couplings and order dependencies, or hide some business logic you are missing.

Command/Query separation

Functions should either do something or answer something, but not both.

References

Books

  • Clean Code (Amazon) [English] by Robert C. Martin

Other links