DEV Community

William Cairns
William Cairns

Posted on

1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay