Coding with style: The Scalastyle style checker
Scalastyle is a static code analysis tool that helps programmers write Scala code according to the coding standards their project.
It is heavily influenced by Checkstyle, PMD and FindBugs for Java. These tools are used by java developers everywhere, and Scalastyle will help Scala gain more acceptance within companies.
Scalastyle can check many aspects of your code. It has as goals:
- to check the style of your code according to house style (usage of tabs, correct whitespace usage),
- coding conventions (too many parameters to a method, usage of disallowed libraries, usage of return)
- and code which exhibits bug patterns (equals and hashCode not implemented in the same class).
Currently, scalastyle works as standalone, and there is an Eclipse plugin, and plugins for sbt and maven are planned.
In this talk, we discuss why we're creating Scalastyle, what it currently does (the rules that are checked), and how it does it (using the wonderful Scalariform), along with the current state of third part integration (Eclipse, Maven).
1. The style checker for Scala
Scala Days 2012
Matthew Farwell @matthewfarwell
@scalastyle
2. Matthew Farwell
Java technical expert @ SQLI Suisse in Lausanne
> 20 years development in various languages from Fortran
to Java/Scala
Contributor to various OSS projects, scala-ide, Junit, and
of course Scalastyle
Enjoying Scala Days 2012!
4. Why an automated style checker?
Douglas Crockford: "All languages should have a lint".
Problem: too many people
Problem: too many lines of code
Solution: code reviews?
Solution: style guides?
Solution: unit tests?
Solution: integration tests?
Solution: fewer lines of code?
Solution: Scalastyle?
5. What is Scalastyle?
Scalastyle is a style checker, similar to
Checkstyle, including bits of Findbugs & PMD.
There is a continuum of checks that we need to make on
our code, but which shouldn’t belong in the compiler.
Projects are different, so everything needs to be
configurable.
They are just guidelines, they aren’t rules. But they can
rules be for your project.
6. What does it check? Format rules
Heavily inspired by Checkstyle
Format rules:
Files do not contain tabs
Files contain correct headers, license and disclaimer etc.
General format rules:
def foo[T](t: T) = fn(t) + 3
not
def foo [ T ] (t : T ) = fn(t)+3
7. What does it check? Style rules
Style rules (house style, things which make the code
harder to understand or more complex):
Methods do not have more than x parameters
Number of classes in a file
Null not used
Return not used
Use of structural types
8. What does it check? Known sources of
bugs
Known sources of bugs, things to avoid:
A class/object implements equals() but not hashCode() or
vice versa
A class/object implements a covariant equals(x) but not
equals(Any)
Missing case default
Structural types
9. What can’t it do?
Anything which you don’t specify in the scala file.
Inferred types
Mismatches between types
Implicit conversions
Multi-file checks. Scalastyle only does one file at a time
11. How
The excellent Scalariform. This gives us an AST and/or list
of tokens.
Lines: (list of lines)
We don’t use compiler plugins for these (main) reasons:
Speed (we can check scala compiler & library (1500 files)
in ~80s)
Comments (they get swallowed by the compiler)
for comprehensions get desugared
The Compiler Plugin API isn’t fully nailed down.
12. Rules which are currently implemented
FileLengthChecker - Check the number of lines in a file
FileLineLengthChecker - Check the number of characters in a line
FileTabChecker - Check that there are no tabs in a file
HeaderMatchesChecker - Check the first lines of each file matches the text
WhitespaceEndOfLineChecker - Check that there is no trailing whitespace at
the end of lines
ClassNamesChecker - Check that class names match a regular expression
CovariantEqualsChecker - Check that classes and objects do not define
equals without overriding equals(java.lang.Object).
EqualsHashCodeChecker - Check that if a class implements either equals or
hashCode, it should implement the other
IllegalImportsChecker - Check that a class does not import certain classes
13. Rules which are currently implemented
IllegalImportsChecker - Check that a class does not import certain classes
MagicNumberChecker - Checks for use of magic numbers
NoCloneChecker - Check that classes and objects do not define the clone() method
NoFinalizeChecker - Check that classes and objects do not define the finalize() method
NoWhitespaceAfterLeftBracketChecker - No whitespace after left bracket [
NoWhitespaceBeforeLeftBracketChecker - No whitespace before left bracket [
NullChecker - Check that null is not used
ObjectNamesChecker - Check that object names match a regular expression
ParameterNumberChecker - Maximum number of Parameters for a method
ReturnChecker - Check that return is not used
SpacesAfterPlusChecker - Check that the plus sign is followed by a space
SpacesBeforePlusChecker - Check that the plus sign is preceded by a space
StructuralTypeChecker - Check that structural types are not used
RegexChecker – Check that no line matches a defined regular expression
14. How do I get involved?
Me: Matthew Farwell @matthewfarwell
Fork us on github: https://github.com/scalastyle/scalastyle
List of rules to implement:
https://github.com/scalastyle/scalastyle/wiki
User mailing list: scalastyle-users@googlegroups.com
Twitter: @scalastyle
Major pieces still needed: Maven plugin, Intellij plugin, sbt
plugin.
As always, documentation.