SlideShare a Scribd company logo
State of Scala code style
Hello! 
• Jaime Jorge from Codacy 
• Master thesis & company with Scala 
• Scala lover for 4 years
• Why? 
• Questions 
• Code styles 
• OSS Analysis 
• Brief overview of observations
Why care? 
• Expressive language, haters like to call ‘complex’ 
• We’re reviewing code everyday 
• Reduce number of moving parts 
• Onboarding becomes easier
Questions 
• What are the current code styles? 
• Are we respecting them? 
• What might become best practices/standard?
What code styles are 
currently out there?
Gathered code styles 
• Official Scala Style 
• Twitters Effective Scala 
• Apache Spark 
• Bizo Scala Style 
• Kiji project 
• Vertx mod lang 
• Apache Kafka
Method 
• Categories 
• Extract rules from all 
• Understand what they have in common/different
Rules extracted 
• 127 rules total 
• All taken from guidelines
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and quality
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)
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
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
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
Are we respecting them?
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
OSS Analysis 
• Analyzed 50 most popular Scala projects
Tools 
• ScalaStyle 
• HairyFotr/linter 
• WartRemover 
• Abide 
• scapegoat 
• Codacy
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)
Breakdown 
Curly braces 
Line max 
Method naming 
catching Fatal exceptions 
Mutable fields 
object naming convention 
class naming convention 
others
Other interesting facts 
• Significant violations of Option.get 
• Significant violations of nulls
Limitations of analysis 
• Most popular tend to be libraries 
• May not represent reality in industry perfectly
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.
What might become 
standard?
Areas 
• Collection of rules we see enforced humanly in Pull 
requests and commits
Areas 
• Collections 
• Testing 
• Object oriented programming 
• Functional programming
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) 
Vs 
Seq(startField.getOrElse("foo"),endField.getOrElse("bar"))
Collapsing of containers 
Futures 
Await.result(client.hGet(foo, bar)).get 
Vs 
client.hGet(foo, bar).map(f => ..)
Collections: optimizations 
From Scapegoat 
• 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
Akka 
Guidelines 
• Immutable messages 
• If state becomes complex, context.become 
• Not expose any state to outside
Unit testing 
Testing with state 
• Consistency in tests 
• Encapsulate test state in a Context object.
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
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)
Note: Microservices 
• Style of thinking and code in ‘Your Server as a 
Function’ 
• Server operations through future combinators 
• Declarative programming
Microservices: declarative 
programming 
recordHandletime andThen 
traceRequest andThen 
collectJvmStats andThen 
parseRequest andThen 
logRequest andThen 
recordClientStats andThen 
sanitize andThen 
respondToHealthCheck andThen 
applyTrafficControl andThen 
virtualHostServer
Scala Bay Meetup - The state of Scala code style and quality
Thanks! 
QA
Exclusive Offer 
Use Codacy for free for 3 months on private repositories 
Promo Code: SCALABAY-LLHLKS

More Related Content

KEY
Testing gone-right
PDF
ECMAScript 6 Overview & Comparision
PPTX
Airbnb Java Script style guide
PPTX
Adopting Elixir in a 10 year old codebase
KEY
Xtext Best Practices
PPTX
Babysitting your orm essenmacher, adam
PDF
From Stairway to Heaven onto the Highway to Hell with Xtext
PPTX
Keep Code Left - How to write better code in almost any language
Testing gone-right
ECMAScript 6 Overview & Comparision
Airbnb Java Script style guide
Adopting Elixir in a 10 year old codebase
Xtext Best Practices
Babysitting your orm essenmacher, adam
From Stairway to Heaven onto the Highway to Hell with Xtext
Keep Code Left - How to write better code in almost any language

What's hot (20)

PDF
AOEcon17: Searchperience - The journey from PHP and Solr to Scala and Elastic...
PPTX
Bsides Knoxville - APT2
PDF
Perfect Code
PDF
Software Engineering Thailand: Programming with Scala
PPTX
Part of the DLM story: Get your Database under Source Control - SQL In The City
PPTX
Generalization in Auto-Testing. How we put what we had into new Technological...
PPTX
WordPress Coding Standards & WP Hooks
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 02)
PDF
Dave Haeffner's Proven Method to Grading the Quality of Selenium Tests
PPTX
Introduction To AOP
PPTX
Segue to design patterns
ZIP
44 Slides About 22 Modules
PDF
Software Patterns
PDF
Defensive Coding Crash Course
PDF
Software testing with examples in Angular (and AngularJS)
PPTX
Automated Software Testing
DOC
Testing Tools Classroom Training
PDF
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
KEY
2 the essentials of effective java
AOEcon17: Searchperience - The journey from PHP and Solr to Scala and Elastic...
Bsides Knoxville - APT2
Perfect Code
Software Engineering Thailand: Programming with Scala
Part of the DLM story: Get your Database under Source Control - SQL In The City
Generalization in Auto-Testing. How we put what we had into new Technological...
WordPress Coding Standards & WP Hooks
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 02)
Dave Haeffner's Proven Method to Grading the Quality of Selenium Tests
Introduction To AOP
Segue to design patterns
44 Slides About 22 Modules
Software Patterns
Defensive Coding Crash Course
Software testing with examples in Angular (and AngularJS)
Automated Software Testing
Testing Tools Classroom Training
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
2 the essentials of effective java
Ad

Viewers also liked (8)

