My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Long functions without reusable parts

Mark's photo
Mark
·Sep 13, 2018

Split it

72%

Keep it together

5%

I like turtles

23%

39 votes · Closed

You have a long function (like 100-300 lines) that describes a sequence of steps that only make sense together.

What do you do?

  • Add some comments to show what the steps are, but keep it in the same function. No point introducing extra indirection and jumping around the code. If a part is ever reused, it can still be split at that time.
  • Split it into multiple functions. Methods should stay short so as to give a faster overview. Who knows, maybe it'll be re-used one day in the future.

Bonus question: if the function is a method and you're splitting it, do you just share data through instance fields, or pass everything as arguments?

EDIT: I'm thinking about compiled languages with the possibility to force the compiler to inline these methods, so no performance cost. But performance considerations may be useful for other readers.