Slide 1

Slide 1 text

State of Scala code style

Slide 2

Slide 2 text

Hello! • Jaime Jorge from Codacy • Master thesis & company with Scala • Scala lover for 4 years

Slide 3

Slide 3 text

• Why? • Questions • Code styles • OSS Analysis • Brief overview of observations

Slide 4

Slide 4 text

Why care? • Expressive language, haters like to call ‘complex’ • We’re reviewing code everyday • Reduce number of moving parts • Onboarding becomes easier

Slide 5

Slide 5 text

Questions • What are the current code styles? • Are we respecting them? • What might become best practices/standard?

Slide 6

Slide 6 text

What code styles are currently out there?

Slide 7

Slide 7 text

Gathered code styles • Official Scala Style • Twitters Effective Scala • Apache Spark • Bizo Scala Style • Kiji project • Vertx mod lang • Apache Kafka

Slide 8

Slide 8 text

Method • Categories • Extract rules from all • Understand what they have in common/different

Slide 9

Slide 9 text

Rules extracted • 127 rules total • All taken from guidelines

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Major differences • Max 100 character line • Do not use relative imports from other packages • Always use braces for ifs except for ternary operator behavior • Do not use infix notation if method is not an operator (map, filter, flatMap)

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Major differences • Max 80 character line • Declarations: • Use vals when possible. • Use private when possible for member variables. • Use named arguments when passing in literal values • Options should be used instead of null whenever possible • Prefer case classes over tuples

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Major differences • Adds more than 40 rules to original style guide • Introduces Collections, Object Oriented and Functional style advice • Shows preferences over APIs

Slide 18

Slide 18 text

Conclusion • Built upon Scala Style Guide • 127 rules you can choose for your project • Some differences are important enough to include in Official Scala Style

Slide 19

Slide 19 text

Are we respecting them?

Slide 20

Slide 20 text

Answer by • Analyzing open source projects for violations of Official Scala Style • Understand % of compliance • Understand number of code style violations • Per category • Per code pattern

Slide 21

Slide 21 text

OSS Analysis • Analyzed 50 most popular Scala projects

Slide 22

Slide 22 text

Tools • ScalaStyle • HairyFotr/linter • WartRemover • Abide • scapegoat • Codacy

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Results • 51% code style compliance on average • Inverse correlation between Age of project and Code style compliance of project • Correlation between number of committers and number of violations (expected)

Slide 25

Slide 25 text

Breakdown Curly braces Line max Method naming catching Fatal exceptions Mutable fields object naming convention class naming convention others

Slide 26

Slide 26 text

Other interesting facts • Significant violations of Option.get • Significant violations of nulls

Slide 27

Slide 27 text

Limitations of analysis • Most popular tend to be libraries • May not represent reality in industry perfectly

Slide 28

Slide 28 text

Conclusions • 51% code style compliance on average • Naming and formatting biggest culprits • Older projects have less compliance • Are we respecting them? • Answer: we could do a better job.

Slide 29

Slide 29 text

What might become standard?

Slide 30

Slide 30 text

Areas • Collection of rules we see enforced humanly in Pull requests and commits

Slide 31

Slide 31 text

Areas • Collections • Testing • Object oriented programming • Functional programming

Slide 32

Slide 32 text

Collapsing of containers Options if (startField.isEmpty && endField.isEmpty) Seq("foo", "bar") else if (startField.isEmpty && !endField.isEmpty) Seq("foo", endField.get) else if (!startField.isEmpty && endField.isEmpty) Seq(startField.get, "bar") else Seq(startField.get, endField.get) Seq(startField.getOrElse("foo"),endField.getOrElse("bar")) Vs

Slide 33

Slide 33 text

Collapsing of containers Await.result(client.hGet(foo, bar)).get client.hGet(foo, bar).map(f => ..) Vs Futures

Slide 34

Slide 34 text

Collections: optimizations • exists(x => x == b) replaceable with contains(b) • .filter(x => ).head can be replaced with find(x => ) match { .. } • .filter(x =>).headOption can be replaced with find(x => ) • .filter(x => Bool).isEmpty can be replaced with !exists(x => Bool) • .filter(_.isDefined).map(_.get) can be replaced with flatten • .filter(x => Bool).size can be replaced more concisely with with count(x => Bool) • sort.filter can be replaced with filter.sort for performance • !Traversable.isEmpty can be replaced with Traversable.nonEmpty • !Traversable.nonEmpty can be replaced with Traversable.isEmpty From Scapegoat

Slide 35

Slide 35 text

Akka • Immutable messages • If state becomes complex, context.become • Not expose any state to outside Guidelines

Slide 36

Slide 36 text

Unit testing • Consistency in tests • Encapsulate test state in a Context object. Testing with state

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Object oriented programming • Use dependency injection for program modularization • The use of traits are highly encouraged • Do not use Exceptions for commonplace errors • Encode commonplace errors explicitly: using Option or (scala.util./com.twitter.util.)Try

Slide 39

Slide 39 text

Functional programming • Options should be used instead of null whenever possible • Do not overuse Option: if there is a sensible default — a Null Object — use that instead. • Don’t use pattern matching for conditional execution • Only use call-by-name for creating new control constructs such as DSLs • Prefer case classes over tuples (specially no ._1)

Slide 40

Slide 40 text

Note: Microservices • Style of thinking and code in ‘Your Server as a Function’ • Server operations through future combinators • Declarative programming

Slide 41

Slide 41 text

Microservices: declarative programming recordHandletime andThen traceRequest andThen collectJvmStats andThen parseRequest andThen logRequest andThen recordClientStats andThen sanitize andThen respondToHealthCheck andThen applyTrafficControl andThen virtualHostServer

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Thanks! QA

Slide 44

Slide 44 text

Exclusive Offer Use Codacy for free for 3 months on private repositories Promo Code: SCALABAY-LLHLKS