Optimal number of lines of code of functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Optimal number of lines of code of functions

My first company had the coding style document forcing to have maximum of 20 lines of code in any function. I've seen huge functions taking more than 600 lines, which obviously isn't fine in terms of code readabilty. What's your policy on that and which is the optimal number?

14th May 2018, 11:50 AM
vardanator
vardanator - avatar
9 Answers
+ 13
My company had a coding standard of 100 lines maximum. In practice, most were in the 10 to 40 range. The more important thing is the purpose of the function. A well organized code is much more important than how big a particular function is. A 100 lines that make sense together is much better than 10 that have no business being together.
14th May 2018, 3:06 PM
John Wells
John Wells - avatar
+ 6
sometimes to keep the related logic at one place, bigger functions seems so compelling. but it does creates a problem in readability. i guess most of the times limiting factor in function sizes would be the cyclomatic complexity ( non - linear flows like if, else, switch, case, loops etc). stackoverflow suggests keeping it less than 10 is considered Ok for any function and greater than 10 considered complex.
14th May 2018, 3:32 PM
Morpheus
Morpheus - avatar
+ 5
Jan Markus 🤔 what if we work on the same code and our screens differ in size? 😂
14th May 2018, 12:30 PM
vardanator
vardanator - avatar
+ 3
I try to stick to one single functionality and make it readable. No hard limit on lines.
14th May 2018, 11:44 PM
Emerson Prado
Emerson Prado - avatar
+ 3
As long as you can write really UNIT test on your function it is in good shape ;)
19th May 2018, 3:37 PM
Lusss
Lusss - avatar
+ 1
use like scripts
14th May 2018, 1:09 PM
Charan Leo25
Charan Leo25 - avatar
+ 1
Charan😭 That would be how to split a large function into smaller units.
14th May 2018, 3:40 PM
John Wells
John Wells - avatar
- 3
John Wells vardanator can we make small small function as packet and use it make larger program when we need large function we can use that
14th May 2018, 3:36 PM
Charan Leo25
Charan Leo25 - avatar
- 3
asking ,can we do that?is it good?
14th May 2018, 3:42 PM
Charan Leo25
Charan Leo25 - avatar