Good practice class line count [closed]

2019-02-03 08:34发布

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...)

标签: code-size
12条回答
走好不送
2楼-- · 2019-02-03 09:03

I focus on methods and (try to) keep them below 20 lines of code. Class length is in general dictated by the single responsibility principle. But I believe that this is no absolute measure because it depends on the level of abstraction, hence somewhere between 300 and 500 lines I start looking over the code for a new responsibility or abstraction to extract.

查看更多
倾城 Initia
3楼-- · 2019-02-03 09:03

Small enough to do only the task it is charged with.

Large enough to do only the task it is charged with.

No more, no less.

查看更多
冷血范
4楼-- · 2019-02-03 09:06

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.

查看更多
别忘想泡老子
5楼-- · 2019-02-03 09:06

Lines of code is much more about verbosity than any other thing. In the project I'm currently working we have some files with over 1000 LOC. But, if you strip the comments, it will probably remain about 300 or even less. If you change declarations like

int someInt;
int someOtherInt;

to one line, the file will be even shorter.

However, if you're not verbose and you still have a big file, you'll probably need to think about refactoring.

查看更多
小情绪 Triste *
6楼-- · 2019-02-03 09:10

From Eric Raymond's "The Art Of Unix Programming"

In nonmathematical terms, Hatton's empirical results imply a sweet spot between 200 and 400 logical lines of code that minimizes probable defect density, all other factors (such as programmer skill) being equal. This size is independent of the language being used — an observation which strongly reinforces the advice given elsewhere in this book to program with the most powerful languages and tools you can. Beware of taking these numbers too literally however. Methods for counting lines of code vary considerably according to what the analyst considers a logical line, and other biases (such as whether comments are stripped). Hatton himself suggests as a rule of thumb a 2x conversion between logical and physical lines, suggesting an optimal range of 400–800 physical lines.

Taken from here

查看更多
孤傲高冷的网名
7楼-- · 2019-02-03 09:12

Better to measure something like cyclomatic complexity and use that as a gauge. You could even stick it in your build script/ant file/etc.

It's too easy, even with a standardized code format, for lines of code to be disconnected from the real complexity of the class.

Edit: See this question for a list of cyclomatic complexity tools.

查看更多
登录 后发表回答