SlideShare a Scribd company logo
1 of 41
Download to read offline
Scala pitfalls
VSUG meetup 22.05.2013
Manuel Bernhardt - manuel@delving.eu - @elmanu
Thursday, May 23, 13
Motivation
Thursday, May 23, 13
Motivation
Thursday, May 23, 13
Thursday, May 23, 13
Thursday, May 23, 13
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
Thursday, May 23, 13
One-liner of death
Thursday, May 23, 13
BIG SCREEN
I HAZ IT !
Thursday, May 23, 13
//	
  update	
  the	
  thumbnail
setThumbnaiL(id,	
  USERCOLLECTION,	
  collectionModel.thumbnail,	
  findThumbnailUrl(collectionModel.thumbnail,	
  collectionModel.objects),	
  updatedUserCollection.links.filter(_.linkType	
  ==	
  Link.LinkType.THUMBNAIL).map(_.link).headOption)
Thursday, May 23, 13
//	
  update	
  the	
  thumbnail
setThumbnail(id,	
  USERCOLLECTION,	
  collectionModel.thumbnail,	
  
findThumbnailUrl(collectionModel.thumbnail,	
  
collectionModel.objects),	
  
updatedUserCollection.links.filter(_.linkType	
  ==	
  
Link.LinkType.THUMBNAIL).map(_.link).headOption)
Thursday, May 23, 13
• hard to read
• hidden context switching
• debugging?
Thursday, May 23, 13
//	
  update	
  the	
  thumbnail
val	
  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
• Scala Style guide
• Tools! Scalastyle, Scalariform
Thursday, May 23, 13
null
Thursday, May 23, 13
null
Thursday, May 23, 13
So many Options
Thursday, May 23, 13
val	
  user:	
  Option[User]	
  =	
  ???
if	
  (user	
  ==	
  None)	
  {
	
  	
  //	
  foo!
}	
  else	
  {
	
  	
  //	
  bar!
	
  	
  val	
  u	
  =	
  user.get
}
Thursday, May 23, 13
val	
  user:	
  Option[User]	
  =	
  ???
if	
  (user.isDefined)	
  {
	
  	
  //	
  do	
  something	
  user-­‐full
}	
  else	
  {
	
  	
  //	
  oh	
  my,	
  what	
  now?
}
Thursday, May 23, 13
val	
  user:	
  Option[User]	
  =	
  ???
user	
  match	
  {
	
  	
  case	
  Some(u)	
  =>	
  //	
  now	
  I'm	
  happy!
	
  	
  case	
  None	
  =>	
  //	
  well,	
  doh
}
Thursday, May 23, 13
• Option monad
• Option[T] ~= List of zero or one elements
Thursday, May 23, 13
val	
  maybeUser:	
  Option[User]	
  =	
  ???
maybeUser	
  map	
  {	
  user	
  =>
	
  	
  user.email
}	
  getOrElse	
  {
	
  	
  "foo@bar.com"
}
Thursday, May 23, 13
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
Did you say implicit?
Thursday, May 23, 13
implicit	
  def	
  string2Int(s:	
  String):	
  Int	
  =
	
  	
  Integer.parseInt(s)
Thursday, May 23, 13
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
It’s all about design
Thursday, May 23, 13
• use implicit conversions wisely: specific
types
• use within the correct scope
• implicit lookup scope
Thursday, May 23, 13
A word on IDEs
Thursday, May 23, 13
Thursday, May 23, 13
• Scala is complex
• IDEs are getting there, but are not there
yet
• False negatives be especially misleading at
the beginning
Thursday, May 23, 13
Modularity
Thursday, May 23, 13
Thursday, May 23, 13
Cake pattern pitfalls
• Initialization order
• Initialization order
• Compilation time overhead (traits)
• Beautiful theory, less beautiful practice
Thursday, May 23, 13
Cake pattern pitfalls
• Initialization order
• Initialization order
• Compilation time overhead (traits)
• Beautiful theory, less beautiful practice
WAT?
Thursday, May 23, 13
Alternatives
• Subcut
• MacWire - macros!
• Guice, Spring Scala
Thursday, May 23, 13
Time is money
Thursday, May 23, 13
http://xkcd.com/303/
Thursday, May 23, 13
• Scala compilation is slow
• Incremental compilation about to get
better
• Get appropriate hardware
Thursday, May 23, 13
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
Questions?
• @elmanu
• manuel@delving.eu
• http://github.com/manuelbernhardt
Thursday, May 23, 13

More Related Content

More from Manuel Bernhardt

Is there anybody out there? Reactive Systems Hamburg
Is there anybody out there? Reactive Systems HamburgIs there anybody out there? Reactive Systems Hamburg
Is there anybody out there? Reactive Systems HamburgManuel Bernhardt
 
Is there anybody out there? Scala Days Berlin 2018
Is there anybody out there? Scala Days Berlin 2018Is there anybody out there? Scala Days Berlin 2018
Is there anybody out there? Scala Days Berlin 2018Manuel Bernhardt
 
Is there anybody out there?
Is there anybody out there?Is there anybody out there?
Is there anybody out there?Manuel Bernhardt
 
Is there anybody out there?
Is there anybody out there?Is there anybody out there?
Is there anybody out there?Manuel Bernhardt
 
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 20178 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017Manuel Bernhardt
 
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware ofScala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware ofManuel Bernhardt
 
8 Akka anti-patterns you'd better be aware of
8 Akka anti-patterns you'd better be aware of8 Akka anti-patterns you'd better be aware of
8 Akka anti-patterns you'd better be aware ofManuel Bernhardt
 
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVMVoxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVMManuel Bernhardt
 
Back to the futures, actors and pipes: using Akka for large-scale data migration
Back to the futures, actors and pipes: using Akka for large-scale data migrationBack to the futures, actors and pipes: using Akka for large-scale data migration
Back to the futures, actors and pipes: using Akka for large-scale data migrationManuel Bernhardt
 
Project Phoenix - From PHP to the Play Framework in 3 months
Project Phoenix - From PHP to the Play Framework in 3 monthsProject Phoenix - From PHP to the Play Framework in 3 months
Project Phoenix - From PHP to the Play Framework in 3 monthsManuel Bernhardt
 
Tips and tricks for setting up a Play 2 project
Tips and tricks for setting up a Play 2 projectTips and tricks for setting up a Play 2 project
Tips and tricks for setting up a Play 2 projectManuel Bernhardt
 

More from Manuel Bernhardt (13)

Is there anybody out there? Reactive Systems Hamburg
Is there anybody out there? Reactive Systems HamburgIs there anybody out there? Reactive Systems Hamburg
Is there anybody out there? Reactive Systems Hamburg
 
Is there anybody out there? Scala Days Berlin 2018
Is there anybody out there? Scala Days Berlin 2018Is there anybody out there? Scala Days Berlin 2018
Is there anybody out there? Scala Days Berlin 2018
 
Is there anybody out there?
Is there anybody out there?Is there anybody out there?
Is there anybody out there?
 
Is there anybody out there?
Is there anybody out there?Is there anybody out there?
Is there anybody out there?
 
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 20178 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
 
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware ofScala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
 
8 Akka anti-patterns you'd better be aware of
8 Akka anti-patterns you'd better be aware of8 Akka anti-patterns you'd better be aware of
8 Akka anti-patterns you'd better be aware of
 
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVMVoxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
 
Back to the futures, actors and pipes: using Akka for large-scale data migration
Back to the futures, actors and pipes: using Akka for large-scale data migrationBack to the futures, actors and pipes: using Akka for large-scale data migration
Back to the futures, actors and pipes: using Akka for large-scale data migration
 
Project Phoenix - From PHP to the Play Framework in 3 months
Project Phoenix - From PHP to the Play Framework in 3 monthsProject Phoenix - From PHP to the Play Framework in 3 months
Project Phoenix - From PHP to the Play Framework in 3 months
 
Scala - Java2Days Sofia
Scala - Java2Days SofiaScala - Java2Days Sofia
Scala - Java2Days Sofia
 
Tips and tricks for setting up a Play 2 project
Tips and tricks for setting up a Play 2 projectTips and tricks for setting up a Play 2 project
Tips and tricks for setting up a Play 2 project
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 

Scala pitfalls

  • 1. Scala pitfalls VSUG meetup 22.05.2013 Manuel Bernhardt - manuel@delving.eu - @elmanu Thursday, May 23, 13
  • 6. 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
  • 9. BIG SCREEN I HAZ IT ! Thursday, May 23, 13
  • 10. //  update  the  thumbnail setThumbnaiL(id,  USERCOLLECTION,  collectionModel.thumbnail,  findThumbnailUrl(collectionModel.thumbnail,  collectionModel.objects),  updatedUserCollection.links.filter(_.linkType  ==  Link.LinkType.THUMBNAIL).map(_.link).headOption) Thursday, May 23, 13
  • 11. //  update  the  thumbnail setThumbnail(id,  USERCOLLECTION,  collectionModel.thumbnail,   findThumbnailUrl(collectionModel.thumbnail,   collectionModel.objects),   updatedUserCollection.links.filter(_.linkType  ==   Link.LinkType.THUMBNAIL).map(_.link).headOption) Thursday, May 23, 13
  • 12. • hard to read • hidden context switching • debugging? Thursday, May 23, 13
  • 13. //  update  the  thumbnail val  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
  • 14. • Scala Style guide • Tools! Scalastyle, Scalariform Thursday, May 23, 13
  • 18. val  user:  Option[User]  =  ??? if  (user  ==  None)  {    //  foo! }  else  {    //  bar!    val  u  =  user.get } Thursday, May 23, 13
  • 19. val  user:  Option[User]  =  ??? if  (user.isDefined)  {    //  do  something  user-­‐full }  else  {    //  oh  my,  what  now? } Thursday, May 23, 13
  • 20. val  user:  Option[User]  =  ??? user  match  {    case  Some(u)  =>  //  now  I'm  happy!    case  None  =>  //  well,  doh } Thursday, May 23, 13
  • 21. • Option monad • Option[T] ~= List of zero or one elements Thursday, May 23, 13
  • 22. val  maybeUser:  Option[User]  =  ??? maybeUser  map  {  user  =>    user.email }  getOrElse  {    "foo@bar.com" } Thursday, May 23, 13
  • 23. 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
  • 24. Did you say implicit? Thursday, May 23, 13
  • 25. implicit  def  string2Int(s:  String):  Int  =    Integer.parseInt(s) Thursday, May 23, 13
  • 26. 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
  • 27. It’s all about design Thursday, May 23, 13
  • 28. • use implicit conversions wisely: specific types • use within the correct scope • implicit lookup scope Thursday, May 23, 13
  • 29. A word on IDEs Thursday, May 23, 13
  • 31. • Scala is complex • IDEs are getting there, but are not there yet • False negatives be especially misleading at the beginning Thursday, May 23, 13
  • 34. Cake pattern pitfalls • Initialization order • Initialization order • Compilation time overhead (traits) • Beautiful theory, less beautiful practice Thursday, May 23, 13
  • 35. Cake pattern pitfalls • Initialization order • Initialization order • Compilation time overhead (traits) • Beautiful theory, less beautiful practice WAT? Thursday, May 23, 13
  • 36. Alternatives • Subcut • MacWire - macros! • Guice, Spring Scala Thursday, May 23, 13
  • 39. • Scala compilation is slow • Incremental compilation about to get better • Get appropriate hardware Thursday, May 23, 13
  • 40. 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
  • 41. Questions? • @elmanu • manuel@delving.eu • http://github.com/manuelbernhardt Thursday, May 23, 13