deckingraj
deckingraj

Reputation: 393

Is there a way to ignore comments and whitespaces for FileLength check in Checkstyle

I am new to checkstyle and have spent good enough time on something which i believe should be an easy kill. I want to put a check on the number of lines in my file, which i successfully did. However i am not able to configure checkstyle to ignore empty lines as well as Java Comments before throwing in an error about Size Violations.

Reading their documentation i am of the view that FileLength module doesn't have any property that can be used to ignore comments and whitespaces. Here is the link and the sample code http://checkstyle.sourceforge.net/config_sizes.html

<module name="FileLength">
  <property name="max" value="500"/>

The only other exposed property is fileExtensions. Can someone please suggest what is the best way to put a tap on fileSize as well as not to discourage developers from putting in detailed documents?

Upvotes: 1

Views: 1488

Answers (1)

barfuin
barfuin

Reputation: 17494

This is a good question. First off, there is no way to configure the FileLength check in the way that you need. It simply goes for the number of lines, including everything like a mandatory copyright statement header or whatnot.

So, what I do is this:

  • Put a FileLength check with a really large number of lines, like 1500, so that even a very well commented file should not legally reach that limit.
  • Use ExecutableStatementCount to limit methods to a certain number of statements. This would exclude comments and formatting which puts braces on lines of their own. In my experience, a good value here is 60. MethodLength can now be disabled.
  • Use MethodCount to limit the number of methods.

Now, by multiplying the values of MethodCount and ExecutableStatementCount, you get a limit like you described, which is a limit on the number of statements per class, instead of a limit on the number of lines.

The actual values will have to undergo some tuning, so be prepared to change them a few times during your project.

Upvotes: 1

Related Questions