Scala pitfalls

1,439 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,439
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Scala pitfalls

  1. 1. Scala pitfallsVSUG meetup 22.05.2013Manuel Bernhardt - manuel@delving.eu - @elmanuThursday, May 23, 13
  2. 2. MotivationThursday, May 23, 13
  3. 3. MotivationThursday, May 23, 13
  4. 4. Thursday, May 23, 13
  5. 5. Thursday, May 23, 13
  6. 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 SuitablyEfficient Process• 4th Order Ignorance: Meta IgnoranceThursday, May 23, 13
  7. 7. Thursday, May 23, 13
  8. 8. One-liner of deathThursday, May 23, 13
  9. 9. BIG SCREENI HAZ IT !Thursday, May 23, 13
  10. 10. //  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
  11. 11. //  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
  12. 12. • hard to read• hidden context switching• debugging?Thursday, May 23, 13
  13. 13. //  update  the  thumbnailval  thumbnailUrl  =  findThumbnailUrl(    collectionModel.thumbnail,    collectionModel.objects)val  maybeThumbnailLink  =  updatedUserCollection.links    .filter(_.linkType  ==  Link.LinkType.THUMBNAIL)    .map(_.link)    .headOptionsetThumbnail(    id,    USERCOLLECTION,    collectionModel.thumbnail,    thumbnailUrl,    maybeThumbnailLink)Thursday, May 23, 13
  14. 14. • Scala Style guide• Tools! Scalastyle, ScalariformThursday, May 23, 13
  15. 15. nullThursday, May 23, 13
  16. 16. nullThursday, May 23, 13
  17. 17. So many OptionsThursday, May 23, 13
  18. 18. val  user:  Option[User]  =  ???if  (user  ==  None)  {    //  foo!}  else  {    //  bar!    val  u  =  user.get}Thursday, May 23, 13
  19. 19. val  user:  Option[User]  =  ???if  (user.isDefined)  {    //  do  something  user-­‐full}  else  {    //  oh  my,  what  now?}Thursday, May 23, 13
  20. 20. val  user:  Option[User]  =  ???user  match  {    case  Some(u)  =>  //  now  Im  happy!    case  None  =>  //  well,  doh}Thursday, May 23, 13
  21. 21. • Option monad• Option[T] ~= List of zero or one elementsThursday, May 23, 13
  22. 22. val  maybeUser:  Option[User]  =  ???maybeUser  map  {  user  =>    user.email}  getOrElse  {    "foo@bar.com"}Thursday, May 23, 13
  23. 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. 24. Did you say implicit?Thursday, May 23, 13
  25. 25. implicit  def  string2Int(s:  String):  Int  =    Integer.parseInt(s)Thursday, May 23, 13
  26. 26. implicit  def  string2Int(s:  String):  Int  =  Integer.parseInt(s)val  foo  =  "22"val  bar  =  "some"def  fooBar(i:  Int)  =  i  +  1fooBar(foo)fooBar(bar)  //  ouchThursday, May 23, 13
  27. 27. It’s all about designThursday, May 23, 13
  28. 28. • use implicit conversions wisely: specifictypes• use within the correct scope• implicit lookup scopeThursday, May 23, 13
  29. 29. A word on IDEsThursday, May 23, 13
  30. 30. Thursday, May 23, 13
  31. 31. • Scala is complex• IDEs are getting there, but are not thereyet• False negatives be especially misleading atthe beginningThursday, May 23, 13
  32. 32. ModularityThursday, May 23, 13
  33. 33. Thursday, May 23, 13
  34. 34. Cake pattern pitfalls• Initialization order• Initialization order• Compilation time overhead (traits)• Beautiful theory, less beautiful practiceThursday, May 23, 13
  35. 35. Cake pattern pitfalls• Initialization order• Initialization order• Compilation time overhead (traits)• Beautiful theory, less beautiful practiceWAT?Thursday, May 23, 13
  36. 36. Alternatives• Subcut• MacWire - macros!• Guice, Spring ScalaThursday, May 23, 13
  37. 37. Time is moneyThursday, May 23, 13
  38. 38. http://xkcd.com/303/Thursday, May 23, 13
  39. 39. • Scala compilation is slow• Incremental compilation about to getbetter• Get appropriate hardwareThursday, May 23, 13
  40. 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.pdfThursday, May 23, 13
  41. 41. Questions?• @elmanu• manuel@delving.eu• http://github.com/manuelbernhardtThursday, May 23, 13

×