coding with style: the scalastyle style checker

15
The style checker for Scala Scala Days 2012 Matthew Farwell @matthewfarwell @scalastyle

Upload: matthew-farwell

Post on 10-May-2015

4.048 views

Category:

Technology


3 download

DESCRIPTION

Coding with style: The Scalastyle style checkerScalastyle 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).

TRANSCRIPT

Page 1: Coding with style: The Scalastyle style checker

The style checker for Scala

Scala Days 2012

Matthew Farwell @matthewfarwell

@scalastyle

Page 2: Coding with style: The Scalastyle style checker

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!

Matthew Farwell

Page 3: Coding with style: The Scalastyle style checker

Why

What

Demo

How

What next?

The plan

Page 4: Coding with style: The Scalastyle 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?

Why an automated style checker?

Page 5: Coding with style: The Scalastyle style checker

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 is Scalastyle?

Page 6: Coding with style: The Scalastyle style checker

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? Format rules

Page 7: Coding with style: The Scalastyle style checker

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? Style rules

Page 8: Coding with style: The Scalastyle style checker

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 does it check? Known sources of bugs

Page 9: Coding with style: The Scalastyle style checker

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

What can’t it do?

Page 10: Coding with style: The Scalastyle style checker

Eclipse plugin – configuration & usage

Command line tool

// scalastyle:off magic.number

Demo

Page 11: Coding with style: The Scalastyle style checker

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.

How

Page 12: Coding with style: The Scalastyle style checker

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

Page 13: Coding with style: The Scalastyle style checker

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

Rules which are currently implemented

Page 14: Coding with style: The Scalastyle style checker

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.

How do I get involved?

Page 15: Coding with style: The Scalastyle style checker

Questions?