Preface - Learning Scala (2015)

Learning Scala (2015)

Preface

Welcome to Learning Scala. In this book I will provide you with a comprehensive yet approachable introduction to the Scala programming language.

Who This Book Is For

This book is meant for developers who have worked in object-oriented languages such as Java, Ruby, or Python and are interested in improving their craft by learning Scala. Java developers will recognize the core object-oriented, static typing and generic collections in Scala. However, they may be challenged to switch to Scala’s more expressive and flexible syntax, and the use of immutable data and function literals to solve problems. Ruby and Python developers will be familiar with the use of function literals (aka closures or blocks) to work with collections, but may be challenged with its static, generic-supporting type system.

For these and any other developers who want to learn how to develop in the Scala programming language, this book provides an organized and examples-based guide that follows a gradual learning curve.

Why Write “Learning Scala”?

When I picked up Scala in early 2012, I found the process of learning the language was longer and more challenging than it ought to be. The available books on Scala did cover the core features of the language. However, I found it difficult to switch from Java to Scala’s unfamiliar syntax, its preference for immutable data structures, and its sheer extensibility. It took me several weeks to become comfortable writing new code, several months to fully understand other developers’ code, and up to a year to figure out the more advanced features of the language.

I chose to write this book so that future developers will have an easier time learning the language. Now, even using this book the process of learning Scala won’t be easy; picking up new skills is always going to be challenging, and learning a new language with an unfamiliar syntax and new methodologies is going to take dedication and lots of work. However, this book at least should make the process easier. Hopefully it will ensure that more developers than before will pick up Scala, and also become capable enough to work with it as their main language.

Why Learn Scala (or, Why Should You Read “Learning Scala”)?

I enjoy developing with Scala and highly recommend it to anyone writing server applications and other types of programs suitable for Java-like languages. If you are working in domains suitable for running the Java Virtual Machine such as web applications, services, jobs, or data processing, then I’ll certainly recommend that you try using Scala.

Here’s why you should take this advice and learn to develop in Scala.

Reason 1—Your Code Will Be Better

You will be able to start using functional programming techniques to stabilize your applications and reduce issues that arise from unintended side effects. By switching from mutable data structures to immutable data structures and from regular methods to pure functions that have no effect on their environment, your code will be safer, more stable, and much easier to comprehend.

Your code will also be simpler and more expressive. If you currently work in a dynamic language such as Python, Ruby, or JavaScript, you already are familiar with the benefits of using a short, expressive syntax, avoiding unnecessary punctuation, and condensing map, filter, and reduce operations to simple one-liners. If you are more familiar with statically typed languages like Java, C#, or C++, you’ll be able to shed explicit types, punctuation, and boilerplate code. You will also be able to pick up an expressive syntax rarely seen in other compiled languages.

Finally, your code will be strongly typed (even without specifying explicit types) and support both multiple inheritance and mixin capabilities. Also, any type incompatibilities will be caught before your code ever runs. Developers in statically typed languages will be familiar with the type safety and performance available in Scala. Those using dynamic languages will be able to drastically increase safety and performance while staying with an expressive language.

Reason 2—You’ll Be a Better Engineer

An engineer who can write short and expressive code (as one expects in Ruby or Python) while also delivering a type-safe and high-performance application (as one expects from Java or C++) would be considered both impressive and valuable. I am assuming that if you read this book and take up Scala programming you will be writing programs that have all of these benefits. You’ll be able to take full advantage of Scala’s functional programming features, deliver type-safe and expressive code, and be more productive than you have ever been.

Learning any new programming language is a worthwhile endeavor, because you’ll pick up new and different ways to approach problem solving and algorithm and data structure design, along with ways to express these new techniques in a foreign syntax. On top of this, taking up a functional programming language like Scala will help to shape how you view the concepts of data mutability, higher-order functions, and side effects, not only as new ideas but how they apply to your current coding work and designs. You may find that working with inline functions and static types are unnecessary for your current needs, but you’ll have some experience with their benefits and drawbacks. Plus, if it becomes possible to apply these features in a partial manner to your current language, such as the new lambda expression support in Java 8, you’ll be ready to handle them appropriately.

Reason 3—You’ll Be a Happier Engineer

This is admittedly a bold statement from someone you haven’t met and who shouldn’t presume to know what effect Scala development will have on your brain. I’ll only state that if your code proficiency improves to the point that you are easily writing code that works better, reads better, debugs better, and runs faster than before, and on top of all this takes less time to write, you’re going to be happier doing so.

Not that life is all about coding, of course. Nor does the work schedule of average software engineers involve more than half of their time spent actually writing code.

But that time spent writing code will be more fun, and you’ll be able to take more pride in your work. That should be reason enough to learn something new.

Why Learning Scala May Not Be for You

You should know that Scala has a reputation for being difficult to learn. The language combines two apparently conflicting software engineering paradigms: object-oriented programming and functional programming. This synergy will be surprising to newcomers and the resulting syntax takes some practice to pick up. Scala also has a sophisticated type system that enables custom typing declarations at a level rarely seen outside of academic languages. Ascertaining the syntax and utility of this type system will be challenging, especially if you do not have academic experience with abstract algebra or type theory.

If you do not have enough time to spend on reading this book and going through its exercises, or alternately prefer more challenging or theoretical routes to learning the language, then this book may not be suitable for you.

About the Syntax Notation in This Book

Here is an example of the syntax notation you’ll encounter in this book:

val <identifier>[: <type>] = <data>

This specific example is the definition of a value, a type of variable in Scala that cannot be reassigned. It uses my own informal notation for defining the Scala language’s syntax, one that can be easier to read than the traditional notations used to define languages but that comes at the cost of being less formal and precise.

Here is how this notation works:

§ Keywords and punctuation are printed normally as they would appear in source code.

§ Variable items, such as values, types, and literals, are surrounded by angular brackets (“<” and “>”).

§ Optional segments are surrounded by square brackets (“[” and “]”).

For example, in the preceding example “val” is a keyword, “identifier” and “data” are variable items that change with the context, and “type” is an optional item that (if specified) must be separated from the identifier by a colon (“:”).

I do suggest reading the formal Scala language specification in addition to this book. Although it uses a traditional syntax notation that may be difficult to learn, it is still invaluable for determining the exact syntax requirements of any given feature. The official title is The Scala Language Specification (Odersky, 2011), and you can find it either on the official Scala site or with a quick web search.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

TIP

This element signifies a tip or suggestion.

NOTE

This element signifies a general note.

WARNING

This element indicates a warning or caution.