The style checker for Scala
Scala Days 2012
Matthew Farwell @matthewfarwell
                  @scalastyle
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!
The plan

Why
What
Demo
How
What next?
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?
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.
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
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
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
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
Demo
Eclipse plugin – configuration & usage
Command line tool
// scalastyle:off magic.number
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.
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
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
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.
Questions?

Coding with style: The Scalastyle style checker

  • 1.
    The style checkerfor Scala Scala Days 2012 Matthew Farwell @matthewfarwell @scalastyle
  • 2.
    Matthew Farwell Java technicalexpert @ 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!
  • 3.
  • 4.
    Why an automatedstyle 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? Scalastyleis 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 itcheck? 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 itcheck? 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 itcheck? 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 itdo? 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
  • 10.
    Demo Eclipse plugin –configuration & usage Command line tool // scalastyle:off magic.number
  • 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 arecurrently 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 arecurrently 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 Iget 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: [email protected] Twitter: @scalastyle Major pieces still needed: Maven plugin, Intellij plugin, sbt plugin. As always, documentation.
  • 15.