scala in practice
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.TRANSCRIPT
![Page 1: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/1.jpg)
Scala In Practice:A Case Study
Tomer GabelJuly 2013
![Page 2: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/2.jpg)
H’ors D’oeuvres
![Page 3: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/3.jpg)
Some context
circa 2011:
• Startup with limited budget• Cloud hosted, Linux based• Complex POC-grade codebase
– Lots of Java code– Serving paying customers
![Page 4: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/4.jpg)
Strategy 101
Evolve codebase
Refactor and
extend
Supplant with new architectu
re
Complete rewrite
Amount of work involved
![Page 5: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/5.jpg)
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
![Page 6: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/6.jpg)
Why Scala?
• JVM-targeted, statically typed• Success stories (Twitter,
Foursquare, LinkedIn)• Active community• Good (not perfect) tool
ecosystem• Reasonable learning curve
![Page 7: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/7.jpg)
Alternatives
• Dynamic• LISP dialect
• Dynamic• Java-like
syntax• Script-oriented• Static• Java-like
syntax• In its infancy
![Page 8: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/8.jpg)
Scala in a nutshell
• Pervasive type inference
![Page 9: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/9.jpg)
Scala in a nutshell
• Type aliases, closures, first class functions and comprehensions Output:
![Page 10: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/10.jpg)
Scala in a nutshell
• Imperative with functional-style constructs
![Page 11: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/11.jpg)
Scala in a nutshell
• Traits: static mixins
![Page 12: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/12.jpg)
Scala in a nutshell
• Pattern matching FTW!
![Page 13: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/13.jpg)
Scala in a nutshell
• Other goodies– Option wrappers (no more NPEs!)– Powerful collection framework– Implicit conversions– Concurrent, immutable maps
(HAMT)– Built in actors
![Page 14: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/14.jpg)
Main course
![Page 15: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/15.jpg)
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!
![Page 16: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/16.jpg)
Focal points
• Learning curve• Tooling• In production
![Page 17: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/17.jpg)
Learning curve
• Risks:– Functional constructs need getting
used to– Few best practices:
• Coding conventions• Patterns• “Dangerous” features
![Page 18: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/18.jpg)
Learning curve
• Mitigations:– Relatively familiar syntax– Seamless Java integration
• Evolve your codebase to include Scala• Reuse existing components and
libraries– Lots of wow moments!
![Page 19: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/19.jpg)
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
![Page 20: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/20.jpg)
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!
![Page 21: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/21.jpg)
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))
![Page 22: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/22.jpg)
Lessons learned
• Promote functional constructs:– Intent over implementation–Massive code savings– Immutability, correct code by
design• Mentor junior developers!
![Page 23: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/23.jpg)
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!
![Page 24: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/24.jpg)
Focal points
• Learning curve• Tooling• In production
![Page 25: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/25.jpg)
IDE Support
• Eclipse support via TypeSafe’s own ScalaIDE
• IntelliJ IDEA support via plugin
![Page 26: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/26.jpg)
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)
![Page 27: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/27.jpg)
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
![Page 28: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/28.jpg)
IDE support
• Expect the unexpected–Error analysis/highlighting
• May highlight correct code• May miss errors (worse!)
–Generally works correctly–The situation is improving daily
![Page 29: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/29.jpg)
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
![Page 30: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/30.jpg)
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
![Page 31: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/31.jpg)
Library ecosystem
• Surprisingly mature landscape!
Testing: Specs2 ScalaTest
ORM: Slick Squeryl
Web: Play! Lift Scalara
Concurrency:
Akka Finagle
![Page 32: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/32.jpg)
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
![Page 33: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/33.jpg)
Focal points
• Learning curve• Tooling• In production
![Page 34: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/34.jpg)
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
![Page 35: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/35.jpg)
Other considerations
• Scala compiler emits lots of synthetic code–Deep call graph– Intermediate stack frames
• Default stack often too small– -Xss=2m should do
![Page 36: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/36.jpg)
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
![Page 37: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/37.jpg)
Dessert
![Page 38: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/38.jpg)
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
![Page 39: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/39.jpg)
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
![Page 40: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/40.jpg)
Questions?… answers may be forthcoming
![Page 41: Scala in practice](https://reader036.vdocument.in/reader036/viewer/2022062319/554f6c0bb4c9058a148b4f88/html5/thumbnails/41.jpg)
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!– [email protected]– http://www.tomergabel.com