Top Banner
State of Scala code style
44

Scala Bay Meetup - The state of Scala code style and quality

Jul 14, 2015

Download

Engineering

Jaime Jorge
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scala Bay Meetup - The state of Scala code style and quality

State of Scala code style

Page 2: Scala Bay Meetup - The state of Scala code style and quality

Hello!

• Jaime Jorge from Codacy

• Master thesis & company with Scala

• Scala lover for 4 years

Page 3: Scala Bay Meetup - The state of Scala code style and quality

• Why?

• Questions

• Code styles

• OSS Analysis

• Brief overview of observations

Page 4: Scala Bay Meetup - The state of Scala code style and quality

Why care?

• Expressive language, haters like to call ‘complex’

• We’re reviewing code everyday

• Reduce number of moving parts

• Onboarding becomes easier

Page 5: Scala Bay Meetup - The state of Scala code style and quality

Questions

• What are the current code styles?

• Are we respecting them?

• What might become best practices/standard?

Page 6: Scala Bay Meetup - The state of Scala code style and quality

What code styles are currently out there?

Page 7: Scala Bay Meetup - The state of Scala code style and quality

Gathered code styles• Official Scala Style

• Twitters Effective Scala

• Apache Spark

• Bizo Scala Style

• Kiji project

• Vertx mod lang

• Apache Kafka

Page 8: Scala Bay Meetup - The state of Scala code style and quality

Method

• Categories

• Extract rules from all

• Understand what they have in common/different

Page 9: Scala Bay Meetup - The state of Scala code style and quality

Rules extracted

• 127 rules total

• All taken from guidelines

Page 10: Scala Bay Meetup - The state of Scala code style and quality
Page 11: Scala Bay Meetup - The state of Scala code style and quality
Page 12: Scala Bay Meetup - The state of Scala code style and quality
Page 13: Scala Bay Meetup - The state of Scala code style and quality

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)

Page 14: Scala Bay Meetup - The state of Scala code style and quality
Page 15: Scala Bay Meetup - The state of Scala code style and quality

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

Page 16: Scala Bay Meetup - The state of Scala code style and quality
Page 17: Scala Bay Meetup - The state of Scala code style and quality

Major differences

• Adds more than 40 rules to original style guide

• Introduces Collections, Object Oriented and Functional style advice

• Shows preferences over APIs

Page 18: Scala Bay Meetup - The state of Scala code style and quality

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

Page 19: Scala Bay Meetup - The state of Scala code style and quality

Are we respecting them?

Page 20: Scala Bay Meetup - The state of Scala code style and quality

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

Page 21: Scala Bay Meetup - The state of Scala code style and quality

OSS Analysis

• Analyzed 50 most popular Scala projects

Page 22: Scala Bay Meetup - The state of Scala code style and quality

Tools

• ScalaStyle

• HairyFotr/linter

• WartRemover

• Abide

• scapegoat

• Codacy

Page 23: Scala Bay Meetup - The state of Scala code style and quality
Page 24: Scala Bay Meetup - The state of Scala code style and quality

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)

Page 25: Scala Bay Meetup - The state of Scala code style and quality

BreakdownCurly braces

Line max

Method naming

catching Fatal exceptions

Mutable fields

object naming convention

class naming convention

others

Page 26: Scala Bay Meetup - The state of Scala code style and quality

Other interesting facts

• Significant violations of Option.get

• Significant violations of nulls

Page 27: Scala Bay Meetup - The state of Scala code style and quality

Limitations of analysis

• Most popular tend to be libraries

• May not represent reality in industry perfectly

Page 28: Scala Bay Meetup - The state of Scala code style and quality

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.

Page 29: Scala Bay Meetup - The state of Scala code style and quality

What might become standard?

Page 30: Scala Bay Meetup - The state of Scala code style and quality

Areas

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

Page 31: Scala Bay Meetup - The state of Scala code style and quality

Areas

• Collections

• Testing

• Object oriented programming

• Functional programming

Page 32: Scala Bay Meetup - The state of Scala code style and quality

Collapsing of containersOptions

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

Page 33: Scala Bay Meetup - The state of Scala code style and quality

Collapsing of containers

Await.result(client.hGet(foo, bar)).get

client.hGet(foo, bar).map(f => ..)

Vs

Futures

Page 34: Scala Bay Meetup - The state of Scala code style and quality

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

Page 35: Scala Bay Meetup - The state of Scala code style and quality

Akka

• Immutable messages

• If state becomes complex, context.become

• Not expose any state to outside

Guidelines

Page 36: Scala Bay Meetup - The state of Scala code style and quality

Unit testing

• Consistency in tests

• Encapsulate test state in a Context object.

Testing with state

Page 37: Scala Bay Meetup - The state of Scala code style and quality
Page 38: Scala Bay Meetup - The state of Scala code style and quality

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

Page 39: Scala Bay Meetup - The state of Scala code style and quality

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)

Page 40: Scala Bay Meetup - The state of Scala code style and quality

Note: Microservices

• Style of thinking and code in ‘Your Server as a Function’

• Server operations through future combinators

• Declarative programming

Page 41: Scala Bay Meetup - The state of Scala code style and quality

Microservices: declarative programming

recordHandletime andThentraceRequest andThencollectJvmStats andThenparseRequest andThenlogRequest andThenrecordClientStats andThensanitize andThenrespondToHealthCheck andThenapplyTrafficControl andThenvirtualHostServer

Page 42: Scala Bay Meetup - The state of Scala code style and quality
Page 43: Scala Bay Meetup - The state of Scala code style and quality

Thanks! QA

Page 44: Scala Bay Meetup - The state of Scala code style and quality

Exclusive Offer

Use Codacy for free for 3 months on private repositories

Promo Code: SCALABAY-LLHLKS