Skip to content

Scalastyle proposed rules (Miscellaneous)

matthewfarwell edited this page Oct 21, 2011 · 10 revisions

This page contains proposed rules for Scalastyle. These rules are from Checkstyle, and do not necessarily apply to Scalastyle.

Miscellaneous

NewlineAtEndOfFile

Checks whether files end with a new line. Rationale: Any source files and text files in general should end with a newline character, especially when using SCM systems such as CVS. CVS will even print a warning when it encounters a file that doesn't end with a newline.

TodoComment

A check for TODO: comments. Actually it is a generic regular expression matcher on Java comments. To check for other patterns in Java comments, set property format.

Translation

A FileSetCheck that ensures the correct translation of code by checking property files for consistency regarding their keys. Two property files describing one and the same context are consistent if they contain the same keys. Consider the following properties file in the same directory: #messages.properties hello=Hello cancel=Cancel #messages_de.properties hell=Hallo ok=OK The Translation check will find the typo in the german hello key, the missing ok key in the default resource file and the missing cancel key in the german resource file: messages_de.properties: Key 'hello' missing. messages_de.properties: Key 'cancel' missing. messages.properties: Key 'hell' missing. messages.properties: Key 'ok' missing.

UncommentedMain

Checks for uncommented main() methods (debugging leftovers). Rationale: A main() method is often used for debug puposes. When debugging is finished, developers often forget to remove the method, which changes the API and increases the size of the resulting class/jar file. With the exception of the real program entry points, all main() methods should be removed/commented out of the sources.

UpperEll

Checks that long constants are defined with an upper ell. That is ' L' and not 'l'. This is in accordance to the Java Language Specification, Section 3.10.1. looks a lot like 1.

ArrayTypeStyle

Checks the style of array type definitions. Some like Java-style: public static void main(String[] args) and some like C-style: public static void main(String args[])

FinalParameters

Check that method/constructor/catch block parameters are final. Interface and abstract methods are not checked - the final keyword does not make sense for interface and abstract method parameters as there is no code that could modify the parameter. Rationale: Changing the value of parameters during the execution of the method's algorithm can be confusing and should be avoided. A great way to let the Java compiler prevent this coding style is to declare parameters final.

DescendantToken

Checks for restricted tokens beneath other tokens. WARNING: This is a very powerful and flexible check, but, at the same time, it is low level and very implementation dependent because its results depend on the grammar we use to build abstract syntax trees. Thus we recomend using other checks when they provide the desired funcionality. All in all, this check just works on the level of an abstract tree and knows nothing about language structures.

Indentation

Checks correct indentation of Java Code. The basic idea behind this is that while pretty printers are sometimes convienent for bulk reformats of legacy code, they often either aren't configurable enough or just can't anticipate how format should be done. Sometimes this is personal preference, other times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules are followed.

TrailingComment

The check to ensure that requires that comments be the only thing on a line. For the case of // comments that means that the only thing that should precede it is whitespace. It doesn't check comments if they do not end line, i.e. it accept the following: Thread.sleep( 10 <some comment here> ); Format property is intended to deal with the "} // while" example. Rationale: Steve McConnel in "Code Complete" suggests that endline comments are a bad practice. An end line comment would be one that is on the same line as actual code. For example: a = b + c; // Some insightful comment d = e / f; // Another comment for this line Quoting "Code Complete" for the justfication:

  • "The comments have to be aligned so that they do not interfere with the visual structure of the code. If you don't align them neatly, they'll make your listing look like it's been through a washing machine."
  • "Endline comments tend to be hard to format...It takes time to align them. Such time is not spent learning more about the code; it's dedicated solely to the tedious task of pressing the spacebar or tab key."
  • "Endline comments are also hard to maintain. If the code on any line containing an endline comment grows, it bumps the comment farther out, and all the other endline comments will have to bumped out to match. Styles that are hard to maintain aren't maintained...."
  • "Endline comments also tend to be cryptic. The right side of the line doesn't offer much room and the desire to keep the comment on one line means the comment must be short. Work then goes into making the line as short as possible instead of as clear as possible. The comment usually ends up as cryptic as possible...."
  • "A systemic problem with endline comments is that it's hard to write a meaningful comment for one line of code. Most endline comments just repeat the line of code, which hurts more than it helps."
His comments on being hard to maintain when the size of the line changes are even more important in the age of automated refactorings.

Regexp

A check that makes sure that a specified pattern exists, exists less than a set number of times, or does not exist in the file. This check combines all the functionality provided by RegexpHeader, GenericIllegalRegexp and RequiredRegexp, except supplying the regular expression from a file. It differs from them in that it works in multiline mode. It's regular expression can span multiple lines and it checks this against the whole file at once. The others work in singleline mode. Their single or multiple regular expressions can only span one line. They check each of these against each line in the file in turn. Note: Because of the different mode of operation there may be some changes in the regular expressions used to achieve a particular end. In multiline mode...

  • ^ means beginning of a line, as oposed to beginning of input.
  • For beginning of input use \A.
  • $ means end of a line, as oposed to end of input.
  • For end of input use \Z.
  • Each line in the file is terminated with a newline character.
Note: Not all regexp engines are created equal. Some provide extra functions that others do not and some elements of the syntax may vary. This check makes use of the java.util.regex package, please check its documentation for details of how to construct a regular expression to achive a particular goal. Note: When entering a regular expression as a parameter in the xml config file you must also take into account the xml rules. e.g. if you want to match a < symbol you need to enter &lt;. The regular expression should be entered on one line.

OuterTypeFilename

Checks that the outer type name and the file name match. For example, the class Foo must be in a file named Foo.java.