HOW WE MOVED FROM JAVA TO SCALA - GOTO...

66
HOW WE MOVED FROM JAVA TO SCALA Graham Tackley guardian.co.uk @tackers Wednesday, 12 October 11

Transcript of HOW WE MOVED FROM JAVA TO SCALA - GOTO...

Page 1: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

HOW WE MOVED FROMJAVA TO SCALA

Graham Tackleyguardian.co.uk

@tackers

Wednesday, 12 October 11

Page 2: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

HOW WE MOVED FROMJAVA TO SCALA

Graham Tackleyguardian.co.uk

@tackers

mostly

^

Wednesday, 12 October 11

Page 3: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Wednesday, 12 October 11

Page 4: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

History

• Java shop since 2006

• guardian.co.uk: java + spring + velocity + hibernate + oracle

Wednesday, 12 October 11

Page 5: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

History

• Java shop since 2006

• guardian.co.uk: java + spring + velocity + hibernate + oracle

• ~100k lines production java code

Wednesday, 12 October 11

Page 6: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

History

• Java shop since 2006

• guardian.co.uk: java + spring + velocity + hibernate + oracle

• ~100k lines production java code

• ~45k lines in velocity templates

Wednesday, 12 October 11

Page 7: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

History

• Java shop since 2006

• guardian.co.uk: java + spring + velocity + hibernate + oracle

• ~100k lines production java code

• ~45k lines in velocity templates

• ... and ~35k xml

Wednesday, 12 October 11

Page 8: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Reality

• This code base still lives!

• We can still enhance and improve

• We still release (at least) every two weeks

Wednesday, 12 October 11

Page 9: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Reality

• This code base still lives!

• We can still enhance and improve

• We still release (at least) every two weeks

BUT

Wednesday, 12 October 11

Page 10: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Java “BUT”

• Want to innovate faster

• Lots of code in current solution...

• ... takes a while to do what should be simple things ...

• ... and often solving a problem once or twice removed from the actual problem

Wednesday, 12 October 11

Page 11: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

We tried an alternative...

Wednesday, 12 October 11

Page 12: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Python + Django

• Easy to learn

• Great web-focused framework, so most things we wanted were out of the box

• Really good documentation & web support

Wednesday, 12 October 11

Page 13: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Python + Django

• Easy to learn

• Great web-focused framework, so most things we wanted were out of the box

• Really good documentation & web support

BUT

Wednesday, 12 October 11

Page 14: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Python “BUT”

Wednesday, 12 October 11

Page 15: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• Had to throw away years of java experience

The Python “BUT”

Wednesday, 12 October 11

Page 16: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• Had to throw away years of java experience

• Dev environment totally different (virtualenv, testing, ci, packaging...)

The Python “BUT”

Wednesday, 12 October 11

Page 17: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• Had to throw away years of java experience

• Dev environment totally different (virtualenv, testing, ci, packaging...)

• Runtime behaviour totally different (mod_wsgi, pgbouncer...)

The Python “BUT”

Wednesday, 12 October 11

Page 18: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• Had to throw away years of java experience

• Dev environment totally different (virtualenv, testing, ci, packaging...)

• Runtime behaviour totally different (mod_wsgi, pgbouncer...)

• Diagnostic tools totally different (stats capture, heap dumps, stack dumps, logging...)

The Python “BUT”

Wednesday, 12 October 11

Page 19: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• Had to throw away years of java experience

• Dev environment totally different (virtualenv, testing, ci, packaging...)

• Runtime behaviour totally different (mod_wsgi, pgbouncer...)

• Diagnostic tools totally different (stats capture, heap dumps, stack dumps, logging...)

• More moving parts (e.g. redis where we’d use singletons on the jvm)

The Python “BUT”

Wednesday, 12 October 11

Page 20: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• We lost as much (if not more) time operationally as we saved in development time

• For us, switching platform simply did not make sense

The Python “BUT”

Wednesday, 12 October 11

Page 21: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Is there a Third Way?

Wednesday, 12 October 11

Page 22: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Is there a Third Way?

Wednesday, 12 October 11

Page 23: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Is there a Third Way?

Wednesday, 12 October 11

Page 24: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Is there a Third Way?

Scala is a statically typed, compiled language running on the jvm

that “feels” like a dynamic language.

Wednesday, 12 October 11

Page 25: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

What’s so good about Scala?

Wednesday, 12 October 11

Page 26: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Same tools, libs, deployment & runtime characteristics as Java

List<String> myList = new ArrayList<String>();

• We still use IntelliJ (with the scala plugin)

• It’s just bytecode

• All the experience of running jvm in production still applies

• Can still use java libraries

Wednesday, 12 October 11

Page 27: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Mixed Scala-Java Projects Work

List<String> myList = new ArrayList<String>();

• Scala compiler can parse java, so can have one project containing both java & scala...

• ... with bi-directional dependencies

• We use this extensively to get the benefits of scala without big bang porting

Wednesday, 12 October 11

Page 28: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Much less verbose

=>

Wednesday, 12 October 11

Page 29: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Type inference

=>

ArrayList<String> myList = new ArrayList<String>();

List<String> myList = new ArrayList<String>();

val myList = new ArrayList[String]

Wednesday, 12 October 11

Page 30: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Console / REPL

List<String> myList = new ArrayList<String>();

• Encourages ipython / irb attitude

• Awesome for experimentation and exploration

Wednesday, 12 October 11

Page 31: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Powerful collections library

Wednesday, 12 October 11

Page 32: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Our First Scala Project: Content API

Wednesday, 12 October 11

