Your SlideShare is downloading. ×
0
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Coding with style: The Scalastyle style checker

4,087

Published on

Coding with style: The Scalastyle style checker …

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).

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,087
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
21
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. The style checker for ScalaScala Days 2012Matthew Farwell @matthewfarwell @scalastyle
  • 2. Matthew FarwellJava technical expert @ SQLI Suisse in Lausanne> 20 years development in various languages from Fortranto Java/ScalaContributor to various OSS projects, scala-ide, Junit, andof course ScalastyleEnjoying Scala Days 2012!
  • 3. The planWhyWhatDemoHowWhat next?
  • 4. Why an automated style checker?Douglas Crockford: "All languages should have a lint".Problem: too many peopleProblem: too many lines of codeSolution: 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 toCheckstyle, including bits of Findbugs & PMD.There is a continuum of checks that we need to make onour code, but which shouldn’t belong in the compiler.Projects are different, so everything needs to beconfigurable.They are just guidelines, they aren’t rules. But they canrules be for your project.
  • 6. What does it check? Format rulesHeavily inspired by CheckstyleFormat 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 rulesStyle rules (house style, things which make the codeharder to understand or more complex):Methods do not have more than x parametersNumber of classes in a fileNull not usedReturn not usedUse of structural types
  • 8. What does it check? Known sources ofbugsKnown sources of bugs, things to avoid:A class/object implements equals() but not hashCode() orvice versaA class/object implements a covariant equals(x) but notequals(Any)Missing case defaultStructural types
  • 9. What can’t it do?Anything which you don’t specify in the scala file.Inferred typesMismatches between typesImplicit conversionsMulti-file checks. Scalastyle only does one file at a time
  • 10. DemoEclipse plugin – configuration & usageCommand line tool// scalastyle:off magic.number
  • 11. HowThe excellent Scalariform. This gives us an AST and/or listof 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 desugaredThe Compiler Plugin API isn’t fully nailed down.
  • 12. Rules which are currently implementedFileLengthChecker - Check the number of lines in a fileFileLineLengthChecker - Check the number of characters in a lineFileTabChecker - Check that there are no tabs in a fileHeaderMatchesChecker - Check the first lines of each file matches the textWhitespaceEndOfLineChecker - Check that there is no trailing whitespace atthe end of linesClassNamesChecker - Check that class names match a regular expressionCovariantEqualsChecker - Check that classes and objects do not defineequals without overriding equals(java.lang.Object).EqualsHashCodeChecker - Check that if a class implements either equals orhashCode, it should implement the otherIllegalImportsChecker - Check that a class does not import certain classes
  • 13. Rules which are currently implementedIllegalImportsChecker - Check that a class does not import certain classesMagicNumberChecker - Checks for use of magic numbersNoCloneChecker - Check that classes and objects do not define the clone() methodNoFinalizeChecker - Check that classes and objects do not define the finalize() methodNoWhitespaceAfterLeftBracketChecker - No whitespace after left bracket [NoWhitespaceBeforeLeftBracketChecker - No whitespace before left bracket [NullChecker - Check that null is not usedObjectNamesChecker - Check that object names match a regular expressionParameterNumberChecker - Maximum number of Parameters for a methodReturnChecker - Check that return is not usedSpacesAfterPlusChecker - Check that the plus sign is followed by a spaceSpacesBeforePlusChecker - Check that the plus sign is preceded by a spaceStructuralTypeChecker - Check that structural types are not usedRegexChecker – Check that no line matches a defined regular expression
  • 14. How do I get involved?Me: Matthew Farwell @matthewfarwellFork us on github: https://github.com/scalastyle/scalastyleList of rules to implement:https://github.com/scalastyle/scalastyle/wikiUser mailing list: scalastyle-users@googlegroups.comTwitter: @scalastyleMajor pieces still needed: Maven plugin, Intellij plugin, sbtplugin.As always, documentation.
  • 15. Questions?

×