Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's going on with CheckStyle's line length check?

I have CheckStyle set to check for lines over 80 characters in Eclipse, and I have a margin line set up in my editor at 80 characters. If I put my cursor at the end of a line of code in my editor, the co-ordinates read (for example) 1433, 77, indicating the 77th character column from the left - yet when I run CheckStyle over the same line it says the line is 88 characters long! There are no extraneous tabs or other whitespace characters at the end of the line, it's definitely 77 long. Is CheckStyle broken?

like image 848
Matt Moran Avatar asked Feb 13 '14 15:02

Matt Moran


2 Answers

From the Checkstyle documentation:

The calculation of the length of a line takes into account the number of expanded spaces for a tab character ('\t'). The default number of spaces is 8. To specify a different number of spaces, the user can set TreeWalker property tabWidth which applies to all Checks, including LineLength; or can set property tabWidth for LineLength alone.

http://checkstyle.sourceforge.net/config_sizes.html#LineLength

To do this in Eclipse:

  • Open Window->Preferences from the Menu.
  • Select Checkstyle.
  • Type TreeWalker into the search box under Known modules.
  • Select TreeWalker on the list on the right.
  • Click Open.
  • Change the tabWidth to 4.
  • Click OK, and OK again.
like image 62
Martin Ellis Avatar answered Oct 16 '22 05:10

Martin Ellis


Is CheckStyle broken?

Probably not.

I expect you/Eclipse and CheckStyle have a different idea of the width of a TAB character. It sounds you think it means 4 spaces, whereas CheckStyle thinks it means 8 characters (its default).

One way around this is to configure Eclipse to not use TAB characters in your source file, then re-indent your source files.

Another way is to make use that CheckStyle and Eclipse agree on the TAB width; e.g. see @MartinEllis's answer.

(I prefer the first approach because it means that my source code will look correctly indented, irrespective of the platform's default TAB width; i.e. Windows versus Linux/Unix. Hard TAB characters in source code are a bad idea.)

like image 40
Stephen C Avatar answered Oct 16 '22 05:10

Stephen C