12. What’s a side effect?
A lot of stuff we do now:
● Reassigning variables
● Modifying data in-place
● Throwing exceptions
● Interacting w/ the external environment
14. Immutability
OO makes code understandable by
encapsulating moving parts.
FP makes code understandable by
minimizing moving parts.
— Michael Feathers
16. An Example
def firstLarger(data: List[Int], threshold: Int): Int = ???
val numbers = List(3, 2, 4, 7, 9, 8, 1)
firstLarger(numbers, 5) // returns 7
firstLarger(numbers, 10) // returns ?
17. Method Signatures & Error Signaling
public int firstLarger(final int[] data,
final int threshold) throws NoSuchElementException
18. Method Signatures & Error Signaling
/**
* ...
* @return null on error
*/
public Integer firstLarger(final int[] data, final int threshold)
19. “My Billion Dollar Mistake”
“I call it my billion-dollar mistake. It was the invention of the null
reference in 1965... This has led to innumerable errors,
vulnerabilities, and system crashes, which have probably
caused a billion dollars of pain and damage in the last forty
years.”
- Sir Tony Hoare (2009)
Null References: The Billion Dollar Mistake
20. Use the type system
Option[T]
Some(value: T) None
21. Option
sealed abstract class Option[T]
// wrapper around a value
final class Some[T](value: T) extends Option[T]
// Singleton marker for no value
// Not null. Safe to dereference.
object None extends Option
22. Scala Map
val people = Map("Frank" -> "N. Furter",
"Lady" -> "Gaga")
val frank = people.get("Frank")
frank: Option[String] = Some(N. Furter)
val bob = people.get("Bob")
bob: Option[String] = None
ask what they want to get out of talk, langs used, who’s used underscore ?
Scala is #14
#25 in TIOBE
F#, Scala, Clojure are functional. Rust has functional elements.
pure function. value in value out.
no side effects. side effects = reach outside box for variable access or io. or exceptions.
No state. Not mutating a variable, we’re streaming data thru functions. Data isn’t changed during processing.
Btw, ??? is valid scala - it’s a symbolic method name for undefined methods. Handy in code instead of TODO
Famous computer scientist. Invented Quicksort. Won Turing Award, John von Neumann Medal, Kyoto Prize, Fellow of Royal Society & Royal Academy of Engineering.
Even in “null-safe” languages like Ruby, nil is a bad idea - http://www.sandimetz.com/blog/2014/12/19/suspicions-of-nil
Also, NULLs in SQL are a bad idea: https://www.youtube.com/watch?v=BC-OBmreMHY