Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good practice class line count [closed]

Tags:

I know that there is no right answer to this question, I'm just asking for your opinions.

I know that creating HUGE class files with thousand lines of code is not a good practice since it's hard to maintain and also it usually means that you should probably review your program logic.

In your opinion what is an average line count for a class let's say in Java (i don't know if the choice of language has anything to do with it but just in case...)

like image 544
Savvas Dalkitsis Avatar asked Jul 06 '09 13:07

Savvas Dalkitsis


People also ask

How many lines should a class have?

Some people recommend that 200 lines is a good limit for a class – not a method, or as few as 50-60 lines (in Ben Nadel's Object Calisthenics exercise)and that a class should consist of “less than 10” or “not more than 20” methods.

How many lines should a class be C#?

In C#, my rule of thumb for classes is anything over 500 lines is getting too long.

How much should be minimum line of code in method and class any coding standards?

In order to help keep methods easy to understand they should be no more than 20 lines of code. This does not include whitespace, closing braces, or comments. If a method gets much longer than 20 lines of code then it is either the algorithm is too convoluted or the method is trying too to do to much.

How many lines should a code file have?

Large files tend to do a lot of things and can make it hard following what's going. While there is not an objective maximum number of lines considered acceptable in a file, most people would agree it should not be in the thousands. Recommendations usually range from 100 to 500 lines.


1 Answers

Yes, I'd say it does have to do with the language, if only because some languages are more verbose than others.

In general, I use these rules of thumb:

  • < 300 lines: fine
  • 300 - 500 lines: reasonable
  • 500 - 1000 lines: maybe ok, but plan on refactoring
  • > 1000 lines: definitely refactor

Of course, it really depends more on the nature and complexity of the code than on LOC, but I've found these reasonable.

like image 76
sleske Avatar answered Oct 21 '22 16:10

sleske