Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many lines should a function have at most?

Tags:

coding-style

Is there a good coding technique that specifies how many lines a function should have ?

like image 1000
Karthik Ratnam Avatar asked Dec 01 '25 08:12

Karthik Ratnam


2 Answers

No. Lines of code is a pretty bad metric for just about anything. The exception is perhaps functions that have thousands and thousands of lines - you can be pretty sure those aren't well written.

There are however, good coding techniques that usually result in fewer lines of code per function. Things like DRY (Don't Repeat Yourself) and the Unix-philosophy ("Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface." from Wikipedia). In this case replace "programs" with "functions".

like image 126
Niki Yoshiuchi Avatar answered Dec 05 '25 04:12

Niki Yoshiuchi


I don't think it matters, who is to say that once a functions lengths passes a certain number of lines it breaks a rule.

In general just code clean functions easy to use and reuse.

like image 35
Andy Avatar answered Dec 05 '25 05:12

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!