How many lines of code should a function be?
14 Answers. Functions should normally be short, between 5-15 lines is my personal “rule of thumb” when coding in Java or C#. This is a good size for several reasons: It fits easily on your screen without scrolling.
How long should lines of code be?
80 to 100 characters
Ideally, one line of code is a unit element that means or performs something specific – a part of a sentence if you will. It is generally agreed that the ideal length for a line of code is from 80 to 100 characters.
When do you break up a long line of code?
However, if the case of a long line I have to break up, I use this: Operators at the end of each line instead of the beginning of subsequent lines, to very clearly indicate visually that the line is incomplete Line break on first line before conditional element — this helps keep code “shape” consistent.
It is only a helpful indicator that can potentially point to the real issue. There’s no specific number. If you have to justify with some number to lawyers or something, figure out the maximum number of lines that fit on a typical development editor window at your shop, and use that.
What happens when you get over 200 lines of code?
However, at some point beyond 200 lines you cross into a danger zone where code quality and understandability will fall apart: code that can’t be tested and can’t be changed safely.
What is the ideal length of a function?
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.
Is the number of lines of code a good metric?
But ultimately, lines of code are a bad metric for pretty much anything. It is only a helpful indicator that can potentially point to the real issue. There’s no specific number.
Which is the longest procedure in the reviewed code?
The reviewed code has several procedures that are many lines of code – the longest is almost 600 lines. A couple of problems with this that I’ve thought of are maintainability and readability. The trick is that I need to justify to a layperson why this is a bad practice and if possible back it up with a well regarded and current reference book.
Is there a maximum length for a function?
If the answer to that question isn’t yes, the function is probably not a good one anyway, regardless of length. Relevant specifically to the maximum length, a quote from Code Complete, generally considered to be one of the best books on the subject of coding practices: