Thanks to Packt Publishing I have got a free ebook copy of „Scala for Java Developers„.
Preface
All Java code samples are covered by the features of the Java language specification whereas the upcoming functional extensions of Java 8 are not part of the discourse.
Review / Summary
The book “Scala for Java Developers” by Thomas Alexandre is an introduction to the Scala language from a Java Developers perspective. It does not explain basic programming paradigms like object orientation beside a very small wrap up of the functional paradigm as it might not be well known to every Java coder. The book is organized into 10 sections which should be read in order and mostly build upon each other.
The first chapter motivates the reader why someone should consider using Scala for the next project by presenting features and syntactical sugar for the easy expression of common programming problems. Fairly someone has to admit that most of the functional programming features are now more easily usable with the current Java 8, but as time of writing Java 7 was the stable version. Furthermore the syntax being claimed as concise and easily readable might frighten some of us more verbose Java developers. It is worth to note that there are often different ways of expressing the same statement which might be more familiar to Scala newbies or switchers from the Java universe.
Starting with a common example of filtering elements from a list scoring with a clean one liner compared to a ‚verbose‘ Java program for the same task. Using Java 8 language features in this comparison there will be no clear winner:
Arrays.asList(1, 2, 3, 4, 5).stream().filter(element -> element < 4);
While broadening the view on more Scala features ‑like advanced collection processing via higher order functions or pattern matching ‑ the great potential of Scala is shown. In addition the reader is introduced into the REPL (Read Eval Print Loop) tool and its usage which is a comfortable way of writing down or testing your ideas getting immediate feedback.
The second chapter starts off with an actual scenario which might be suitable to introduce Scala to your existing code base, by writing test case for existing business logic in Scala instead of well known Java. Furthermore it covers some key points of building a successful cooperation between Java and Scala code blocks. It puts the focus in the collaboration of Scala and Java code covering the Bean Pattern, List conversions, class design and interface / traits usage, exception handling and more advanced topics like companion objects. It is also shown how to set up your Maven build appropriately and concludes with some overall style hints.
The next chapter is dedicated to the Scala Eco-system, beside your known tools from the Java universe there are a lot of similar more Scala‑ish tools. Afterwards the reader would be able to set up a mature development environment including Maven-like project models, dependency management as well as generators for different IDE templates. The handling of SBT and the declaration of dependencies will be the foundation for the samples of the following chapter. The most important point for Java Developers would probably be that SBT incorporates existing maven repositories. This will allow for a soft transition of existing projects and will not produce blockers simply caused by a technology or library which seems not mature enough or simple not available as a pure Scala solution.
Testing is the main topic of the 4th chapter. The author presents libraries to assist the user in writing tests for different style like BDD (Behavior Driven Design) or TDD (Test Driven Design) along with instructions for build mocks where needed. One interesting aspect is the conception to ScalaCheck which provides properties based testing facilities. You specify some assumptions (properties) which should hold for every case and ScalaCheck will execute test provided with randomly generated input values.
Chapter 5 moves forward to the Play framework, which is a kind of ’new‘ web framework available both for Java and Scala. The reader will set up a simple play application using the Typesafe Activator. This goes along with an introduction into the architectural features of a Play application.
The following chapter deals with the entire topic of data persistence. It starts with picking up the reader on how to integrate JPA into your Scala code base moving on to pure Scala solutions like Anorm as an ORM. Showing up the Object-Relational Impedance Mismatch it is suggested to replace the ORM with the Slick framework a more functional approach to database communication.
The next topic covers Web Services whereas both SOAP based and REST based services taken into consideration. Therefore the author introduces XML binding and all libraries required to communicate with SOAP services. He points out that XML is already integrated as a language construct into Scala, the same applies to JSON which will be focused later on. The section is finished by setting up services in a Play project and writing appropriate test code which also includes mocking techniques to abstract from the physical service availability.
The following two chapters are dealing with the topic of asynchrony and concurrency. The reader gets introduced to Scalas basic language constructs and patterns for asynchrony and concurrencies. This includes a discussion about the usage and combination of Futures and Promises. Finally the discourse leads to the Akka framework which might be a novel approach to concurrencies for the traditional Java Developer although it has to be noted that Akka is available for Java too and the actor approach is known since 1973. The Akka framework features the idea of Actors to break down work into several independent tasks. It is the foundation of the Play framework which allows writing reactive web applications easily. The author introduces the stream concept to consume data without blocking the application. The section is closed with some small example applications illustrating the power of the reactive approach.
The last chapter is dedicated to some Scala goodies like working with MongoDB, creating DSLs (Domain Specific Languages) and Scala.js which compiles Scala code to Javascript.
Verdict
The book is an interesting introduction to the Scala language for the profound Java developer. The author encourages the reader to experiment with Scala and explore an alternative programming style. He shows up new ways of problem solutions and might inspire some developers to catch up some ideas and put them into real in their projects, even if it is not a Scala code base.