Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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.

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top