-
Notifications
You must be signed in to change notification settings - Fork 3
Scalastyle proposed rules (Class Design)
This page contains proposed rules for Scalastyle. These rules are from Checkstyle, and do not necessarily apply to Scalastyle.
Checks visibility of class members. Only static final members may be public; other class members must be private unless property protectedAllowed or packageAllowed is set. Public members are not flagged if the name matches the public member regular expression (contains "^serialVersionUID$" by default). Note: Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default pattern to allow CMP for EJB 1.1 with the default settings. With EJB 2.0 it is not longer necessary to have public access for persistent fields, hence the default has been changed. Rationale: Enforce encapsulation.
Checks that a class which has only private constructors is declared as final.
Implements Bloch, Effective Java, Item 17 - Use Interfaces only to define types. According to Bloch, an interface should describe a type. It is therefore inappropriate to define an interface that does not contain any methods but only constants. The Standard class javax.swing.SwingConstants is an example of a class that would be flagged by this check. The check can be configured to also disallow marker interfaces like java.io.Serializable, that do not contain methods or constants at all.
Make sure that utility classes (classes that contain only static methods or fields in their API) do not have a public constructor. Rationale: Instantiating utility classes does not make sense. Hence the constructors should either be private or (if you want to allow subclassing) protected. A common mistake is forgetting to hide the default constructor. If you make the constructor protected you may want to consider the following constructor implementation technique to disallow instantiating subclasses:
Checks that classes are designed for extension. More specifically, it enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses. The exact rule is that nonprivate, nonstatic methods of classes that can be subclassed must either be
- abstract or
- final or
- have an empty implementation
Ensures that exceptions (defined as any class name conforming to some regular expression) are immutable. That is, have only final fields. The current algorithm is very simple it checks that all members of exception are final. User can still mutates an exception's instance (e.g. Throwable has setStackTrace(StackTraceElement[] stackTrace) method which changes stack trace). But, at least, all information provided by this exception type is unchangable. Rationale: Exception instances should represent an error condition. Having non final fields not only allows the state to be modified by accident and therefore mask the original condition but also allows developers to accidentally forget to initialise state thereby leading to code catching the exception to draw incorrect conclusions based on the state.
Restricts throws statements to a specified count (default = 1). Rationale: Exceptions form part of a methods interface. Declaring a method to throw too many differently rooted exceptions makes exception handling onerous and leads to poor programming practices such as catch (Exception). This check forces developers to put exceptions into a heirachy such that in the simplest case, only one type of exception need be checked for by a caller but allows any sub-classes to be caught specifically if necessary.
Check nested (internal) classes/interfaces are declared at the bottom of the class after all method and field declarations.