DevCon³: Scala Best Practices

Preview:

DESCRIPTION

A lecture given at the DevCon TLV conference in February 2013 (http://devcon-february.events.co.il/presentations/771-scala-best-practices): Although Scala has been gaining tremendous traction over the last couple of years, it's still a relatively new language with few "common knowledge" best- and worst-practices. This lecture will attempt to distill working knowledge from 1.5 years of production Scala use, articles from around the web and other sources into a coherent set of recommendations for those considering Scala, as well as existing practitioners.

Citation preview

Scala Best Practices

Tomer Gabel @ DevCon³February 2013

• Scala is awesome

• But it’s new

• … and misunderstood

DO’S AND DON’TSWe’re gonna focus on…

Don’t

… overuse tuples

Do

… name intermediate steps

Also

… did you know you can do this?

Don’t

… use null

Do

… use Option!

Don’t

… overload operators

What the hell does this mean?

s(7) <*> (s(8) <*> (s(9) ∘ add3))

Don’t

… abuse implicit parameters

def lookup( id: Int )( implicit jdbcUrl: String )

• Danger, Will Robinson!

• Primitives may be reused elsewhere

• Wrong semantics

Don’t

… abuse implicit methods, either

implicit def stringToInt( x: String ) = x.toInt

Implicit conversion considered harmful• Performance• Ambiguity• Code locality

Don’t

• Namespace pollution• Slow compilation• Unintended implicits

… over-import

import package1._import package2._import package3._

Do

… study the collection framework

• It’s a layer cake!

• Always qualify mutability–Aim for immutability–But have no fear

• Many gems!

Don’t

… abuse the type system

• Path-dependent types

• Visibility modifiers (e.g. package-private)

• Case class inheritance–Not allowed in 2.10

Our time is almost up!

Questions?

Thank you for listening!

Get in touch:• http://tomergabel.com• tomer@tomergabel.com• @tomerg