Page 33: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Content API

• Provide API to access all of our website content

• Limited beta released early 2009

• Started implementation of final version late 2009

• Live May 2010: http://content.guardianapis.com

Wednesday, 12 October 11

Page 34: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Wednesday, 12 October 11

Page 35: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

• Started implementation

• java + guice + guice servlets + apache solr

• 3-4 java devs

2010 20112009

Nov

Wednesday, 12 October 11

Page 36: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

• A few people had played with scala

• No production scala code

Nov

Wednesday, 12 October 11

Page 37: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

• Integration tests with ScalaTest

• maven-scala-plugin

Jan

Wednesday, 12 October 11

Page 38: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Jan

Wednesday, 12 October 11

Page 39: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Jan

A fair bit of our test code was java-without-semicolons as we learnt about Scala...

Wednesday, 12 October 11

Page 40: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Jan

A fair bit of our test code was java-without-semicolons as we learnt about Scala...

Wednesday, 12 October 11

Page 41: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Feb

... despite that we loved it so much that after a month we decided to convert the whole app to Scala

java + guice + guice servlets + apache solr

Wednesday, 12 October 11

Page 42: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Feb

... despite that we loved it so much that after a month we decided to convert the whole app to Scala

scala + guice + guice servlets + apache solr

Wednesday, 12 October 11

Page 43: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

May

Live!

Wednesday, 12 October 11

Page 44: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Jul

Switched from maven to simple-build-tool

Mainly for incremental compilation

Wednesday, 12 October 11

Page 45: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Jul

scala + guice + guice servlets + apache solr

Wednesday, 12 October 11

Page 46: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Jul

5k loc => 3.5k (mostly due to writing better scala)

scala + lift + apache solr

Wednesday, 12 October 11

Page 47: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

2010 20112009

Today

• Scala used by default on all new jvm-based projects

• Still do some (externally-hosted) new things in python

• Team of 20 ex-java devs all audibly groan when working on java rather than scala

Wednesday, 12 October 11

Page 48: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Scala “BUT”?

Wednesday, 12 October 11

Page 49: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

But, isn’t Scala really complex?

The Scala “BUT”?

Wednesday, 12 October 11

Page 50: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Scala “BUT”?

This example taken from Cay Horstmannhttp://weblogs.java.net/blog/cayhorstmann/archive/2011/10/05/javaone-2011-day-3

Wednesday, 12 October 11

Page 51: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Four Scala Features That Scare Java

Developers...

Wednesday, 12 October 11

Page 52: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

The Four Scala Features That Scare Java

Developers...

... can all be explained in one slide each

Wednesday, 12 October 11

Page 53: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Symbolic method names

class Complex(real: Double, imag: Double) {

def +(other: Complex) = ....

}

Wednesday, 12 October 11

Page 54: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Infix & Postfix Operations

• obj.op(x) can be written obj op x

• obj.op can be written obj op

Wednesday, 12 October 11

Page 55: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Infix & Postfix Operations

• obj.op(x) can be written obj op x

• obj.op can be written obj op

So given: val a = new Complex(1, 3) val b = new Complex(3, 4)Can write: a.+(b)as a + b

Wednesday, 12 October 11

Page 56: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Higher order functionsscala> def apply(f: Int => Int, v: Int) = f(v)apply: (f: Int => Int, v: Int)Int

scala> apply(i => i * 2, 7)res1: Int = 14

Wednesday, 12 October 11

Page 57: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Higher order functionsscala> def apply(f: Int => Int, v: Int) = f(v)apply: (f: Int => Int, v: Int)Int

scala> apply(i => i * 2, 7)res1: Int = 14

scala> apply(_ * 2, 7)res2: Int = 14

Wednesday, 12 October 11

Page 58: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Higher order functions (ii)// Java Integer run(Callable<Integer> fn) throws Exception {  return fn.call(); }

 Integer processInt(final Integer i) throws Exception {  return run(new Callable<Integer>() {  public Integer call() throws Exception {  return i + 1;         }     });}

Wednesday, 12 October 11

Page 59: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Higher order functions (ii)// Java Integer run(Callable<Integer> fn) throws Exception {  return fn.call(); }

 Integer processInt(final Integer i) throws Exception {  return run(new Callable<Integer>() {  public Integer call() throws Exception {  return i + 1;         }     });}

// Scaladef run(f: => Int) = fdef processInt(i: Int) = run(i + 1)

Wednesday, 12 October 11

Page 60: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Implicit Conversion

• given an in-scope declarationimplicit def conv(a: A): B

• “conv” will be called whenever you have an A and need a B

• or you call a method on an instance of A that doesn’t exist on A but does on B

Wednesday, 12 October 11

Page 61: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Writing Good Scala

Wednesday, 12 October 11

Page 62: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Writing Good Scala

• Express your Intent. Simply.

Wednesday, 12 October 11

Page 63: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Writing Good Scala

• Express your Intent. Simply.

• The pram is full of toys.Use only to achieve the above.

Wednesday, 12 October 11

Page 64: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Demo

Wednesday, 12 October 11

Page 65: HOW WE MOVED FROM JAVA TO SCALA - GOTO Conferencegotocon.com/dl/goto-aarhus-2011/slides/GrahamTackley_How...• Scala used by default on all new jvm-based projects • Still do some

Summary

• Smooth migration path from Java

• Take it easy, and don’t fear java-without-semicolons in the early days

• You’ll lose if you stay there though!

• Incrementally embrace Scala features to increase readability

• Expect to keep learning

Wednesday, 12 October 11