40
Scala Days 2013 by Hung Lin @hunglin

Tech Talk - Things I Learned at Scala Days 2013

Embed Size (px)

DESCRIPTION

Tech Talk I gave at my company for the things I learned at Scala Days 2013

Citation preview

Page 1: Tech Talk - Things I Learned at Scala Days 2013

Scala Days 2013by Hung Lin @hunglin

Page 2: Tech Talk - Things I Learned at Scala Days 2013

Scala the **** out of NYC3 days event @ NYCScala Sunday @ meetup HQ: 8 talksShapeless workshop on Monday

Page 3: Tech Talk - Things I Learned at Scala Days 2013

Hot TopicsScala 2.10Style of Scala*Asynchronous Programming - akkaBig Data / Streaming Data - SparkMacroPlay

Page 4: Tech Talk - Things I Learned at Scala Days 2013

Keynote: Scala with Styleby Martin Odersky, creator of scala

Page 5: Tech Talk - Things I Learned at Scala Days 2013

What is OOP good for?Fixed APIs with unknown implementations. e.g.Simula67 for simulation, Smalltalk for GUIObjects are characterized by state, identity andbehavior - Grady Booch

Page 6: Tech Talk - Things I Learned at Scala Days 2013

OOP v.s. FP

Page 7: Tech Talk - Things I Learned at Scala Days 2013

Guideline #1 Keep it simple

Simple Made Easy - Rich Hichey

Page 8: Tech Talk - Things I Learned at Scala Days 2013

Guideline #2 Don't pack too much in one

expressionfind meaningful names for the intermediate results. It's

not easy but it's important

Page 9: Tech Talk - Things I Learned at Scala Days 2013

Guideline #3 Prefer Functional

vals v.s. varsrecursions or combinators v.s. loopsimmutable v.s. mutable collectionstransformations v.s. CRUD*

Page 10: Tech Talk - Things I Learned at Scala Days 2013

Guideline #4 But, don't diabolize local statesometimes, mutable gives better performancesometimes, mutable adds convenience

Page 11: Tech Talk - Things I Learned at Scala Days 2013

vars v.s. valsvar interfaces = parseClassHeader()...if (isAnnotation) interfaces += ClassFileAnnotation

val parsedIfaces = parseClassHeader()...val interfaces = if (isAnnotation) parsedIfaces + ClassFileAnnotation else parsedIfaces

Page 12: Tech Talk - Things I Learned at Scala Days 2013

loops v.s. combinatorsval totalPrice = items.map(_.price).sumval totalDiscount = items.map(_.discount).sum

Page 13: Tech Talk - Things I Learned at Scala Days 2013

val (totalPrice, totalDiscount) = items.foldLeft((0.0, 0.0)) { case ((tprice, tdiscount), item) => (tprice + item.price, tdiscount + item.discount) }

var totalPrice, totalDiscount = 0.0for (item <- items) { totalPrice += item.price totalDiscount += item.discount}

Page 14: Tech Talk - Things I Learned at Scala Days 2013

Guideline #5 Careful with mutable objects

val m = ArrayBuffer[Int]()

Page 15: Tech Talk - Things I Learned at Scala Days 2013

Guideline #6 Don't stop improving too early

shrink code by factor of 10 + make it more readable atthe same timeclean and elegant solutions don't come to mind at thefirst timeit is fun to find better solutions

Page 16: Tech Talk - Things I Learned at Scala Days 2013

Choice #1 Infix v.s. "."

Page 17: Tech Talk - Things I Learned at Scala Days 2013

Choice #2 Alphabetic v.s. Symbolic

val xs = List("apples", "oranges", "pears")

xs.foldLeft("")((result, i) => result + i)

("" /: xs)((result, i) => result + i)

Page 18: Tech Talk - Things I Learned at Scala Days 2013

Choice #3 Loop, recursion or combinators

try conbinators firstif it becomes too tedious, or efficiency is a big concern,try tail-recursionuse loop for simple case or for readability

Page 19: Tech Talk - Things I Learned at Scala Days 2013

Choice #4 Procedures or "="

DON'T use procedure syntax

def method: Unit = { // operations}

Page 20: Tech Talk - Things I Learned at Scala Days 2013

Choice #5 Private v.s. nested

use nested function to avoid passing parametersuse nested function for small functionsdon't nest too many levels

Page 21: Tech Talk - Things I Learned at Scala Days 2013

Choice #6 pattern matching v.s. dynamic

dispatchthe expression problem, check out

, lecture 4.5 and 4.6scala class @ coursera

Page 22: Tech Talk - Things I Learned at Scala Days 2013

Choice #7 type parameters v.s. abstract type

members

try type parameters firstavoid Animals[_]

a good article by Bill Venners

Page 23: Tech Talk - Things I Learned at Scala Days 2013

Keynote: Scala in 2018by Rod Johnson, creator of Spring

Page 24: Tech Talk - Things I Learned at Scala Days 2013

in 2018Java is still aliveEnterprise apps are using ScalaStartups are still NOT using Scala

Page 25: Tech Talk - Things I Learned at Scala Days 2013

myths / pointsReadability over cleverness, LOC, FP, ...Scala needs coding standardScala needs to move slowerEmbracing instead of hating Java

Page 26: Tech Talk - Things I Learned at Scala Days 2013

Any fool can write code that a computer can understand.Good programmers write code that human can

understand. - Martin Fowler

Page 27: Tech Talk - Things I Learned at Scala Days 2013

Debugging is twice as hard as writing the code in the firstplace. Therefore, if you write the code as cleverly aspossible, you are, by definition, not smart enough to

debug it. - Brian Kernighan

Page 28: Tech Talk - Things I Learned at Scala Days 2013

Asynchronous Programming

Page 29: Tech Talk - Things I Learned at Scala Days 2013

asynchronous v.s. parallelwhat else?who's doing it?

Page 30: Tech Talk - Things I Learned at Scala Days 2013

asynchronous v.s. batching

Page 31: Tech Talk - Things I Learned at Scala Days 2013

pitfall: thread pool?for tomcat, one thread handles one request

Page 32: Tech Talk - Things I Learned at Scala Days 2013

pitfall: back pressure

Page 33: Tech Talk - Things I Learned at Scala Days 2013

pitfall: performance v.s. scaling issue

Page 34: Tech Talk - Things I Learned at Scala Days 2013

pitfall: theory v.s. realitycost of thread context switch# of threads v.s. # of CPUswhere is the bottleneck?

Page 35: Tech Talk - Things I Learned at Scala Days 2013

Some thoughts about our system

Page 36: Tech Talk - Things I Learned at Scala Days 2013

Upgrade to scala 2.10

Page 37: Tech Talk - Things I Learned at Scala Days 2013

Use akkaasynchronous programmingfault tolerancemodular design

Page 38: Tech Talk - Things I Learned at Scala Days 2013

Use case class for data modeltreat data as map

Page 39: Tech Talk - Things I Learned at Scala Days 2013

Use finagle for service modulesplay is not for REST APIwhy pay the cost of HTTP?statisticsseparate REST and Data layermodularity

Page 40: Tech Talk - Things I Learned at Scala Days 2013

Questions?