DEV Community

William Cairns
William Cairns

Posted on

Clean Code : Functions

I think every developer should read the book Clean Code by Robert Martin. The book gives great guidance for writing high-quality, well-tested, and maintainable code.

An often quote piece of advice is to keep functions as small as possible. My recollection is that Clean Code suggested functions should not be more than 20 lines long. a 20 line function is a good basis to work from but, clean code gives guidance, it does not prescribe rules.

Oftimes the readability of code is decreased through having too many small functions. In these cases, another suggested rule should take preference: a method should do one thing, do only it and do it well.

An area I often make a long function is in simple PHP scripts. A PHP script is usually readable by scanning from top to bottom. Breaking it into functions would make it harder to understand.

Take guidance as a suggested best practice and evaluate the current scenario before blindly following it.

Top comments (0)