5. What is OOP good for?
Fixed APIs with unknown implementations. e.g.
Simula67 for simulation, Smalltalk for GUI
Objects are characterized by state, identity and
behavior - Grady Booch
8. Guideline #2
Don't pack too much in one
expression
find meaningful names for the intermediate results. It's
not easy but it's important
9. Guideline #3
Prefer Functional
vals v.s. vars
recursions or combinators v.s. loops
immutable v.s. mutable collections
transformations v.s. CRUD*
10. Guideline #4
But, don't diabolize local state
sometimes, mutable gives better performance
sometimes, mutable adds convenience
11. vars v.s. vals
var interfaces = parseClassHeader()...
if (isAnnotation) interfaces += ClassFileAnnotation
val parsedIfaces = parseClassHeader()...
val interfaces =
if (isAnnotation) parsedIfaces + ClassFileAnnotation
else parsedIfaces
12. loops v.s. combinators
val totalPrice = items.map(_.price).sum
val totalDiscount = items.map(_.discount).sum
13. val (totalPrice, totalDiscount) =
items.foldLeft((0.0, 0.0)) {
case ((tprice, tdiscount), item) => (tprice + item.price,
tdiscount + item.discount)
}
var totalPrice, totalDiscount = 0.0
for (item <- items) {
totalPrice += item.price
totalDiscount += item.discount
}
15. Guideline #6
Don't stop improving too early
shrink code by factor of 10 + make it more readable at
the same time
clean and elegant solutions don't come to mind at the
first time
it is fun to find better solutions
17. Choice #2
Alphabetic v.s. Symbolic
val xs = List("apples", "oranges", "pears")
xs.foldLeft("")((result, i) => result + i)
("" /: xs)((result, i) => result + i)
18. Choice #3
Loop, recursion or combinators
try conbinators first
if it becomes too tedious, or efficiency is a big concern,
try tail-recursion
use loop for simple case or for readability
24. in 2018
Java is still alive
Enterprise apps are using Scala
Startups are still NOT using Scala
25. myths / points
Readability over cleverness, LOC, FP, ...
Scala needs coding standard
Scala needs to move slower
Embracing instead of hating Java
26. Any fool can write code that a computer can understand.
Good programmers write code that human can
understand.
- Martin Fowler
27. Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as
possible, you are, by definition, not smart enough to
debug it.
- Brian Kernighan