Java 8 in Action – Ready for lambdas, streams and functional-style programming? #bookreview


Java 8 in Action

Lambdas, streams, and functional-style programming

Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft

(Manning – paperback)

 Java 8, we were sometimes assured, would just be Java 7 with a few slick new (or past-due) features added.

Actually, now that it’s here, Java 8 represents “the biggest change to Java in the 18 years since Java 1.0 was released,” the three authors of this fine new book point out.

Of course, news of “big changes” seldom sits well with developers who have spent countless hours learning and getting comfortable with one particular version of a programming language.

And many coders and companies will continue sticking with Java 7 for a while longer, because it still works. But the adoption pace for Java 8 keeps picking up. So, to misquote an old sci-fi slogan, resistance soon will become somewhat futile.

Lambdas, streams, and functional-style programming capabilities are Java 8’s headline additions. And there are some other major and minor additions, as well, including default methods and a new Date and Time API.

Java 8 in Action does an excellent job of introducing these new capabilities, and the book offers many short code examples and other illustrations to show how to put the new Java 8 capabilities to work.

Indeed, short (and shorter!) code is one of the hallmarks of Java 8. “In Java 8 you can write more concise code that reads a lot closer to the problem statement,” the writers emphasize. To illustrate that point, they offer a five-line example of verbose Java 7 code and follow it with a one-line Java 8 code example that accomplishes the same thing. Other examples also drive home the coding efficiencies that Java 8 can offer.

Lambdas, also known as anonymous functions, enable you to skip writing method definitions that will only be used once. The authors note that “passing code is currently tedious and verbose in Java [meaning 7 and earlier]. Well, good news! Lambdas fix this problem: they let you pass code in a concise way. Lambdas technically don’t let you do anything you couldn’t do prior to Java 8. But you no longer have to write clumsy code using anonymous classes to benefit from behavior parameterization!”

The new Streams API makes it much easier to work with collections in Java and provides “a much different way to process data in comparison to the Collections API.” Using the Streams API, “you don’t need to think in terms of loops at all. The data processing happens internally inside the library.”

Meanwhile, if you are a diehard object-oriented programmer, you may be leery of the term “functional programming” and the notion of using functions as values. (“In practice, you can’t completely program in pure functional style in Java,” the authors note. Instead, you will learn how to write “functional-style programs” in which you hide the side effects.)

With Java 8, “two core ideas from functional programming…are now part of Java: using methods and lambdas as first-class values, and the idea that calls to functions or methods can be efficiently and safely executed in parallel in the absence of mutable shared state. Both of these ideas are exploited by the new Streams API,” the writers state.  Also, in Java 8, they add, “there’s an Optional class that, if used consistently can help you avoid NullPointer exceptions.”

This review barely dents the surface of this excellent how-to book’s contents. Whether you are learning Java now or you are a Java developer who wants to keep your coding skills up-to-date and sharp, Java 8 in Action should be a book you will read soon.

Si Dunn

The Well-Grounded Rubyist, 2nd Edition – A solid, well-written, updated guide to the Ruby programming language – #bookreview



The Well-Grounded Rubyist

David A. Black

(Manning – paperback)

Ruby, predominately known as an object-oriented programming language, shows up frequently on lists of the top ten (or whatever) languages to know. And Ruby has long been paired with Rails to create the popular Ruby on Rails web application framework.

When the forerunner of this book appeared eight years ago, it was titled Ruby for Rails: Ruby Techniques for Rails Developers. And R4R, as it is sometimes known, was well received in both the Ruby and Rails camps.

In 2009, the R4R book was revised and retitled The Well-Grounded Rubyist. “This new edition is  a descendant of R4R but not exactly an update. It’s more of a repurposing,” the author, David A. Black, noted at the time. “The Well-Grounded Rubyist is a ‘just Ruby’ book, and it’s written to be read by anyone interested in Ruby.”

That focus continues in this second edition, which has been updated to cover Ruby 2.1. Ruby newcomers can get started and advance quickly with this fine “just Ruby” book in hand. Ruby veterans also can use it to gain new knowledge and sharpen familiar skills.

Black approaches the process of explaining Ruby “as a kind of widening spiral, building on the familiar but always opening out into the unknown.”

His well-written text does not try to be a “complete” language reference. Instead, reading The Well-Grounded Rubyist is like having a well-experienced and patient mentor close at hand–a mentor who willingly offers up clear examples and explanations. You likely will want to keep this book around as a go-to how-to reference long after you have learned and begun to work with Ruby.

It does help to have at least a little experience with programming before you tackle Ruby and this book. And, if you already have an older version of Ruby installed on your computer, upgrade it to 2.1.x. (As this review is being written, 2.1.2 is the current version.)

Yes, Ruby can be used in several different programming paradigms, including functional and imperative. But The Well-Grounded Rubyist is essentially all-object-oriented-all-the-time in its approach.

“Ruby is an object-oriented language, and the sooner you dive into how Ruby handles objects, the better,” Black states. “Accordingly, objects will serve both as a way to bootstrap the discussion of the language (and your knowledge of it) and as a golden thread leading us to further topics and techniques.”

Si Dunn

Functional JavaScript – Applying functional techniques and Underscore.js – #programming #bookreview

Functional JavaScript
Introducing Functional Programming with Underscore.js
Michael Fogus
(O’Reilly – paperback, Kindle)

When I first started tinkering with JavaScript, object-oriented programming (OOP) was the rage, and JavaScript frequently was touted as one of the key object-oriented languages. After all, according to many online descriptions, almost everything within JavaScript “is OO.”

Now, in this enjoyable new book, JavaScript is hailed as a functional programming language. Of course, functional programming recently has been the rage in many programming circles. And “enjoyable” may seem an odd word to attach to a software text. Yet, it fits. Michael Fogus shows here that he is a technical writer who can be entertaining, effective and well-organized.

“This book,” he states, “is not about spewing dogma regarding the virtues of functional programming. I think there are many functional techniques that offer ways to rein in the complexities of software development, but I realize that at times, there are better ways to implement any given individual part.”

It is possible, of course, to debate object-oriented vs. functional JavaScript until the digital cows are called back home and put into infinite loops. But, for those who don’t know or care much about the differences, here are some basic views.

The Mozilla Developers Network (to simply pick  one example) discusses objected-oriented JavaScript on its site and declares: “Object-oriented programming may be seen as the design of software using a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a collection of functions, or simply as a list of instructions to the computer. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent little machine with a distinct role or responsibility.”

Meanwhile, HaskellWiki offers this explanation of functional programming: “In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state.

“Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function. Being first-class also means that it is possible to define and manipulate functions from within other functions.”

Some of the programming languages typically labeled “functional” include Clojure, OCaml, Erlang,  Haskell, Scala, and F#.

Here is how Michael Fogus defines functional programming:

“As a bare-bones introduction, functional programming can be described in a single sentence:

Functional programming is the use of functions that transform values into units of
abstraction, subsequently used to build software systems.

“This is a simplification bordering on libel,” he continues, “but it’s functional (ha!) for this early stage in the book. The library that I use as my medium of functional expression in JavaScript is Underscore, and for the most part, it adheres to this basic definition.”

(Underscore.js can be obtained from and imported into “the applicable project directories.”)

Fogus refuses, in his text, “to create a false dichotomy and say that functional and object-oriented styles should stand in opposition.” Indeed, he notes that JavaScript supports both models and “systems can and should be composed of both models.”

He also points out that JavaScript can be used with other paradigms, including imperative programming, prototype-based object-oriented programming, and metaprogramming.

“In no way does this book represent even a modicum of original thinking regarding functional programming in JavaScript,” he states. Yet, it is a worthy effort.

It is well written, smoothly organized, and nicely illustrated with short code examples and helpful diagrams. And Fogus notes that JavaScript does have “[l]anguage oddities, unsafe features, and a sea of competing libraries” that raise concerns when it is selected for a project. Functional programming, he contends, can be one way to help ensure that JavaScript “can be not only safe, but also simple to understand and test, in addition to being proportionally scalable to the size of the code base.”

Here is the chapter lineup for Fogus’s 237-page book:

  • Chapter 1. Introducing Functional JavaScript
  • Chapter 2. First-Class Functions and Applicative Programming
  • Chapter 3. Variable Scope and Closures
  • Chapter 4. Higher-Order Functions
  • Chapter 5. Function-Building Functions
  • Chapter 6. Recursion
  • Chapter 7. Purity, Immutability, and Policies for Change
  • Chapter 8. Flow-Based Programming
  • Chapter 9. Programming without Class

Functional JavaScript is a winner on at least two counts: (1) as a how-to text for long-time JavaScript programmers wishing to learn more about functional programming; and (2) as a how-to text for long-time functional programmers desiring to learn more about JavaScript.

The book is not recommended for JavaScript newcomers who are still at the level of typing console.log(“The answer to everything in the universe is 42”). However, experienced beginners can learn from it, and so can those of us who just tinker with JavaScript periodically and use it mainly to work with Node.js, Backbone.js, Ember.js, CoffeeScript, HTML and other choices.

Si Dunn

Four good books that can help boost your JavaScript skills – #programming #bookreview

Ready for some enlightenment that can boost your JavaScript programming skills?

O’Reilly recently has published four books that can help you move from basic JavaScript library user to confident, experienced developer. 

“JavaScript started out as a simple and approachable front-end scripting language,” the publisher notes. “It has matured into a true cross-platform environment targeted by the latest emerging languages, frameworks, and developer tools.” The four new JavaScript books can help you “[l]earn how you can get the ultimate in responsiveness and interactivity from JavaScript, whether you use it on the front-end or server-side.” 

The four books are: JavaScript Enlightenment and DOM Enlightenment, both by Cody Lindley; Learning from jQuery by Callum Macrae; and Testable JavaScript by Mark Ethan Trostler.


JavaScript Enlightenment
Cody Lindley
(O’Reilly – paperback, Kindle)

Short, clear code samples are the stars of this fine, informative book. And most of the code samples can be viewed, executed and modified online using file links provided for the website.

The book’s goal is “to give the reader an accurate JavaScript worldview through an examination of native JavaScript objects and supporting nuances: complex values, primitive values, scope, inheritance, the head object, etc.” Cody Lindley adds: “I intend this book to be a short and digestible summary of the ECMAScript 3 Edition specification, focused on the nature of objects in JavaScript.”

Lindley keeps that promise in his 147-page book. His code samples rarely span more than a half page, and his explanatory paragraphs also are taut and to the point.

For example: “In JavaScript, objects are king: Almost everything is an object or acts like an object. Understand objects and you will understand JavaScript. So let’s examine the creating of objects in JavaScript….An object is just a container for a collection of named values (a.k.a properties).” 

Lindley’s book covers six of the nine native object constructors that are pre-packaged with JavaScript. The six are: Number(); String(); Boolean(); Object(); Array(); and Function(). He skips Date(), Error(), and RegEx() “because, as useful as they are, grasping the details of these objects will not make or break your general understanding of objects in JavaScript.” But he does hope you will learn them later, on your own. 

“JavaScript,” he writes, is mostly constructed from just these nine objects (as well as string, number, and boolean primitive values.) Understanding these objects in detail is key to taking advantage of JavaScript’s unique programming power and language flexibility.”


DOM Enlightenment
Cody Lindley
(O’Reilly – paperback, Kindle)

If you work with JavaScript, you probably rely on a Document Object Model (DOM) library such as jQuery to help you handle HTML scripting. 

But you can script the DOM without a DOM library, using JavaScript. Cody Lindley shows how in this excellent guide aimed at two types of developers who have experience with JavaScript, HTML, and CSS.

“The first developer is someone who has a good handle on JavaScript or jQuery, but has really never taken the time to understand the purpose and value of a library like jQuery,” Lindley writes. “The second type of developer is an engineer who is tasked with scripting HTML documents that will only run in modern browsers or that will get ported to native code for multiple OSes and device distributions (e.g., PhoneGap) and needs to avoid the overhead (i.e., size or size versus use) of a library.”

He notes that “HTML documents get parsed by a browser and converted into a tree structure of node objects representing a live document. The purpose of the DOM is to provide a programmatic interface for scripting (removing, adding, replacing, eventing, and modifying) this live document.”

Much of his 161-page DOM Enlightenment  focuses on how to work in JavaScript with “the most common types of nodes…one encounters when working with HTML documents.” He purposefully has “left out any details pertaining to XML or XHTML.” And, to help keep the book small, he has “purposely excluded the form and table APIs,” but adds: “I can see these sections being added in the future.”

Lindley also imposes a key technical limitation on the “content and code in this book….” It was, he says, “written with modern browsers (IE9+, Firefox latest, Chrome latest, Safari latest, Opera latest) in mind.”  

In keeping with the goals of O’Reilly’s Enlightenment series, explanations are short and concise and code examples are kept small. Also, the code examples are available online and can be displayed, run, and modified at the website.

 Cody Lindley emphasizes that he is “not promoting the idea of only going native when it comes to DOM scripting….” He hopes, instead,  “that developers may realize that DOM libraries are not always required when scripting the DOM.”


Learning from jQuery
Callum Macrae
(O’Reilly – paperback, Kindle)

Some developers work comfortably with jQuery yet have only a modest understanding of JavaScript.

Callum Macrae’s concise, well-written new book is intended to help fill that gap. It is “targeted at developers who know jQuery, but who don’t feel comfortable in their JavaScript knowledge or would just like to know more.”

The 102-page book focuses on the JavaScript code that jQuery covers up. It offers five chapters and two appendixes, with many short code examples and other illustrations. Much of the code is available through a GitHub repo.

Chapter 1, “Event Handling,” explains how event handling works in JavaScript and notes that “[e]vents are the heart of pretty much all web applications….jQuery provides a suite of functions to make event handling considerably easier than in JavaScript alone.” But these functions “add overhead and remove control from you, the developer. For this reason, it is important to know how you can handle events without jQuery in pure JavaScript.”

Chapter 2 covers “Constructors and Prototypes.” Writes Macrae: “Constructors are a way of creating objects, and can be initiated via the new keyword. Prototypes are one of the more powerful features of JavaScript, and allow the developer to declare a method or property that all instances of an object will inherit.” The chapter also can “help you understand how jQuery works, as jQuery itself uses prototypes.” 

Chapter 3 deals with “DOM Traversal and Manipulation.” Macrae notes that “jQuery includes a number of functions that make working with the DOM a lot easier than with JavaScript alone, which can be pretty ugly. However, the functions provided by jQuery can be rather hefty (especially in older browsers), and it is often a lot faster to just use pure JavaScript. Therefore, it is important to know how to work both.”

Chapter 4, “AJAX,” covers jQuery’s AJAX functions and concedes that they “offer some significant improvements over the native JavaScript AJAX features, as they are a lot easier to use.” Macrae explains: “AJAX is the act of making an HTTP request from JavaScript without having to reload the page; you could think of it as an inline HTTP request.” The chapter shows some jQuery AJAX requests and how those AJAX requests are sent in JavaScript. The goal is to help you get better at debugging code and also realize that “it isn’t worth loading the entire jQuery library to send a few requests and nothing else….”

Chapter 5, “JavaScript Conventions,” explains some “common conventions that you can use to improve your JavaScript…such as making your code more readable by using comments and whitespace correctly, optimizing your code in order to improve performance, design patterns, and some common antipatterns (code that causes more problems than it solves.)”

 This book is not recommended for persons who have no jQuery or JavaScript experience. Still, Appendix A, “JavaScript Basics,” provides a 28-page introduction to JavaScript, starting at “Hello World!” Appendix B, meanwhile, describes several applications and websites that can help you improve your JavaScript knowledge.


Testable JavaScript
Mark Ethan Trostler
(O’Reilly – paperback, Kindle)

“You have to test your code,” Mark Ethan Trostler emphasizes, “so why not make the process as easy and painless as possible?”

That’s a very desirable goal. Yet, as he notes a few sentences later, “testing–especially JavaScript testing–is complicated.”

For example: “Client-side JavaScript is especially difficult to test properly, as we have very little control over the environment within which our code runs. Multiple operating systems, multiple versions of operating systems, multiple browsers, multiple versions of browsers, not to mention plug-ins, extensions, different languages, zoom levels, and who knows what else, all conspire to hinder the performance of our applications. These permutations slow down, break, crash, and eat our applications for lunch. It’s a jungle out there!”