PPTX
Scala-Ls1
PDF
Scala in practice
PPTX
Tour d’horizon de scala
PDF
Scala vs Java 8 in a Java 8 World
PDF
Six years of Scala and counting
KEY
Scala For Java Programmers
PPT
Scala Talk at FOSDEM 2009
PDF
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
Scala-Ls1
Scala in practice
Tour d’horizon de scala
Scala vs Java 8 in a Java 8 World
Six years of Scala and counting
Scala For Java Programmers
Scala Talk at FOSDEM 2009
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
Ad

Similar to Scala Bay Meetup - The state of Scala code style and quality (20)

PPTX
TDD and the Legacy Code Black Hole
PPTX
Design p atterns
PDF
Working With Concurrency In Java 8
PDF
Style & Design Principles 01 - Code Style & Structure
KEY
Learning from "Effective Scala"
KEY
Enterprise search in Plone using Solr
PPTX
Code reviews
PPTX
Clean code presentation
PDF
Orthogonality: A Strategy for Reusable Code
PPTX
Design Like a Pro: Scripting Best Practices
PPTX
Design Like a Pro: Scripting Best Practices
PPTX
Code reviews
PDF
DSL's with Groovy
PDF
JSR 335 / java 8 - update reference
PPTX
Coding standards
PPTX
Writing Clean Code (Recommendations by Robert Martin)
PPTX
Scala in practice
PPTX
10 Sets of Best Practices for Java 8
PPTX
Salesforce Development Best Practices
PDF
ScalaClean at ScalaSphere 2019
TDD and the Legacy Code Black Hole
Design p atterns
Working With Concurrency In Java 8
Style & Design Principles 01 - Code Style & Structure
Learning from "Effective Scala"
Enterprise search in Plone using Solr
Code reviews
Clean code presentation
Orthogonality: A Strategy for Reusable Code
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Code reviews
DSL's with Groovy
JSR 335 / java 8 - update reference
Coding standards
Writing Clean Code (Recommendations by Robert Martin)
Scala in practice
10 Sets of Best Practices for Java 8
Salesforce Development Best Practices
ScalaClean at ScalaSphere 2019

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
additive manufacturing of ss316l using mig welding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Construction Project Organization Group 2.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPT
Project quality management in manufacturing
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Well-logging-methods_new................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
additive manufacturing of ss316l using mig welding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Construction Project Organization Group 2.pptx
bas. eng. economics group 4 presentation 1.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Model Code of Practice - Construction Work - 21102022 .pdf
Sustainable Sites - Green Building Construction
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CH1 Production IntroductoryConcepts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Foundation to blockchain - A guide to Blockchain Tech
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Operating System & Kernel Study Guide-1 - converted.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Project quality management in manufacturing
OOP with Java - Java Introduction (Basics)
Well-logging-methods_new................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx

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

  • 1. State of Scala code style
  • 2. Hello! • Jaime Jorge from Codacy • Master thesis & company with Scala • Scala lover for 4 years
  • 3. • Why? • Questions • Code styles • OSS Analysis • Brief overview of observations
  • 4. Why care? • Expressive language, haters like to call ‘complex’ • We’re reviewing code everyday • Reduce number of moving parts • Onboarding becomes easier
  • 5. Questions • What are the current code styles? • Are we respecting them? • What might become best practices/standard?
  • 6. What code styles are currently out there?
  • 7. Gathered code styles • Official Scala Style • Twitters Effective Scala • Apache Spark • Bizo Scala Style • Kiji project • Vertx mod lang • Apache Kafka
  • 8. Method • Categories • Extract rules from all • Understand what they have in common/different
  • 9. Rules extracted • 127 rules total • All taken from guidelines
  • 13. 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)
  • 15. 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
  • 17. Major differences • Adds more than 40 rules to original style guide • Introduces Collections, Object Oriented and Functional style advice • Shows preferences over APIs
  • 18. 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
  • 20. 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
  • 21. OSS Analysis • Analyzed 50 most popular Scala projects
  • 22. Tools • ScalaStyle • HairyFotr/linter • WartRemover • Abide • scapegoat • Codacy
  • 24. 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)
  • 25. Breakdown Curly braces Line max Method naming catching Fatal exceptions Mutable fields object naming convention class naming convention others
  • 26. Other interesting facts • Significant violations of Option.get • Significant violations of nulls
  • 27. Limitations of analysis • Most popular tend to be libraries • May not represent reality in industry perfectly
  • 28. 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.
  • 29. What might become standard?
  • 30. Areas • Collection of rules we see enforced humanly in Pull requests and commits
  • 31. Areas • Collections • Testing • Object oriented programming • Functional programming
  • 32. 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) Vs Seq(startField.getOrElse("foo"),endField.getOrElse("bar"))
  • 33. Collapsing of containers Futures Await.result(client.hGet(foo, bar)).get Vs client.hGet(foo, bar).map(f => ..)
  • 34. Collections: optimizations From Scapegoat • 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
  • 35. Akka Guidelines • Immutable messages • If state becomes complex, context.become • Not expose any state to outside
  • 36. Unit testing Testing with state • Consistency in tests • Encapsulate test state in a Context object.
  • 38. 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
  • 39. 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)
  • 40. Note: Microservices • Style of thinking and code in ‘Your Server as a Function’ • Server operations through future combinators • Declarative programming
  • 41. Microservices: declarative programming recordHandletime andThen traceRequest andThen collectJvmStats andThen parseRequest andThen logRequest andThen recordClientStats andThen sanitize andThen respondToHealthCheck andThen applyTrafficControl andThen virtualHostServer
  • 44. Exclusive Offer Use Codacy for free for 3 months on private repositories Promo Code: SCALABAY-LLHLKS