Scala pitfalls

41
Scala pitfalls VSUG meetup 22.05.2013 Manuel Bernhardt - [email protected] - @elmanu Thursday, May 23, 13

Transcript of Scala pitfalls

Page 1: Scala pitfalls

Scala pitfallsVSUG meetup 22.05.2013

Manuel Bernhardt - [email protected] - @elmanu

Thursday, May 23, 13

Page 2: Scala pitfalls

Motivation

Thursday, May 23, 13

Page 3: Scala pitfalls

Motivation

Thursday, May 23, 13

Page 4: Scala pitfalls

Thursday, May 23, 13

Page 5: Scala pitfalls

Thursday, May 23, 13

Page 6: Scala pitfalls

Orders of ignorance

• 0th Order Ignorance: Lack of Ignorance

• 1st Order Ignorance: Lack of Knowledge

• 2nd Order Ignorance: Lack of Awareness

• 3rd Order Ignorance: Lack of a Suitably Efficient Process

• 4th Order Ignorance: Meta Ignorance

Thursday, May 23, 13

Page 7: Scala pitfalls

Thursday, May 23, 13

Page 8: Scala pitfalls

One-liner of death

Thursday, May 23, 13

Page 9: Scala pitfalls

BIG SCREEN

I HAZ IT !Thursday, May 23, 13

Page 10: Scala pitfalls

//  update  the  thumbnailsetThumbnaiL(id,  USERCOLLECTION,  collectionModel.thumbnail,  findThumbnailUrl(collectionModel.thumbnail,  collectionModel.objects),  updatedUserCollection.links.filter(_.linkType  ==  Link.LinkType.THUMBNAIL).map(_.link).headOption)

Thursday, May 23, 13

Page 11: Scala pitfalls

//  update  the  thumbnailsetThumbnail(id,  USERCOLLECTION,  collectionModel.thumbnail,  findThumbnailUrl(collectionModel.thumbnail,  collectionModel.objects),  updatedUserCollection.links.filter(_.linkType  ==  Link.LinkType.THUMBNAIL).map(_.link).headOption)

Thursday, May 23, 13

Page 12: Scala pitfalls

• hard to read

• hidden context switching

• debugging?

Thursday, May 23, 13

Page 13: Scala pitfalls

//  update  the  thumbnailval  thumbnailUrl  =  findThumbnailUrl(    collectionModel.thumbnail,    collectionModel.objects)val  maybeThumbnailLink  =  updatedUserCollection.links    .filter(_.linkType  ==  Link.LinkType.THUMBNAIL)    .map(_.link)    .headOption

setThumbnail(    id,    USERCOLLECTION,    collectionModel.thumbnail,    thumbnailUrl,    maybeThumbnailLink)

Thursday, May 23, 13

Page 14: Scala pitfalls

• Scala Style guide

• Tools! Scalastyle, Scalariform

Thursday, May 23, 13

Page 15: Scala pitfalls

null

Thursday, May 23, 13

Page 16: Scala pitfalls

null

Thursday, May 23, 13

Page 17: Scala pitfalls

So many Options

Thursday, May 23, 13

Page 18: Scala pitfalls

val  user:  Option[User]  =  ???

if  (user  ==  None)  {    //  foo!}  else  {    //  bar!    val  u  =  user.get}

Thursday, May 23, 13

Page 19: Scala pitfalls

val  user:  Option[User]  =  ???

if  (user.isDefined)  {    //  do  something  user-­‐full}  else  {    //  oh  my,  what  now?}

Thursday, May 23, 13

Page 20: Scala pitfalls

val  user:  Option[User]  =  ???

user  match  {    case  Some(u)  =>  //  now  I'm  happy!    case  None  =>  //  well,  doh}

Thursday, May 23, 13

Page 21: Scala pitfalls

• Option monad

• Option[T] ~= List of zero or one elements

Thursday, May 23, 13

Page 22: Scala pitfalls

val  maybeUser:  Option[User]  =  ???

maybeUser  map  {  user  =>    user.email}  getOrElse  {    "[email protected]"}

Thursday, May 23, 13

Page 23: Scala pitfalls

val  maybeUser:  Option[User]  =  ???

def  turnLeft(u:  User):  Option[User]  =  ???def  shake(u:  User):  Option[User]  =  ???def  shower(u:  User):  Option[User]  =  ???

val  transformedUser:  Option[User]  =  maybeUser    .flatMap  (turnLeft)    .flatMap  (shake)    .flatMap  (shower)

Thursday, May 23, 13

Page 24: Scala pitfalls

Did you say implicit?

Thursday, May 23, 13

Page 25: Scala pitfalls

implicit  def  string2Int(s:  String):  Int  =    Integer.parseInt(s)

Thursday, May 23, 13

Page 26: Scala pitfalls

implicit  def  string2Int(s:  String):  Int  =  Integer.parseInt(s)

val  foo  =  "22"val  bar  =  "some"

def  fooBar(i:  Int)  =  i  +  1

fooBar(foo)fooBar(bar)  //  ouch

Thursday, May 23, 13

Page 27: Scala pitfalls

It’s all about design

Thursday, May 23, 13

Page 28: Scala pitfalls

• use implicit conversions wisely: specific types

• use within the correct scope

• implicit lookup scope

Thursday, May 23, 13

Page 29: Scala pitfalls

A word on IDEs

Thursday, May 23, 13

Page 30: Scala pitfalls

Thursday, May 23, 13

Page 31: Scala pitfalls

• Scala is complex

• IDEs are getting there, but are not there yet

• False negatives be especially misleading at the beginning

Thursday, May 23, 13

Page 32: Scala pitfalls

Modularity

Thursday, May 23, 13

Page 33: Scala pitfalls

Thursday, May 23, 13

Page 34: Scala pitfalls

Cake pattern pitfalls

• Initialization order

• Initialization order

• Compilation time overhead (traits)

• Beautiful theory, less beautiful practice

Thursday, May 23, 13

Page 35: Scala pitfalls

Cake pattern pitfalls

• Initialization order

• Initialization order

• Compilation time overhead (traits)

• Beautiful theory, less beautiful practice

WAT?Thursday, May 23, 13

Page 36: Scala pitfalls

Alternatives

• Subcut

• MacWire - macros!

• Guice, Spring Scala

Thursday, May 23, 13

Page 37: Scala pitfalls

Time is money

Thursday, May 23, 13

Page 38: Scala pitfalls

http://xkcd.com/303/

Thursday, May 23, 13

Page 39: Scala pitfalls

• Scala compilation is slow

• Incremental compilation about to get better

• Get appropriate hardware

Thursday, May 23, 13

Page 40: Scala pitfalls

Useful links• Scala style guide - http://docs.scala-lang.org/style/

• Scalastyle - http://www.scalastyle.org

• SBT-scalariform: https://github.com/sbt/sbt-scalariform

• Post on implicits - http://stackoverflow.com/questions/5598085/where-does-scala-look-for-implicits

• Cake pattern - http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/

• Subcut - https://github.com/dickwall/subcut

• MacWire - https://github.com/adamw/macwire

• Play 2.1 MacWire demo - https://github.com/przemek-pokrywka/play-2.1-macwire-demo

• Orders of ignorance - http://www-plan.cs.colorado.edu/diwan/3308-s10/p17-armour.pdf

Thursday, May 23, 13

Page 41: Scala pitfalls

Questions?

• @elmanu

[email protected]

• http://github.com/manuelbernhardt

Thursday, May 23, 13