HACKER Q&A
📣 pizzaparty2

What is the maximum number of lines a function should have?


I'm specifically interested in C# and JavaScript if you require that distinction.


  👤 zadler Accepted Answer ✓
It doesn’t make sense to think about a maximum number of lines.

There may be a complex function of many variables which is not simplified by splitting into multiple functions.

Most often though, the code in a function has several logical parts which have their own temporary variables and are good candidates for splitting into separate functions.

Generally it makes sense to split code up so that it can be encapsulated at different levels. For example if you have a validation routine for a record, it may make sense to have different functions to test different properties, so that you can see which validations are being performed at a high level and then zoom in when you need to.

Encapsulation should be done to manage scope of temporary variables, and to make the code easier to read and to test. But there’s no hard and fast rule, really.