Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Conservative Language Use For Fun and Profit

Conservative Language Use For Fun and Profit

Scala provides many tools for building programs. Depending on how you
look at it, this can be a great thing, or a terrifying thing. I will
take a look at the scala "sub-set" that I adopted over years of pain,
and hopefully demonstrate that you can get more benefit from scala by
adopting less of it. In the end my sub-set may be different to yours,
but it might give you some different ideas on how to approach
programming in scala (or in general).

Mark Hibberd

February 10, 2016
Tweet

More Decks by Mark Hibberd

Other Decks in Programming

Transcript

  1. Conservative
    Mark Hibberd
    Scala

    View full-size slide

  2. You can make better use of tools by having
    deep knowledge of a tool, but only applying
    a specialised subset of its features.
    Claim #1

    View full-size slide

  3. You can’t effectively control your use of
    tools to a specialised subset unless you
    have very deep knowledge of the tool.
    Claim #2

    View full-size slide

  4. Programming languages are just tools.
    Claim #3

    View full-size slide

  5. Conservative usage patterns generalise to
    lots of tools not just languages.
    Claim #4

    View full-size slide

  6. SCALA
    a primer

    View full-size slide

  7. 1 scala> val x = List("1", "2", "3", "4").toSet() + "5"
    2
    3 scala> println(x)
    4
    5 output:
    6
    7 ???

    View full-size slide

  8. 1 scala> val x = List("1", "2", "3", "4").toSet() + "5"
    2
    3 scala> println(x)
    4
    5 output:
    6
    7 false5

    View full-size slide

  9. 1 scala> println({} + "")
    2
    3 output:
    4
    5 ???

    View full-size slide

  10. 1 scala> println({} + "")
    2
    3 output:
    4
    5 ()

    View full-size slide

  11. 1 scala> val x = Option(1).zip(Option(1)) ==
    2 Option((1, 1))
    3
    4 scala> println(x)
    5
    6 output:
    7
    8 ???

    View full-size slide

  12. 1 scala> val x = Option(1).zip(Option(1)) ==
    2 Option((1, 1))
    3
    4 scala> println(x)
    5
    6 output:
    7
    8 false

    View full-size slide

  13. 1 scala> val x = Option(1).zip(Option(1)) ==
    2 List((1, 1))
    3
    4 scala> println(x)
    5
    6 output:
    7
    8 ???

    View full-size slide

  14. 1 scala> val x = Option(1).zip(Option(1)) ==
    2 List((1, 1))
    3
    4 scala> println(x)
    5
    6 output:
    7
    8 true

    View full-size slide

  15. 1 scala> val x = Option(1).zip(Option(1)) ==
    2 Vector((1, 1))
    3
    4 scala> println(x)
    5
    6 output:
    7
    8 true

    View full-size slide

  16. scalac thinks your code is bad

    View full-size slide

  17. 1 List("1", "2", "3", "4").toSet() + “5"

    View full-size slide

  18. 1 List("1", "2", "3", "4").toSet() + "5"
    2
    3 // example.scala:1: error: not enough arguments for
    4 // method apply: (elem: Any)Boolean in trait GenSet.
    5 // Unspecified value parameter elem.
    -Yno-adapted-args

    View full-size slide

  19. 1 def update(data: String): Future[Unit] = for {
    2 n <- get
    3 _ <- save(n, data)
    4 } yield set(n + 1)
    5
    6 def get: Future[Int] =
    7 ???
    8
    9 def set(n: Int): Future[Int] =
    10 ???
    11
    12 def save(n: Int, data: String): Future[Unit] =
    13 ???

    View full-size slide

  20. 1 def update(data: String): Future[Unit] = for {
    2 n <- get
    3 _ <- save(n, data)
    4 } yield set(n + 1)
    5
    6 //example.scala:4: warning: discarded non-Unit value
    -Ywarn-value-discard

    View full-size slide

  21. -Xlint / -Ywarn-all
    https://github.com/scala/scala/blob/v2.10.3/src/compiler/scala/tools/nsc/settings/
    Warnings.scala#L18-L44

    View full-size slide

  22. -Xfatal-warnings

    View full-size slide

  23. scala
    scala vs

    View full-size slide

  24. Lets reduce the surface area of the
    language we have to worry about

    View full-size slide

  25. Lets not save characters by risking
    correctness

    View full-size slide

  26. Lets not make it “easy” by building
    APIs using ever scala feature ever

    View full-size slide

  27. Lets do more with less

    View full-size slide

  28. Types & Inference

    View full-size slide

  29. Closed better than Open

    View full-size slide

  30. Don’t confuse Types and Data

    View full-size slide

  31. Scala does not have Type Inference

    View full-size slide

  32. Variance or Not

    View full-size slide

  33. Any is the same as not having Types

    View full-size slide

  34. Type Classes vs Global Mutable HashMaps

    View full-size slide

  35. Syntactic Extensions

    View full-size slide

  36. Scala is just a Tool

    View full-size slide

  37. Performance vs Correctness

    View full-size slide

  38. Concurrent Scala is not a Win

    View full-size slide

  39. Integration not Ecosystem

    View full-size slide