Scala in practice

Preview:

DESCRIPTION

While the Java platform has gained notoriety in the last 15 years as a robust application platform with a thriving ecosystem and well-established practices, the Java language has had its share of criticism. Highly verbose, overly didactic, limited feature set; whichever flavor of criticism you prefer, it's patently obvious that Java is playing catch up to more modern languages with a less rigid evolution path.The language landscape today is vastly different than it had been five or ten years ago; a wide array of languages are available, designed to suit a variety of flavors: Groovy, Clojure, Scala, Gosu, Kotlin... which should you choose? This lecture focuses on one company's decision to focus on Scala, and presents a case study based on our experiences using Scala in practice, in the hope of providing much-needed real world context to assist your decision.This presentation was used for the Scala In Practice lecture at the Botzia Israeli Java User Group meeting, May 3rd 2012.

Citation preview

Scala In Practice:A Case Study

Tomer GabelJuly 2013

H’ors D’oeuvres

Some context

circa 2011:

• Startup with limited budget• Cloud hosted, Linux based• Complex POC-grade codebase

– Lots of Java code– Serving paying customers

Strategy 101

Evolve codebase

Refactor and

extend

Supplant with new architectu

re

Complete rewrite

Amount of work involved

Data points

• Complete system rewrite impractical– Too much code– Business continuity a priority– Cannot afford product stagnation

• Team comfortable with Java platform

• … but want a better language

Why Scala?

• JVM-targeted, statically typed• Success stories (Twitter,

Foursquare, LinkedIn)• Active community• Good (not perfect) tool

ecosystem• Reasonable learning curve

Alternatives

• Dynamic• LISP dialect

• Dynamic• Java-like

syntax• Script-oriented• Static• Java-like

syntax• In its infancy

Scala in a nutshell

• Pervasive type inference

Scala in a nutshell

• Type aliases, closures, first class functions and comprehensions Output:

Scala in a nutshell

• Imperative with functional-style constructs

Scala in a nutshell

• Traits: static mixins

Scala in a nutshell

• Pattern matching FTW!

Scala in a nutshell

• Other goodies– Option wrappers (no more NPEs!)– Powerful collection framework– Implicit conversions– Concurrent, immutable maps

(HAMT)– Built in actors

Main course

Caveat emptor

• Scala is bleeding edge• You will get cut

– Learning curve– Rough edges– Partial/outdated documentation

• It’s well worth it, if you’re prepared!

Focal points

• Learning curve• Tooling• In production

Learning curve

• Risks:– Functional constructs need getting

used to– Few best practices:

• Coding conventions• Patterns• “Dangerous” features

Learning curve

• Mitigations:– Relatively familiar syntax– Seamless Java integration

• Evolve your codebase to include Scala• Reuse existing components and

libraries– Lots of wow moments!

Lessons learned

“New techniques are easy to explain. Old techniques are hard to maintain.” - an old Vulcan proverb

• Encourage experimentation• Encourage rapid iteration• Document and share new

techniques

Lessons learned

• Get a good grasp of the basics:– Functions and closures– Option wrappers– Pattern matching– Traits, traits and traits

• Each of these is immensely powerful

• Together, they make ponies!

Lessons learned

• Avoid wacky syntax!– Some common Scala libraries have

atrocious syntax (scalaz)– Does this make sense to you?

val p2c = ((_: Int) * (_: Int)).curriedsome(5) <*> (some(3) <*> p2c.pure[Option]) should equal(Some(15))

Lessons learned

• Promote functional constructs:– Intent over implementation–Massive code savings– Immutability, correct code by

design• Mentor junior developers!

Lessons learned

• Don’t go overboard– Use implicits sparingly– Avoid tuple overload

• x._1._2 * x._2 orElse x._3 == yuck• Use case classes and/or partial

functions instead– Path-dependent types are scary!

Focal points

• Learning curve• Tooling• In production

IDE Support

• Eclipse support via TypeSafe’s own ScalaIDE

• IntelliJ IDEA support via plugin

IDE support

• Loads better than a year or two ago– Mostly just works– ... but not up to Java standards yet

• Don’t skimp on dev hardware!• Take your time

– Assign a “go-to guy” (or gal)– Participate! (file bugs, track

commits)

IDE support

• Wonky debugging– Java debuggers work…– … with some caveats

• Step into synthetic stack frames• Do not step over closures

– TypeSafe are focusing on debugging

– JetBrains will likely follow

IDE support

• Expect the unexpected–Error analysis/highlighting

• May highlight correct code• May miss errors (worse!)

–Generally works correctly–The situation is improving daily

Build tools

• Native Scala build tool is sbt– Ivy2 (-> Maven repositories)– Scala syntax– Very powerful, steep learning curve

• Stay away from ant• Maven works well• … so do Buildr and Gradle

Compiler nasties

• Different compiler, different edge-cases– Scary compiler errors with 2.10.x– Much less of an issue since 2.10.2

• Slow compilation– sbt helps. A lot– IDEA “external compiler” helps too

Library ecosystem

• Surprisingly mature landscape!

Testing: Specs2 ScalaTest

ORM: Slick Squeryl

Web: Play! Lift Scalara

Concurrency:

Akka Finagle

Library ecosystem

• Use your favorite Java libraries natively!– Yes, this includes Spring, Guice and

Hibernate• Usually with no interop pain

– Options can help a lot– Callbacks may need a little work

Focal points

• Learning curve• Tooling• In production

GC considerations

• Lots of generated classes– Higher PermGen utilization– Increase -XX:MaxPermSize

• Lots of intermediate objects– High eden space churn (normally

OK)– Tune with –XX:NewRatio

Other considerations

• Scala compiler emits lots of synthetic code–Deep call graph– Intermediate stack frames

• Default stack often too small– -Xss=2m should do

Error analysis

• Scala stack traces look different– Lots of intermediate frames– … and delegations– … and unusual symbols

• You get used to it very quickly• Takipi’s Stackifier can help

Dessert

1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106 111 116 121 1260K

20000K

40000K

60000K

80000K

100000K

120000K

140000K

160000K

180000K

200000K

Codebase Evolution

ScalaJava

Weeks

KLO

CCodebase evolution

Conclusion

• I would recommend Scala for:– Startups willing to take the plunge– Small teams of senior developers– Large enterprises with tech

advisory/leadership teams• But not (yet) for:

– Contractors– Traditional enterprises

Questions?… answers may be forthcoming

Afterword

• newBrandAnalytics for allowing the use of the company name and R&D evidence

• Typesafe and the amazing Scala community for making Scala what it is

• … and SmileTemplate.com for the PowerPoint template

• Get in touch!– tomer@tomergabel.com– http://www.tomergabel.com