Functions in your code should be no longer than five lines

Arpit Bhayani

Arpit Bhayani

May 15, 2024 β€’ 1 min read


Functions in your code should be no longer than five lines … πŸ€¦β€β™‚οΈ

This has to be the stupidest practice I have seen many codebases follow. This just fills the codebase with a ton of tiny functions that do nothing important but call other abstract functions surrounded by some trivial logic.

Such a codebase is a nightmare to understand and make changes to. Just imagine taking 79 clicks to just navigate and reach the place of interest, a sheer waste of time and effort. After a few taps, you will even forget where did you begin and how did you end up here.

As a general rule of thumb, optimize the code for anyone to understand, extend, and modify today. Keep your functions large enough to be doing something substantial, but short enough to be reused at other places.

Remember, functions were introduced to programming languages to increase the reuse of business β€œlogic”.

a β†’ b β‰  ~a β†’ ~b.

⚑ I keep writing and sharing my practical experience and learnings every day, so if you resonate then follow along. I keep it no fluff.

youtube.com/c/ArpitBhayani

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses