Software Engineering Thailand: Programming with Scala
-
Upload
brian-topping -
Category
Software
-
view
340 -
download
2
Transcript of Software Engineering Thailand: Programming with Scala
Programming with ScalaBrian Topping
Mauswerks LLC
About Me• Globally experienced dev with over 25 years professional experience
• Have led teams of three to fifteen developers as lead developer, architect, general manager. Start projects first, then hire when ten fingers are not enough.
• Have been using Scala since 2012, JDK Open Source since 2001
• Interests: Functional Reactive Programming, financial markets, machine learning, Javascript, travel, motorcycles
• Shameless plug: Available for investment or work in BKK
• http://github.com/briantoppinghttp://twitter.com/briantoppinghttp://linkedin.com/in/briantopping
Why Scala?
What is wrong with this picture?
• "Looks good to me, ship it!"
• "Looks good to me, ship it!"
• We're at the wrong level of zoom to see problems
• "Looks good to me, ship it!"
• We're at the wrong level of zoom to see problems
•
• "Looks good to me, ship it!"
• We're at the wrong level of zoom to see problems
•
• Solutions at a global level must factor problems at the local level.
• "Looks good to me, ship it!"
• We're at the wrong level of zoom to see problems
•
• Solutions at a global level must factor problems at the local level.
• Complexity is the natural tradeoff
What is Scala?
• Object Oriented / Functional hybrid
• Compiled to JVM bytecode
• Statically typed (compile time)
• Strongly typed (types must match)
• Growing ecosystem
• Language is stable and mature
(a few) Benefits of Scala
• Seamless Java / JDK ecosystem interoperability
• Type Inferencing & Parameterization
• Concurrency & Distribution
• Collections Classes
• Pattern Matching
• Obvious Correctness
Standard Example
Standard Example
• Java "Bean"
Standard Example
• Java "Bean"
Standard Example
• Java "Bean"
• Scala Case Class
Standard Example
• Java "Bean"
• Scala Case Class
Standard Example
• Java "Bean"
• Scala Case Class
Java / JDK Interoperability
Java / JDK Interoperability
• Scala classes are JVM classes
Java / JDK Interoperability
• Scala classes are JVM classes
• Groovy, Clojure, JRuby, Jython – 40+ in all!
Java / JDK Interoperability
• Scala classes are JVM classes
• Groovy, Clojure, JRuby, Jython – 40+ in all!
• Scala is a mixed Object + Functional language
Java / JDK Interoperability
• Scala classes are JVM classes
• Groovy, Clojure, JRuby, Jython – 40+ in all!
• Scala is a mixed Object + Functional language
• It is possible and common for young teams to start writing Scala with Java style (OOD) and API, test with existing tools
Java / JDK Interoperability
• Scala classes are JVM classes
• Groovy, Clojure, JRuby, Jython – 40+ in all!
• Scala is a mixed Object + Functional language
• It is possible and common for young teams to start writing Scala with Java style (OOD) and API, test with existing tools
• Take advantage of Java libraries
Java / JDK Interoperability
• Scala classes are JVM classes
• Groovy, Clojure, JRuby, Jython – 40+ in all!
• Scala is a mixed Object + Functional language
• It is possible and common for young teams to start writing Scala with Java style (OOD) and API, test with existing tools
• Take advantage of Java libraries
Type Inference & Immutability Focus
Type Inference & Immutability Focus
• Compiler knows the type of an assignment, yet most languages require the developer to prove it
Type Inference & Immutability Focus
• Compiler knows the type of an assignment, yet most languages require the developer to prove it
• Java:
Type Inference & Immutability Focus
• Compiler knows the type of an assignment, yet most languages require the developer to prove it
• Java:
• Scala:
Type Inference & Immutability Focus
• Compiler knows the type of an assignment, yet most languages require the developer to prove it
• Java:
• Scala:
• Built for immutability!
Type Inference & Immutability Focus
• Compiler knows the type of an assignment, yet most languages require the developer to prove it
• Java:
• Scala:
• Built for immutability!
• Use the var keyword for mutable variables
Type Inference & Immutability Focus
• Compiler knows the type of an assignment, yet most languages require the developer to prove it
• Java:
• Scala:
• Built for immutability!
• Use the var keyword for mutable variables
• One finds themselves using it less and less often!
Type Parameterization
Type Parameterization• Types allow you to denote function domain & codomains. For example, from
mathematics, we are used to seeing:this tells us that function “f” maps values from the set of real numbers to values of the set of natural numbers
Type Parameterization• Types allow you to denote function domain & codomains. For example, from
mathematics, we are used to seeing:this tells us that function “f” maps values from the set of real numbers to values of the set of natural numbers
• In the abstract, this is exactly what concrete types are. Type systems give us some more powerful ways to express these sets
Type Parameterization• Types allow you to denote function domain & codomains. For example, from
mathematics, we are used to seeing:this tells us that function “f” maps values from the set of real numbers to values of the set of natural numbers
• In the abstract, this is exactly what concrete types are. Type systems give us some more powerful ways to express these sets
• Given these annotations, the compiler can now statically (at compile time) verify that the program is sound
Type Parameterization• Types allow you to denote function domain & codomains. For example, from
mathematics, we are used to seeing:this tells us that function “f” maps values from the set of real numbers to values of the set of natural numbers
• In the abstract, this is exactly what concrete types are. Type systems give us some more powerful ways to express these sets
• Given these annotations, the compiler can now statically (at compile time) verify that the program is sound
• Variance annotations allow you to express relationships between class hierarchies & polymorphic types
Type Parameterization• Types allow you to denote function domain & codomains. For example, from
mathematics, we are used to seeing:this tells us that function “f” maps values from the set of real numbers to values of the set of natural numbers
• In the abstract, this is exactly what concrete types are. Type systems give us some more powerful ways to express these sets
• Given these annotations, the compiler can now statically (at compile time) verify that the program is sound
• Variance annotations allow you to express relationships between class hierarchies & polymorphic types
• Reference: http://twitter.github.io/scala_school/type-basics.html
Concurrency & Distribution
Concurrency & Distribution• Futures and Promises – Long history across platforms
Concurrency & Distribution• Futures and Promises – Long history across platforms
• Factory objects look like integral part of language
Concurrency & Distribution• Futures and Promises – Long history across platforms
• Factory objects look like integral part of language
• Remember that functions are parameters!
Concurrency & Distribution• Futures and Promises – Long history across platforms
• Factory objects look like integral part of language
• Remember that functions are parameters!
•
Concurrency & Distribution• Futures and Promises – Long history across platforms
• Factory objects look like integral part of language
• Remember that functions are parameters!
•
• Don't return values, return a Future!
Concurrency & Distribution• Futures and Promises – Long history across platforms
• Factory objects look like integral part of language
• Remember that functions are parameters!
•
• Don't return values, return a Future!
• Akka-HTTP is a perfect example, you can return a value, but then your code manages the concurrency. Return a Future to the caller and let it manage it instead!
Concurrency & Distribution• Futures and Promises – Long history across platforms
• Factory objects look like integral part of language
• Remember that functions are parameters!
•
• Don't return values, return a Future!
• Akka-HTTP is a perfect example, you can return a value, but then your code manages the concurrency. Return a Future to the caller and let it manage it instead!
• Unlike Node, full access to all cores.
Collections Classes
Collections Classes• Scala Collections are what brought me to the language
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
• Immutable by default, great for parallel deployments
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
• Immutable by default, great for parallel deployments
• Java
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
• Immutable by default, great for parallel deployments
• Java
•
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
• Immutable by default, great for parallel deployments
• Java
•
• Scala
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
• Immutable by default, great for parallel deployments
• Java
•
• Scala
•
Collections Classes• Scala Collections are what brought me to the language
• Designed to consistently behave as a collections DSL
• Immutable by default, great for parallel deployments
• Java
•
• Scala
•
• Don't get the wrong idea here! Rolling a one-liner into a println is not what I'd call "good style". Instead, note how the functional transparency and type inferencing allows us to inline functions as easily as we can assign them to variables.
Pattern Matching
Pattern Matching
• Partial Functions – A function that is only defined for specific input values
Pattern Matching
• Partial Functions – A function that is only defined for specific input values
•
Pattern Matching
• Partial Functions – A function that is only defined for specific input values
•
• Regular Expressions – Please enjoy them now! 😉
Pattern Matching
• Partial Functions – A function that is only defined for specific input values
•
• Regular Expressions – Please enjoy them now! 😉
•
Obvious Correctness
Obvious Correctness• Trick question: Which of these is more obviously correct?
Obvious Correctness• Trick question: Which of these is more obviously correct?
• Java:
Obvious Correctness• Trick question: Which of these is more obviously correct?
• Java:
• Scala:
Obvious Correctness• Trick question: Which of these is more obviously correct?
• Java:
• Scala:
• Depends on experience!!
Team Evolution w/ Scala
Team Evolution w/ Scala
• Start with this:
Team Evolution w/ Scala
• Start with this:
• To get to this:
Team Evolution w/ Scala
• Start with this:
• To get to this:
• Make the transition gradually! Do it together as a team!
What does all this buy us?
What does all this buy us?• Pure functions have no side effects... data in, data out
What does all this buy us?• Pure functions have no side effects... data in, data out
• Objects maintain state. This is why Scala is not considered a purely functional language!
What does all this buy us?• Pure functions have no side effects... data in, data out
• Objects maintain state. This is why Scala is not considered a purely functional language!
• When objects are immutable though, the state acts as constants in the functions. Constants are thread-safe!
What does all this buy us?• Pure functions have no side effects... data in, data out
• Objects maintain state. This is why Scala is not considered a purely functional language!
• When objects are immutable though, the state acts as constants in the functions. Constants are thread-safe!
• Immutable objects do not need to be locked. Locking is actually very expensive and error prone
What does all this buy us?• Pure functions have no side effects... data in, data out
• Objects maintain state. This is why Scala is not considered a purely functional language!
• When objects are immutable though, the state acts as constants in the functions. Constants are thread-safe!
• Immutable objects do not need to be locked. Locking is actually very expensive and error prone
• Immutable state is updated by replacing the object!
But it looks so complex!
But it looks so complex!• Scala actually has a smaller grammar than Java
But it looks so complex!• Scala actually has a smaller grammar than Java
• Functional composition (the ability for one function to feed another) allows intermediate variables to be removed.
But it looks so complex!• Scala actually has a smaller grammar than Java
• Functional composition (the ability for one function to feed another) allows intermediate variables to be removed.
• Scala ends up looking complex because developers collapse code that maybe shouldn't be collapsed.
But it looks so complex!• Scala actually has a smaller grammar than Java
• Functional composition (the ability for one function to feed another) allows intermediate variables to be removed.
• Scala ends up looking complex because developers collapse code that maybe shouldn't be collapsed.
• Program so your code is readable, not so it fits in as few lines as possible.
But it looks so complex!• Scala actually has a smaller grammar than Java
• Functional composition (the ability for one function to feed another) allows intermediate variables to be removed.
• Scala ends up looking complex because developers collapse code that maybe shouldn't be collapsed.
• Program so your code is readable, not so it fits in as few lines as possible.
• Occam's Razor: We should reduce to the simplest possible outcome, and no simpler!
Some things haven't changed
Some things haven't changed• TDD – Use it! Create unit tests for everything, use them as documentation
Some things haven't changed• TDD – Use it! Create unit tests for everything, use them as documentation
• One liners should have a simple comment describing what they do
Some things haven't changed• TDD – Use it! Create unit tests for everything, use them as documentation
• One liners should have a simple comment describing what they do
• Frequent code reviews! Pull requests that receive no comments are to be avoided.
Some things haven't changed• TDD – Use it! Create unit tests for everything, use them as documentation
• One liners should have a simple comment describing what they do
• Frequent code reviews! Pull requests that receive no comments are to be avoided.
• Avoid learning a new technique and immediately using it everywhere.
Some things haven't changed• TDD – Use it! Create unit tests for everything, use them as documentation
• One liners should have a simple comment describing what they do
• Frequent code reviews! Pull requests that receive no comments are to be avoided.
• Avoid learning a new technique and immediately using it everywhere.
• If you are the only person that can maintain your code, you will be cursed to own it forever.
Some things haven't changed• TDD – Use it! Create unit tests for everything, use them as documentation
• One liners should have a simple comment describing what they do
• Frequent code reviews! Pull requests that receive no comments are to be avoided.
• Avoid learning a new technique and immediately using it everywhere.
• If you are the only person that can maintain your code, you will be cursed to own it forever.
• If you can inspire others to read your code because it is both short and people "learn one new thing" (only one!), you will be able to move the team forward with new techniques, do more with less, have fun!
I'm not sold yet...
I'm not sold yet...
• Take Coursera's Scala class taught by Martin Odersky
I'm not sold yet...
• Take Coursera's Scala class taught by Martin Odersky
• You will become a better Scala programmer
I'm not sold yet...
• Take Coursera's Scala class taught by Martin Odersky
• You will become a better Scala programmer
• You will become a better Java programmer
I'm not sold yet...
• Take Coursera's Scala class taught by Martin Odersky
• You will become a better Scala programmer
• You will become a better Java programmer
• You will become a better programmer
I'm not sold yet...
• Take Coursera's Scala class taught by Martin Odersky
• You will become a better Scala programmer
• You will become a better Java programmer
• You will become a better programmer
• Apple's Swift language is a bit of a knockoff
I'm not sold yet...
• Take Coursera's Scala class taught by Martin Odersky
• You will become a better Scala programmer
• You will become a better Java programmer
• You will become a better programmer
• Apple's Swift language is a bit of a knockoff
• Get two platforms for the price of one!
How Do I Get Started?
IDEs: IntelliJ vs. Eclipse
IDEs: IntelliJ vs. Eclipse
IDEs: IntelliJ vs. Eclipse
IDEs: IntelliJ vs. Eclipse
• They both have advantages. Each team should "bake off" and doc the results
IDEs: IntelliJ vs. Eclipse
• They both have advantages. Each team should "bake off" and doc the results
• Don't be restricted to one, but a common denominator of skills is helpful
Testing
Testing• ScalaTest is standard
Testing• ScalaTest is standard
• Behavior Driven Development
Testing• ScalaTest is standard
• Behavior Driven Development
• Use with TDD, just works!
Testing• ScalaTest is standard
• Behavior Driven Development
• Use with TDD, just works!
• The power is in the Matchers
Development Environments
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
• scalac compiler can be run from command line like javac
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
• scalac compiler can be run from command line like javac
• SBT is Scala's own build tool, allows Scala code to be used within build, but not very IDE friendly.
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
• scalac compiler can be run from command line like javac
• SBT is Scala's own build tool, allows Scala code to be used within build, but not very IDE friendly.
• Maven has a Scala plugin, but comes with all of Maven's idiosyncrasies.
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
• scalac compiler can be run from command line like javac
• SBT is Scala's own build tool, allows Scala code to be used within build, but not very IDE friendly.
• Maven has a Scala plugin, but comes with all of Maven's idiosyncrasies.
• SBT can use Maven central repository, but is designed for Ivy repositories. Both end up being required, which is a pain point.
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
• scalac compiler can be run from command line like javac
• SBT is Scala's own build tool, allows Scala code to be used within build, but not very IDE friendly.
• Maven has a Scala plugin, but comes with all of Maven's idiosyncrasies.
• SBT can use Maven central repository, but is designed for Ivy repositories. Both end up being required, which is a pain point.
• All the cool kids use SBT, but it's not as mature and harder to use with projects that span multiple source repositories, also not as simple for tagged release environments.
Development Environments• REPL (Read-Eval-Print Loop) is an interactive shell
• scalac compiler can be run from command line like javac
• SBT is Scala's own build tool, allows Scala code to be used within build, but not very IDE friendly.
• Maven has a Scala plugin, but comes with all of Maven's idiosyncrasies.
• SBT can use Maven central repository, but is designed for Ivy repositories. Both end up being required, which is a pain point.
• All the cool kids use SBT, but it's not as mature and harder to use with projects that span multiple source repositories, also not as simple for tagged release environments.
• Verdict: If you already use Maven, stick with it, otherwise use SBT.
Concurrency & Distribution Part 2
Concurrency & Distribution Part 2
• Use Akka – http://akka.io
• Scala and Java APIs, Scala easier to use via immutables
Concurrency & Distribution Part 2
• Use Akka – http://akka.io
• Scala and Java APIs, Scala easier to use via immutables
• Message passing pattern without all the JMS overhead
Concurrency & Distribution Part 2
• Use Akka – http://akka.io
• Scala and Java APIs, Scala easier to use via immutables
• Message passing pattern without all the JMS overhead
• 50 million msg/sec on a single machine + small memory footprint; ~2.5 million actors (message endpoints) per GB of heap
Concurrency & Distribution Part 2
• Use Akka – http://akka.io
• Scala and Java APIs, Scala easier to use via immutables
• Message passing pattern without all the JMS overhead
• 50 million msg/sec on a single machine + small memory footprint; ~2.5 million actors (message endpoints) per GB of heap
• Remoting can scale to tens of thousands of machines
Concurrency & Distribution Part 2
• Use Akka – http://akka.io
• Scala and Java APIs, Scala easier to use via immutables
• Message passing pattern without all the JMS overhead
• 50 million msg/sec on a single machine + small memory footprint; ~2.5 million actors (message endpoints) per GB of heap
• Remoting can scale to tens of thousands of machines
• Akka Streams focuses on streaming messages, uses standard TCP facilities to throttle clients, avoid buffer overflows
Concurrency & Distribution Part 2
• Use Akka – http://akka.io
• Scala and Java APIs, Scala easier to use via immutables
• Message passing pattern without all the JMS overhead
• 50 million msg/sec on a single machine + small memory footprint; ~2.5 million actors (message endpoints) per GB of heap
• Remoting can scale to tens of thousands of machines
• Akka Streams focuses on streaming messages, uses standard TCP facilities to throttle clients, avoid buffer overflows
• 2014Q1 test reached 2400 nodes as well as starting up a 1000 node cluster in just over four minutes on Google App Engine.
Who's using it?
Who's using it?
Just about every major player has something in Scala:
• Apple
• Intel
• Agoda
• Airbnb
Who's using it?
Just about every major player has something in Scala:
• Apple
• Intel
• Agoda
• Airbnb
And any company using these scaling technologies:
• Apache Spark
• Apache Scalding
• Apache Kafka
• Twitter Finagle
• Typesafe Akka
Lessons?
• Every developer has the power to enlighten or obfuscate
• Every developer has the power to enlighten or obfuscate
• Complexity has many different measures at many different levels. The solution of least overall complexity often hides behind greater surface complexity!
• Every developer has the power to enlighten or obfuscate
• Complexity has many different measures at many different levels. The solution of least overall complexity often hides behind greater surface complexity!
• Every developer has the power to enlighten or obfuscate
• Complexity has many different measures at many different levels. The solution of least overall complexity often hides behind greater surface complexity!
• On first glance, Functional Programming looks difficult, but hundreds of years of proven advanced math are what built society – bridges, buildings, cars, computers
• Every developer has the power to enlighten or obfuscate
• Complexity has many different measures at many different levels. The solution of least overall complexity often hides behind greater surface complexity!
• On first glance, Functional Programming looks difficult, but hundreds of years of proven advanced math are what built society – bridges, buildings, cars, computers
• Why reinvent that wheel? Allow mathematical induction to help your efforts.
• Every developer has the power to enlighten or obfuscate
• Complexity has many different measures at many different levels. The solution of least overall complexity often hides behind greater surface complexity!
• On first glance, Functional Programming looks difficult, but hundreds of years of proven advanced math are what built society – bridges, buildings, cars, computers
• Why reinvent that wheel? Allow mathematical induction to help your efforts.
• Moore's Law has given us blessings, including the ability to use increasingly complex compilers.
References + Q&A• Scala Course:
https://www.coursera.org/course/progfun
• Please read chapter 1, available for free: http://manning.com/kuhn/
• Twitter's Scala School:http://twitter.github.io/scala_school/
• Scala Cheat Sheet : http://mbonaci.github.io/scala/
• Deeper insight:http://danielwestheide.com/scala/neophytes.html