81

NetBeans 7.4 beta is currently available for public download, and it introduces a weird warning rule by default:

Method length is 16 lines (10 allowed)

My question is: Is this an accepted code convention rule, that can be proven somehow, somewhere ? NetBeans support/devs say it's not a bug, but don't give a statement why they only allow 10 lines, and where exactly this rule has its origin.

Sliq
  • 15,937
  • 27
  • 110
  • 143
  • 25
    It's a crazy rule made even more absurd by the fact that the 10 lines includes whitespace. –  Oct 18 '13 at 22:41
  • 13
    Yes, let's all remove our empty lines...that will improve the code :) – Chris Laarman Oct 22 '13 at 13:40
  • 2
    Whitespaces are not included anymore. Update your NB installation to patch 1. https://netbeans.org/bugzilla/show_bug.cgi?id=237620 – Ben Jan 03 '14 at 20:06
  • 5
    As I understood: It is something to enforce us to plan our minds better during writing code. i.e if it has to be longer, so it may be divided. – SaidbakR Mar 04 '14 at 00:27

5 Answers5

131

You can change the maximum method/function length warning behavior in NetBeans options (it's under Tools->Options->Editor, in the Hints tab, under "Too Many Lines" section in the checkboxes list).

There you can set the maximum number of lines, how you would like to be warned, etc.

I believe that the rule was introduced by NetBeans developers because when working in teams, the automated tools that QAs use to "inspect" code flag long method declarations/functions bodies. Unfortunately, the use of automated tools by "code analysts" is on the rise, whilst their understanding of the reasons behind that are still limited. I do not say that your functions should be hundreds of lines long - that's just plain wrong, but a hard-coded number as a coding law - come on!

wilsonrufus
  • 449
  • 1
  • 5
  • 14
Milen
  • 1,510
  • 1
  • 10
  • 8
  • 6
    In all fairness, every metric I've seen says that code readability drops as function length increases. Code Complete 2 even goes so far as to say that maintainability starts to drop precipitously at about line 50. – cwallenpoole Dec 30 '13 at 13:46
  • 1
    Don´t forget to switch "Language" rollup to "PHP" first if you have installed multple coding languages in your NetBeans. – panticz May 27 '18 at 18:42
  • Joost Visser in *Building Maintainable Software* says 15 lines. But that is to be considered in context with the whole codebase. A few units longer than 15 lines are not cause for concern, but several 100-line units would be. – Alonso del Arte Jul 29 '19 at 04:19
25

The "10 lines rule" has to do with enforcing test-driven development. The theory is that any method that has more than ten lines can be better broken down into units that are testable. it holds up in theory, but in practice a warning like this is more annoying than helpful.

Matt
  • 6,993
  • 4
  • 29
  • 50
0

I think there is not a convention about that, and it's very hard to make small functions in particular working in big projects.

I feel that the problem in NetBeans (or the rule) is counting lines with just one bracket or documentation.

This article gives him opinion about write functions with 5-15 lines.

Community
  • 1
  • 1
Carlos Huchim
  • 102
  • 1
  • 11
0

I always disable this warning, as well as the warning about too many nested blocks. I understand the idea around not having large methods but a LOT of the time it's just not practical, and as someone else mentioned if you keep splitting your code into arbitrary functions just to appease the IDE you end up with spaghetti code jumping all over the place, refactoring becomes a huge problem later on as well.

Same as the line length limit warning, maybe a line 50 characters long made you scroll sideways in 1985, but today we have larger monitors (in color now as well!). I've seen people mutilate a line of code by shortening variable names so that it fits within the limit, turning a perfectly readable line of code into an indecipherable mess just so it fits within the limit.

Personally I think those three rules together have caused more garbage spaghetti code than helped create readable / testable code.

LordWabbit
  • 147
  • 5
-1

I think there is no such rule. I always thought a good convention would be no more lines of code in a class than one can read without scrolling. 10 lines seems not very much for me but in general it's for overview purposes and easier testing..

to4dy
  • 128
  • 1
  • 1
  • 10
  • 5
    To me, a 1-page class should definitely not be something to aspire too. It's easy to put rules to limit the number of lines, but you still need to put your code logic *somewhere*. There is also a limit on how many function/class jump-ins and jump-outs one's mind can hold together. – userfuser Apr 23 '14 at 11:54