Skip to content

Scalastyle proposed rules (Coding)

matthewfarwell edited this page Oct 18, 2012 · 9 revisions

This page contains proposed rules for Scalastyle, for the category Coding.

Coding

EmptyStatement

Detects empty statements (standalone ;).

FinalLocalVariable

Checks that local variables that never have their values changed are declared val. The check can be configured to also check that unchanged parameters are declared val.

HiddenField

Checks that a local variable or a parameter does not shadow a field that is defined in the same class.

MissingSwitchDefault

Checks that pattern matches have "_" clause. Rationale: It's usually a good idea to introduce a default case in every pattern match statement. Even if the developer is sure that all currently possible cases are covered, this should be expressed in the default branch, e.g. by using an assertion. This way the code is protected aginst later changes, e.g. introduction of new types in an enumeration type.

RedundantThrows

Checks for redundant exceptions declared in throws clause such as duplicates, unchecked exceptions or subclasses of another declared exception.

NestedForDepth

Restricts nested for blocks to a specified depth (default = 1).

NestedIfDepth

Restricts nested if-else blocks to a specified depth (default = 1).

NestedTryDepth

Restricts nested try blocks to a specified depth (default = 1).

SuperClone

Checks that an overriding clone() method invokes super.clone(). Reference: Object.clone().

SuperFinalize

Checks that an overriding finalize() method invokes super.finalize(). Reference: Cleaning Up Unused Objects.

IllegalCatch

Catching java.lang.Exception, java.lang.Error or java.lang.RuntimeException is almost never acceptable. Rationale: Junior developers often simply catch Exception in an attempt to handle multiple exception classes. This unfortunately leads to code that inadvertantly catchs NPE, OutOfMemoryErrors, etc.

IllegalThrows

This check can be used to ensure that types are not declared to be thrown. Declaring to throw java.lang.Error or java.lang.RuntimeException is almost never acceptable.

PackageDeclaration

Ensure a class has a package declaration, and (optionally) whether the package name matches the directory name for the source file. Rationale: Classes that live in the null package cannot be imported. Many novice developers are not aware of this.

IllegalType

Checks that particular class are never used as types in variable declarations, return values or parameters. Includes a pattern check that by default disallows abstract classes. Rationale: Helps reduce coupling on concrete classes. In addition abstract classes should be thought of a convenience base class implementations of interfaces and as such are not types themselves.

DeclarationOrder

[MJF] TODO to be updated to fit in with Scala Style guide According to Code Conventions for the Java Programming Language , the parts of a class or interface declaration should appear in the following order:

  1. Class (static) variables. First the public class variables, then the protected, then package level (no access modifier), and then the private.
  2. Instance variables. First the public class variables, then the protected, then package level (no access modifier), and then the private.
  3. Constructors
  4. Methods

MultipleVariableDeclarations

[MJF] update for Scala style guide Checks that each variable declaration is in its own statement and on its own line. Rationale: the SUN Code conventions chapter 6.1 recommends that declarations should be one per line/statement.

UnnecessaryParentheses

Checks for the use of unnecessary parentheses.

OneStatementPerLine

Checks there is only one statement per line. The following line will be flagged as an error: x = 1; y = 2; // Two statements on a single line.

LazyValOverriding

Avoid null during initialization of overriding val: see https://github.com/paulp/scala-faq/wiki/Initialization-Order. override val (with no 'lazy') or override def when abstract definition is a val and we are not in an early initialization block.

Configuration

  • early-init: accept only early-initialization block
  • lazy val: accept only lazy val
  • both