SlideShare a Scribd company logo
1 of 27
Learning from
“Effective Scala”
              #akskscala 38
Scala study group at Akasaka, Tokyo, Japan
         Kazuhiro Sera @seratch
Effective Scala?

• http://twitter.github.com/effectivescala/
• Best practices from Twitter engineers
• Creative Commons BY 3.0 License
• Japanese version is also available (great
  work by @okapies and @scova0731)
Introduction
• A short essay that provides a set of best
  practices
• Libraries from Twitter - Finagle, Ostrich,
  Util, Gizzard, Scalding and more...
• Program in Scala, you’re not writing Java,
  nor Haskell, nor Python
• Assumes the reader is familiar with Scala
Formatting 1

• Consistent application of the same
  formatting rules will enhance readability
• Particular importance to Scala because its
  grammar has a high degree of overwrap
• Scala Style Guide and some additional rules
Formatting 2
• Indent: 2spaces, Straight-line length: 100
• External APIs should have longer and
  explanatory names
• Use val (= don’t rebind names)
• Avoid using ` to overload reserved names
• Active names for ops with side effects
Formatting 3
• Getters without `get` prefix
• Don’t repeat names that are already
  encapsulated - e.g. User.getUser(123)
• Wildcard import for more than 6 names
• Don’t use relative imports
• Put imports at the top (in one place)
Formatting 4
• Avoid using braces for simple expressions
• Use PartialFunction if possible
• Comments to explain the behavior of your
  code are bad sign
• Prefer “Obviously it works” to “it works,
  obviously” and restructure so
Types and Generics 1
• Fascinating but avoid type level
  programming in production code
• Use explicit return type annotations for
  public methods
• Immutable collections should be covariant
• Mutable collections should be invariant
Types and Generics 2
• Use type alias for convenient naming or
  clarify purpose
• Don’t subclassing when an alias will do
• Using implicits sparingly
• Ask yourself if there is a way to achieve the
  same thing without implicits
• “Pimp my library” pattern - e.g. RichInt
Collections 1
• Should read “collections design document”
• Prefer using immutable collections
• Use “mutable” namespace explicitly
• Receive the most generic collection type
  appropriate in methods/constructors
  (Iterable, Seq, Set, Map ...)
Collections 2
• Pipelining transformations leads to very
  succinct solutions but can also be confusing
  to the reader
• “val votesByLang = votes groupBy { case
  (lang, _) => lang }” instead of just
  “groupBy(_._1) “
• If you worry about namespace pollution,
  group expressions with {}
Collections 3
• Read “Performance Characterisitics”
• Use profiler (e.g.YourKit Java Profiler)
• Use arrays instead of lists for large
  sequences
• Use buffers when performance matters
• Not JavaConversions but JavaConverters
Concurrency
• Use Futures to manage concurrency
• Futures are declarative, composable and
  have principled handling of failures
• Prefer Future combinators
  (com.twitter.util.Future.join/collect)
• Reach for a concurrent collection after
  trying with a synchronized collection
Control structures 1
• Declarative style, more expression-oriented
• Recursion makes your programs simple
• Starts with a well-defined clean slate, no
  reference cells and invariants abound
• Check the tail call optimization application
  by the @tailrec annotation
Control structures 2

• Use returns to cut down branching and
  establish invariants
• Avoid using returns as you would do in
  imperative languages
• Avoid using returns inside of a closure
  because of hidden costs
Control structures 3
• for-comprehensions provides succinct and
  natural expressions
• Demerit: hidden costs to allocate and
  dispatch closures and unexpected
  semantics
• require/assert are useful when the type
  system cannot express the required
  invariants
Functional programming 1

• Value oriented programming (emphasizes
  the transformation of values over stateful
  mutation)
• Referentially transparent, stronger invaliants
• case classes as ADT(Algebraic Data Type)s
Functional programming 2

• Option type is a container which provides a
  safe alternative to the use of null
• Use “opt foreach { v => }” or pattern
  matching or getOrElse instead of
  “if(opt.isDefined) opt.get”
• Use Option(v) instead of Some(v)
Functional programming 3

• Might be more composability with a
  PartialFunction than returning an Option
• Destructuring bindings are particularly
  useful for tuples and case classes
• Lazy fields compute and memorizes a result
• Should make the cost model explicit and
  precisely control side-effects
Functional programming 4

• Use call-by-name only to construct natural
  DSLs (control constructs)
• Use call-by-name only in the last position of
  the last argument list
• Use explicit functions for multiple times
  execution or side-effecting
Object oriented programming 1


• Dependency Injection by typically defining
  trait and subclassing
• Injecting factories by using simple functions
• Keep traits short and orthogonal - e.g.
  Reader/Writer instead of IOer
Object oriented programming 2


• private[this] can aid performance
  optimizations
• Constrain visibility of singleton class types -
  e.g. def foo(): Foo = new Foo with Bar {}
• Don’t use structural types in normal use
Garbage collection

• Functional Scala code tends to generate
  more short-lived garbage than Java
• Don’t act without data, use profiling tools -
  e.g. heapster, gcprof
Java compatibility

• Sometimes your Scala code is not directly
  usable from Java (traits that contain
  implementation, collections, functions...)
• Sometimes need to separate Java APIs
• Write unit tests in Java
Twitter’s standard libraries 1


• Util: an extension to the Scala and Java
  standard libraries
• Finagle: RPC system - the kernel distributed
  systems components
Twitter’s standard libraries 2

• Future - a simple container(a type of
  collection) which hold the promise for the
  result of a computation which is not yet
  complete
• 3 states: pending, failed or completed
• Future#flatMap is useful to define
  composite operations
Twitter’s standard libraries 3

• Use callbacks(onSuccess) instead of foreach
• Future.value(), exception() creates pre-
  satisfied Futures
• Future.collect(), join() provide combinators
  that turn futures into one
• Future#cancel from consumers is
  propagated to its producer

More Related Content

What's hot

Java annotations
Java annotationsJava annotations
Java annotations
Sujit Kumar
 
PowerCLI in the Enterprise Breaking the Magicians Code original
PowerCLI in the Enterprise Breaking the Magicians Code   originalPowerCLI in the Enterprise Breaking the Magicians Code   original
PowerCLI in the Enterprise Breaking the Magicians Code original
jonathanmedd
 

What's hot (20)

Functional programming for the Advanced Beginner
Functional programming for the Advanced BeginnerFunctional programming for the Advanced Beginner
Functional programming for the Advanced Beginner
 
Java Tutorial Lab 1
Java Tutorial Lab 1Java Tutorial Lab 1
Java Tutorial Lab 1
 
Extracts from "Clean code"
Extracts from "Clean code"Extracts from "Clean code"
Extracts from "Clean code"
 
Collections
CollectionsCollections
Collections
 
Analysis of a basic java program
Analysis of a basic java programAnalysis of a basic java program
Analysis of a basic java program
 
Java annotations
Java annotationsJava annotations
Java annotations
 
Semantic DEX Components
Semantic DEX ComponentsSemantic DEX Components
Semantic DEX Components
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
CSharp for Unity Day 3
CSharp for Unity Day 3CSharp for Unity Day 3
CSharp for Unity Day 3
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Java- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solutionJava- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solution
 
Java related basic tutorial
Java related basic tutorialJava related basic tutorial
Java related basic tutorial
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Python libraries
Python librariesPython libraries
Python libraries
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 
Lambdas
LambdasLambdas
Lambdas
 
What's new in Java 8
What's new in Java 8What's new in Java 8
What's new in Java 8
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
PowerCLI in the Enterprise Breaking the Magicians Code original
PowerCLI in the Enterprise Breaking the Magicians Code   originalPowerCLI in the Enterprise Breaking the Magicians Code   original
PowerCLI in the Enterprise Breaking the Magicians Code original
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 

Viewers also liked

Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
mircodotta
 

Viewers also liked (14)

ScalikeJDBC Tutorial for Beginners
ScalikeJDBC Tutorial for BeginnersScalikeJDBC Tutorial for Beginners
ScalikeJDBC Tutorial for Beginners
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 
Java/Scala Lab 2016. Александр Конопко: Машинное обучение в Spark.
Java/Scala Lab 2016. Александр Конопко: Машинное обучение в Spark.Java/Scala Lab 2016. Александр Конопко: Машинное обучение в Spark.
Java/Scala Lab 2016. Александр Конопко: Машинное обучение в Spark.
 
Distributed machine learning 101 using apache spark from the browser
Distributed machine learning 101 using apache spark from the browserDistributed machine learning 101 using apache spark from the browser
Distributed machine learning 101 using apache spark from the browser
 
Scala for Machine Learning
Scala for Machine LearningScala for Machine Learning
Scala for Machine Learning
 
Scalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedInScalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedIn
 
Using Deep Learning for Recommendation
Using Deep Learning for RecommendationUsing Deep Learning for Recommendation
Using Deep Learning for Recommendation
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
 
Getting Started with Deep Learning using Scala
Getting Started with Deep Learning using ScalaGetting Started with Deep Learning using Scala
Getting Started with Deep Learning using Scala
 
PredictionIO – A Machine Learning Server in Scala – SF Scala
PredictionIO – A Machine Learning Server in Scala – SF ScalaPredictionIO – A Machine Learning Server in Scala – SF Scala
PredictionIO – A Machine Learning Server in Scala – SF Scala
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
 
Machine Learning with Scala
Machine Learning with ScalaMachine Learning with Scala
Machine Learning with Scala
 
Large-Scale Machine Learning with Apache Spark
Large-Scale Machine Learning with Apache SparkLarge-Scale Machine Learning with Apache Spark
Large-Scale Machine Learning with Apache Spark
 
Real time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkReal time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache Spark
 

Similar to Learning from "Effective Scala"

Code reviews
Code reviewsCode reviews
Code reviews
Roger Xia
 
Code reviews
Code reviewsCode reviews
Code reviews
Roger Xia
 

Similar to Learning from "Effective Scala" (20)

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Java
JavaJava
Java
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Javasession6
Javasession6Javasession6
Javasession6
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
Code reviews
Code reviewsCode reviews
Code reviews
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
Apex code (Salesforce)
Apex code (Salesforce)Apex code (Salesforce)
Apex code (Salesforce)
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and quality
 
Game Programming 04 - Style & Design Principles
Game Programming 04 - Style & Design PrinciplesGame Programming 04 - Style & Design Principles
Game Programming 04 - Style & Design Principles
 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
 
Code reviews
Code reviewsCode reviews
Code reviews
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
Elixir
ElixirElixir
Elixir
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 

More from Kazuhiro Sera

Future on Servlet #scala_ks
Future on Servlet #scala_ksFuture on Servlet #scala_ks
Future on Servlet #scala_ks
Kazuhiro Sera
 
テストの運用について #m3dev
テストの運用について #m3devテストの運用について #m3dev
テストの運用について #m3dev
Kazuhiro Sera
 

More from Kazuhiro Sera (20)

All I learned while working on a Scala OSS project for over six years #ScalaM...
All I learned while working on a Scala OSS project for over six years #ScalaM...All I learned while working on a Scala OSS project for over six years #ScalaM...
All I learned while working on a Scala OSS project for over six years #ScalaM...
 
Contributing to Scala OSS from East Asia #ScalaMatsuri
 Contributing to Scala OSS from East Asia #ScalaMatsuri Contributing to Scala OSS from East Asia #ScalaMatsuri
Contributing to Scala OSS from East Asia #ScalaMatsuri
 
Skinny Meetup Tokyo 2 日本語スライド
Skinny Meetup Tokyo 2 日本語スライドSkinny Meetup Tokyo 2 日本語スライド
Skinny Meetup Tokyo 2 日本語スライド
 
Skinny 2 Update
Skinny 2 UpdateSkinny 2 Update
Skinny 2 Update
 
Seasar ユーザだったプログラマが目指す OSS の世界展開 #seasarcon
Seasar ユーザだったプログラマが目指す OSS の世界展開 #seasarconSeasar ユーザだったプログラマが目指す OSS の世界展開 #seasarcon
Seasar ユーザだったプログラマが目指す OSS の世界展開 #seasarcon
 
Java エンジニアチームが始めやすい Scala コーディングスタイル #ichigayageek
Java エンジニアチームが始めやすい Scala コーディングスタイル #ichigayageekJava エンジニアチームが始めやすい Scala コーディングスタイル #ichigayageek
Java エンジニアチームが始めやすい Scala コーディングスタイル #ichigayageek
 
Future on Servlet #scala_ks
Future on Servlet #scala_ksFuture on Servlet #scala_ks
Future on Servlet #scala_ks
 
Servlet と Future の関わり方 #scala_ks
Servlet と Future の関わり方 #scala_ksServlet と Future の関わり方 #scala_ks
Servlet と Future の関わり方 #scala_ks
 
マイクロサービス運用の所感 #m3dev
マイクロサービス運用の所感 #m3devマイクロサービス運用の所感 #m3dev
マイクロサービス運用の所感 #m3dev
 
Scala が支える医療系ウェブサービス #jissenscala
Scala が支える医療系ウェブサービス #jissenscalaScala が支える医療系ウェブサービス #jissenscala
Scala が支える医療系ウェブサービス #jissenscala
 
Scala on Rails #rakutentech
Scala on Rails #rakutentechScala on Rails #rakutentech
Scala on Rails #rakutentech
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
Beginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccBeginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_ccc
 
[Japanese] Skinny Framework で始める Scala #jjug_ccc #ccc_r24
[Japanese] Skinny Framework で始める Scala #jjug_ccc #ccc_r24[Japanese] Skinny Framework で始める Scala #jjug_ccc #ccc_r24
[Japanese] Skinny Framework で始める Scala #jjug_ccc #ccc_r24
 
Skinny Framework 1.0.0
Skinny Framework 1.0.0Skinny Framework 1.0.0
Skinny Framework 1.0.0
 
Skinny Framework Progress Situation
Skinny Framework Progress SituationSkinny Framework Progress Situation
Skinny Framework Progress Situation
 
Skinny Framework 進捗どうですか? #fud_scala
Skinny Framework 進捗どうですか? #fud_scalaSkinny Framework 進捗どうですか? #fud_scala
Skinny Framework 進捗どうですか? #fud_scala
 
テストの運用について #m3dev
テストの運用について #m3devテストの運用について #m3dev
テストの運用について #m3dev
 
めんどくさくない Scala #kwkni_scala
めんどくさくない Scala #kwkni_scalaめんどくさくない Scala #kwkni_scala
めんどくさくない Scala #kwkni_scala
 
歌舞伎座.tech 1 LT - ScalikeJDBC Async & Skinny Framework #kbkz_tech
歌舞伎座.tech 1 LT - ScalikeJDBC Async & Skinny Framework #kbkz_tech歌舞伎座.tech 1 LT - ScalikeJDBC Async & Skinny Framework #kbkz_tech
歌舞伎座.tech 1 LT - ScalikeJDBC Async & Skinny Framework #kbkz_tech
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Learning from "Effective Scala"

  • 1. Learning from “Effective Scala” #akskscala 38 Scala study group at Akasaka, Tokyo, Japan Kazuhiro Sera @seratch
  • 2. Effective Scala? • http://twitter.github.com/effectivescala/ • Best practices from Twitter engineers • Creative Commons BY 3.0 License • Japanese version is also available (great work by @okapies and @scova0731)
  • 3. Introduction • A short essay that provides a set of best practices • Libraries from Twitter - Finagle, Ostrich, Util, Gizzard, Scalding and more... • Program in Scala, you’re not writing Java, nor Haskell, nor Python • Assumes the reader is familiar with Scala
  • 4. Formatting 1 • Consistent application of the same formatting rules will enhance readability • Particular importance to Scala because its grammar has a high degree of overwrap • Scala Style Guide and some additional rules
  • 5. Formatting 2 • Indent: 2spaces, Straight-line length: 100 • External APIs should have longer and explanatory names • Use val (= don’t rebind names) • Avoid using ` to overload reserved names • Active names for ops with side effects
  • 6. Formatting 3 • Getters without `get` prefix • Don’t repeat names that are already encapsulated - e.g. User.getUser(123) • Wildcard import for more than 6 names • Don’t use relative imports • Put imports at the top (in one place)
  • 7. Formatting 4 • Avoid using braces for simple expressions • Use PartialFunction if possible • Comments to explain the behavior of your code are bad sign • Prefer “Obviously it works” to “it works, obviously” and restructure so
  • 8. Types and Generics 1 • Fascinating but avoid type level programming in production code • Use explicit return type annotations for public methods • Immutable collections should be covariant • Mutable collections should be invariant
  • 9. Types and Generics 2 • Use type alias for convenient naming or clarify purpose • Don’t subclassing when an alias will do • Using implicits sparingly • Ask yourself if there is a way to achieve the same thing without implicits • “Pimp my library” pattern - e.g. RichInt
  • 10. Collections 1 • Should read “collections design document” • Prefer using immutable collections • Use “mutable” namespace explicitly • Receive the most generic collection type appropriate in methods/constructors (Iterable, Seq, Set, Map ...)
  • 11. Collections 2 • Pipelining transformations leads to very succinct solutions but can also be confusing to the reader • “val votesByLang = votes groupBy { case (lang, _) => lang }” instead of just “groupBy(_._1) “ • If you worry about namespace pollution, group expressions with {}
  • 12. Collections 3 • Read “Performance Characterisitics” • Use profiler (e.g.YourKit Java Profiler) • Use arrays instead of lists for large sequences • Use buffers when performance matters • Not JavaConversions but JavaConverters
  • 13. Concurrency • Use Futures to manage concurrency • Futures are declarative, composable and have principled handling of failures • Prefer Future combinators (com.twitter.util.Future.join/collect) • Reach for a concurrent collection after trying with a synchronized collection
  • 14. Control structures 1 • Declarative style, more expression-oriented • Recursion makes your programs simple • Starts with a well-defined clean slate, no reference cells and invariants abound • Check the tail call optimization application by the @tailrec annotation
  • 15. Control structures 2 • Use returns to cut down branching and establish invariants • Avoid using returns as you would do in imperative languages • Avoid using returns inside of a closure because of hidden costs
  • 16. Control structures 3 • for-comprehensions provides succinct and natural expressions • Demerit: hidden costs to allocate and dispatch closures and unexpected semantics • require/assert are useful when the type system cannot express the required invariants
  • 17. Functional programming 1 • Value oriented programming (emphasizes the transformation of values over stateful mutation) • Referentially transparent, stronger invaliants • case classes as ADT(Algebraic Data Type)s
  • 18. Functional programming 2 • Option type is a container which provides a safe alternative to the use of null • Use “opt foreach { v => }” or pattern matching or getOrElse instead of “if(opt.isDefined) opt.get” • Use Option(v) instead of Some(v)
  • 19. Functional programming 3 • Might be more composability with a PartialFunction than returning an Option • Destructuring bindings are particularly useful for tuples and case classes • Lazy fields compute and memorizes a result • Should make the cost model explicit and precisely control side-effects
  • 20. Functional programming 4 • Use call-by-name only to construct natural DSLs (control constructs) • Use call-by-name only in the last position of the last argument list • Use explicit functions for multiple times execution or side-effecting
  • 21. Object oriented programming 1 • Dependency Injection by typically defining trait and subclassing • Injecting factories by using simple functions • Keep traits short and orthogonal - e.g. Reader/Writer instead of IOer
  • 22. Object oriented programming 2 • private[this] can aid performance optimizations • Constrain visibility of singleton class types - e.g. def foo(): Foo = new Foo with Bar {} • Don’t use structural types in normal use
  • 23. Garbage collection • Functional Scala code tends to generate more short-lived garbage than Java • Don’t act without data, use profiling tools - e.g. heapster, gcprof
  • 24. Java compatibility • Sometimes your Scala code is not directly usable from Java (traits that contain implementation, collections, functions...) • Sometimes need to separate Java APIs • Write unit tests in Java
  • 25. Twitter’s standard libraries 1 • Util: an extension to the Scala and Java standard libraries • Finagle: RPC system - the kernel distributed systems components
  • 26. Twitter’s standard libraries 2 • Future - a simple container(a type of collection) which hold the promise for the result of a computation which is not yet complete • 3 states: pending, failed or completed • Future#flatMap is useful to define composite operations
  • 27. Twitter’s standard libraries 3 • Use callbacks(onSuccess) instead of foreach • Future.value(), exception() creates pre- satisfied Futures • Future.collect(), join() provide combinators that turn futures into one • Future#cancel from consumers is propagated to its producer

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n