devcon³: scala best practices

27
Scala Best Practices Tomer Gabel @ DevCon³ February 2013

Upload: tomer-gabel

Post on 10-May-2015

2.288 views

Category:

Technology


1 download

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.

TRANSCRIPT

Page 1: DevCon³: Scala Best Practices

Scala Best Practices

Tomer Gabel @ DevCon³February 2013

Page 2: DevCon³: Scala Best Practices

• Scala is awesome

• But it’s new

• … and misunderstood

Page 3: DevCon³: Scala Best Practices

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

Page 4: DevCon³: Scala Best Practices

Don’t

Page 5: DevCon³: Scala Best Practices

… overuse tuples

Page 6: DevCon³: Scala Best Practices

Do

Page 7: DevCon³: Scala Best Practices

… name intermediate steps

Page 8: DevCon³: Scala Best Practices

Also

Page 9: DevCon³: Scala Best Practices

… did you know you can do this?

Page 10: DevCon³: Scala Best Practices

Don’t

Page 11: DevCon³: Scala Best Practices

… use null

Page 12: DevCon³: Scala Best Practices

Do

Page 13: DevCon³: Scala Best Practices

… use Option!

Page 14: DevCon³: Scala Best Practices

Don’t

Page 15: DevCon³: Scala Best Practices

… overload operators

What the hell does this mean?

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

Page 16: DevCon³: Scala Best Practices

Don’t

Page 17: DevCon³: Scala Best Practices

… abuse implicit parameters

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

• Danger, Will Robinson!

• Primitives may be reused elsewhere

• Wrong semantics

Page 18: DevCon³: Scala Best Practices

Don’t

Page 19: DevCon³: Scala Best Practices

… abuse implicit methods, either

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

Implicit conversion considered harmful• Performance• Ambiguity• Code locality

Page 20: DevCon³: Scala Best Practices

Don’t

Page 21: DevCon³: Scala Best Practices

• Namespace pollution• Slow compilation• Unintended implicits

… over-import

import package1._import package2._import package3._

Page 22: DevCon³: Scala Best Practices

Do

Page 23: DevCon³: Scala Best Practices

… study the collection framework

• It’s a layer cake!

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

• Many gems!

Page 24: DevCon³: Scala Best Practices

Don’t

Page 25: DevCon³: Scala Best Practices

… abuse the type system

• Path-dependent types

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

• Case class inheritance–Not allowed in 2.10

Page 26: DevCon³: Scala Best Practices

Our time is almost up!

Questions?

Page 27: DevCon³: Scala Best Practices

Thank you for listening!

Get in touch:• http://tomergabel.com• [email protected]• @tomerg