When should you break functions into smaller parts?
When should you break functions into smaller parts?
When should you put a part of a bigger function into a smaller function? So for example lets say you have a bigger function with multiple parts.At what point should you say: hmmm, this function is too big, I am going to create a smaller sub-function and put outside of the function, in the same file, but below the same function? Well one example of this is comments.
We were learning how to write comments today with a colleague. And he showed me that if a section of code within an existing function body has reached so much complexity that it is taking multi-line comments to explain it, it is definitely a time to put the code block into a separate function of its own.
There is an added perk of this: it reduces the cognitive load of the next software engineer to read your codebase whether that's you or a colleague.
All of this knowledge is courtesy of an amazing new mentor I am working with. He is so incredible and I am so indebted to him; thank you so much.
Comments
Post a Comment