How long a function should be?
Wondering if that function you just finished implementing is too long? Or maybe it looks so short that you think you’d better get rid of it?
Whenever that happens in future, I’ll try to follow this guideline from my colleague Martin Fowler’s Bliki named “FunctionLength”
If you have to spend effort into looking at a fragment of code to figure out what it’s doing, then you should extract it into a function and name the function after that “what”. That way when you read it again, the purpose of the function leaps right out at you, and most of the time you won’t need to care about how the function fulfills its purpose - which is the body of the function.
What’s your guideline on function length? Share with me on twitter: @cengizIO
Continue reading →