Trostler, a software engineer who works in test at Google, says his book “attempts to bridge the gap between sane development practices and JavaScript. JavaScript is a weird little language.” And he has aimed his guide at “people who encounter JavaScript professionally. Beginning, intermediate, or guru-level developers are all welcome, as this book has something for everyone.”

His 250-page how-to guide is structured into eight chapters that “tackle testable code in several steps. First we will investigate complexity. Then we will look at an architecture choice that attempts to limit complexity and coupling. With that as our foundation,” Trostler continues, “we will move on to testing, both at the functional level and at the application level.” From there, he delves into: code coverage; integration, performance, and load testing; debugging; and using automation in tests.

 “Writing unit tests for client-side JavaScript can be daunting,” Trostler states. “That means too many people don’t do it. This is not OK…”

Testable JavaScript is well written and rich with code examples, screenshots, diagrams and other illustrations. Whether you write client-side or server-side JavaScript — or both — or you are trying to rework some legacy files, Mark Ethan Trostler’s text can help you learn how to better create and maintain testable code.

Si Dunn

Intermediate Perl, 2nd Edition – An excellent guide to pushing well beyond the basics – #programming #bookreview

Intermediate Perl, 2nd Edition
Randal L. Schwartz, brian d foy & Tom Phoenix
(O’Reilly, paperback)

Attention, Perl programmers. Particularly those of you who write Perl programs with 100 lines of code or fewer but want to expand your limits. This popular intermediate guide, first published in 2006, has just been updated.

Intermediate Perl, 2nd Edition covers Perl 5.14. And: “It covers what you need to write programs that are 100 to 10,000 (or even longer) lines long,” the authors state.

This excellent book by three well-known Perl gurus does indeed cover a lot of ground. It shows you, for example, “how to work with multiple programmers on the same project by writing reusable Perl modules that you can wrap in distributions usable by the common Perl tools.”

It also shows you “how to deal with larger and more complex data structures….”

And it gets into some “object-oriented programming, which allows parts of your code (or hopefully code from others) to be reused with minor or major variations within the same program.”

It delves into two other important aspects of team programming: “…having a release cycle and a process for unit and integration testing. You’ll learn the basics of packaging your code as a distribution and providing unit tests for that distribution, both for development and for verifying that your code works in your target environment.”

One very important addition in the new edition is a chapter on references. “References,” the authors emphasize, “are the basis for complex data structures, object-oriented programming, and fancy subroutine handling. They’re the magic that was added between Perl versions 4 and 5 to make it all possible…” to handle “complex data interrelationships.”

The authors write in a lighthearted style that helps the coding medicine go down. And there are plenty of code examples and illustrations, plus a link to a website with downloads. They also provide exercises at the ends of chapters, with suggested completion times in minutes.

“If you take longer,” they add, “that’s just fine, at least until we figure out how to make ebooks with timers.”

However, if you take longer than “longer” or if you just get stumped, the answers conveniently are provided at the back of Intermediate Perl, 2nd Edition.

Si Dunn

Oh, Say Can You C#? – C# 5.0 in a Nutshell & C# 5.0 Pocket Reference -#programming #bookreview

O’Reilly recently has released two handy and helpful books for practitioners and students of the C# object-oriented programming language. One is a hefty, 1042-page “definitive reference” that (in paperback format) can double as a handy weight for physical exercise. The other is a compact, 215-page reference that really does fit in a coat pocket or pants pocket (but not a typical shirt pocket, unless you happen to wear an XXL, or larger, shirt).

Starting first with the muscle-building reference guide that weighs in at nearly three pounds…

C# 5.0 in a Nutshell, 5th Edition
Joseph Albahari and Ben Albahari
(O’Reilly, paperback, list price $49.99; Kindle edition, list price $39.99)

This latest Nutshell edition covers C# 5.0, “the fourth major update to Microsoft’s flagship programming language, an update that positions C# “as a language with unusual flexibility and breadth,” the authors state.

They note: “At one end, it [C#] offers high-level abstractions, such as query expressions and asynchronous continuations, while at the other end, it provides low-level power through constructions such as custom value types and the optional use of pointers.”

The revised and expanded new edition “covers C#, the CLR [Common Language Runtime], and the core Framework assemblies. The authors have chosen this focus “to allow space for difficult topics such as concurrency, security and application domains—without compromising depth or readability.” It’s hard to argue with the “depth” of a book 1,000+ pages long. And the book is written clearly, with numerous short code examples to illustrate its points.

C# 5.0 in a Nutshell is aimed at readers with intermediate to expert knowledge of programming, but no prior knowledge of C# is assumed. Indeed, chapters two through four provide an introduction to C# basics, starting just above the “Hello, World” level with a program that multiplies 12 times 30. The remaining chapters cover advanced C# 5.0 topics and the core .NET framework. With just a few exceptions, you can read these chapters randomly.

The hefty book has a hefty index. It has been “shaped by more than 20 expert reviewers,” including several from Microsoft. And the authors have solid backgrounds in their subject matter.

And now, for something a bit lighter…

C# 5.0 Pocket Reference
Joseph Albahari and Ben Albahari
(O’Reilly, paperback, list price $19.99; Kindle edition, list price $11.99)

The Nutshell authors have boiled their big book down to a very practical aid that you can easily carry to work, class or your favorite coffee shop or when traveling. Conveniently, the Pocket Reference still includes an introduction to C# 5.0’s fundamentals, starting with the simple multiplication program. It also covers many advanced topics, ranging from operator overload and Language Integrated Query (LINQ) to preprocessor directives, custom attributes, and XML documentation.

This book is not organized by chapters, so you will find yourself making frequent use of the book’s two-page table of contents and 12-page index, particularly if you are new to C# 5.0.

But you can readily find brief explanations, code samples and illustrations that define and clarify much of what you are seeking. And you will appreciate the book’s convenient size.

Si Dunn

Programming Clojure, 2nd Edition – The virtues of concurrency and functional style – #bookreview #in #programming

Programming Clojure, 2nd Edition
Stuart Halloway and Aaron Bedra
(Pragmatic Bookshelf,
paperback, list price $35.00)

Clojure is yet another computer programming language with an odd name and many followers.

For example, Dr. Venkat Subramaniam, an award-winning author and founder of Agile Developer, Inc., has called Clojure “a beautiful, elegant, and very powerful language on the JVM [Java Virtual Machine].”

A lot has happened to Clojure (pronounced like “closure”) since the first edition of this book was published in 2009. Clojure has been updated several times and gained some enhancements, a lot of new followers, and many libraries.

Programming Clojure, 2nd Edition has been rewritten and reorganized to cover these new features, concepts and developments.

“Clojure is a powerful, general-purpose programming language,” the authors note. But this is not a book for raw beginners, even though its first chapter does start at the traditional “Hello, world” level. It is intended for “experienced programmers looking for power and elegance.” You should have some experience with C#, Java, Python, or Ruby before tackling Clojure.

“Clojure is built on top of the Java Virtual Machine, and it is fast,” Halloway and Bedra emphasize. “This book will be of interest of Java programmers who want the expressiveness of a dynamic language without compromising on performance.”

They add: “If you program in Lisp, use a functional language such as Haskell, or write explicitly concurrent programs, you will enjoy Clojure.”

Indeed, Clojure’s creator, Rick Hickey, emphasizes the language’s value as an alternative to the “complexity” introduced in object-oriented programming. “Object-oriented programming seems easy,” he states, “but the programs it yields can often be complex webs of interconnected mutable objects.”

He continues: “Functional programming offers an alternative. By emphasizing pure functions that take and return immutable values, it makes side effects the exception rather than the norm. Clojure is designed to make functional programming approachable and practical for commercial software developers.”

Clojure also makes concurrent programming easier. “Many languages build a concurrency model around locking, which is difficult to use correctly,” the authors point out. “Clojure provides several alternatives to locking: software transactional memory, agents, atoms, and dynamic variables.”

The 268-page book is well-organized and well written, and it offers numerous practical code examples. The book also has been reviewed for technical accuracy by a panel of Clojure practitioners.

Clojure code, incidentally, tends to be short (but you do need an editor that can “at least indent code correctly and can match parentheses”).

In one of the book’s comparison examples, the function produced by a 14-line sample of Java code is duplicated in Clojure with just two lines. So development in Clojure potentially can be fast.

Si Dunn