Coding Practice: Function Size

Coding Practice: What is the maximum number of lines of code for a function?
In general, a function should fit on your screen both length and width wise without requiring you to scroll, which is about 50 lines.  A function is the right size if it only has a single responsibility, which tends to result in short functions.

Quote:
“The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. Functions should not be 100 lines long. Functions   should hardly ever be 20 lines long.”
–            Robert Martin

Application:
You can test a function for single responsibility in a function by making sure its name indicates exactly what the function does.  If the name of the function is too long, then   the function is probably doing too much.  Go ahead and break the function into multiple functions each with their own area of responsibility.

References:
Clean   Code: A Handbook of Agile Software Craftsmanship

About Chris VanHoose

Principal Software Architect at CT Lien Solutions
This entry was posted in Software Architecture and tagged , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.