Monday, December 7, 2015

Book Review: Learning JavaScript Data Structures and Algorithms

The Packt Publishing book Learning JavaScript Data Structures and Algorithms by Loiane Groner has the subtitle "Understand and implement classic data structures and algorithms using JavaScript." Learning JavaScript Data Structures and Algorithms has just under 200 substantive pages spanning ten chapters in the main book. There is also an additional Chapter 11 and an Appendix referenced in the Preface that can be downloaded as separate PDFs.

Preface

The Preface of Learning JavaScript Data Structures and Algorithms briefly describes the importance of data structures and algorithms in software development and provides brief descriptions of each of the book's eleven chapters and of the appendix. In the "What you need for this book" section, the Preface describes three development environments that the reader can choose from to use for examples in the book.

The "Who this book is for" section may be the most important of the Preface I include it here to provide a good idea of who Learning JavaScript Data Structures and Algorithms is best suited for.

This book is intended for students of Computer Science, people who are just starting their career in technology, and those who want to learn about data structures and algorithms with JavaScript. Some knowledge of programming logic is the only thing you need to know to start having fun with algorithms and JavaScript!
This book is written for beginners who want to learn about data structures and algorithms, and also for those who are already familiar with data structures and algorithms but who want to learn how to use them with JavaScript.

Chapter 1: JavaScript – A Quick Overview

The initial chapter of Learning JavaScript Data Structures and Algorithms asserts, "JavaScript is a must-have on your résumé if you are or going to become a web developer." The chapter explains why the author chose JavaScript as the language "to learn about data structures and algorithms."

Chapter 1 of Learning JavaScript Data Structures and Algorithms has a section "Setting up the environment" that describes using a browser such as Chrome or Firefox (with FireBug) as the simplest environment and demonstrates via screen snapshots use of either browser and its associated development tools. This section also describes installing XAMPP and node.js.

Chapter 1's section on "JavaScript basics" begins by describing and demonstrating the two ways (directly embedded and included in referenced file) to include JavaScript in HTML. The chapter then discusses and demonstrates variables and variable scope, types, operators, approaches to writing output, true and false in JavaScript (truthy and falsy), the equals operators, conditional statements, loops, functions, and objects.

The section "Debugging and tools" briefly references JavaScript debugging and recommends the Google Chrome Debugging JavaScript page. This section also recommends choosing an editor from three it lists and briefly describes: Aptana, JetBrains's WebStorm, and Sublime.

I really like Learning JavaScript Data Structure and Algorithms's introduction to JavaScript. The chapter presents JavaScript basics in a clear, concise manner. The author does a nice job of mixing commonly accepted "best practices" of JavaScript development in with the topics as they are introduced. For example, I really like the table that summarizes how true and false work in JavaScript.

Chapter 2: Arrays

Chapter 2 of Learning JavaScript Data Structures and Algorithms introduces the simplest of JavaScript's data structures - arrays. The chapter provides simple code listings and associated explanations that cover creating and initializing arrays, adding elements to and removing elements from arrays, multidimensional arrays, calling methods on arrays, joining arrays, iterating over an array, sorting (including custom sorting) an array, searching with indexOf and lastIndexOf, and writing out the contents of an array as a String with toString() and join(). The second chapter concludes with a short section that lists and briefly describes additional resources on JavaScript arrays.

Chapter 3: Stacks

The third chapter of Learning JavaScript Data Structures and Algorithms introduces the concept of a LIFO stack and how to implement a stack in JavaScript as a custom Stack class. The discussion that introduces the newly written Stack class is followed by discussion on using that class. This chapter is surprisingly short given all the details it introduces (including conversions between different numeric bases) and it concludes with references to the classical "balanced parentheses and the Hanoi tower examples" that can be downloaded with the book's source code (not included in the book itself).

Chapter 4: Queues

FIFO Queues are the subject of Learning JavaScript Data Structures and Algorithms's fourth chapter. As was done in the previous chapter, Chapter 4 begins with discussion and code listings for creating a Queue class and moves onto how to apply this Queue class. The chapter concludes with descriptions of applying a priority queue and Hot Potato queue.

Chapter 5: Linked Lists

Chapter 5 of Learning JavaScript Data Structures and Algorithms introduces linked lists and compares and contrasts linked lists with arrays. As was done in the previous two chapters, the fifth chapter describes and illustrates how to implement its data structure (LinkedList) and then demonstrates using it.

Doubly linked lists are also covered in Chapter 5. The chapter discusses and illustrates adapting LinkedList to be a DoublyLinkedList. This discussion includes several simple graphics and text explaining the complexities of the book-keeping for managing a custom-developer doubly-linked list.

The fifth chapter also introduces the concepts of a circular linked list and a doubly circular linked list. Although the chapter doesn't actually cover the implementation of these data structures, it references the source code that is available for download for those implementations.

Chapter 6: Sets

The data structures covered in Chapters 2 through 5 of Learning JavaScript Data Structures and Algorithms are sequential structures, but the sixth chapter introduces Sets and describes a "Set" as "a collection of items that are unordered and consists of unique elements". The author points out that ECMAScript 6 is expected to provide a built-in Set and then demonstrates implementation of a Set "based on the Set implementation of ECMAScript 6." I found it interesting that a JavaScript Object was chosen to back the custom Set, but the author explains an advantage of this approach: "objects in JavaScript do not allow you to have two different properties on the same key, which guarantees unique elements in our set."

Chapter 6 explains and illustrates with code samples the implementation of a Set with methods matching or similar to those anticipated for the ECMAScript 6 Set. The chapter demonstrates use of the custom Set class. The coverage includes introduction with graphics of different set operations and how to implement them using the Set class.

Chapter 7: Dictionaries and Hashes

Chapter 7 of Learning JavaScript Data Structures and Algorithms introduces two structures that are based on key/value pairs and on the concept of unique entries: dictionaries (maps) and hashes. The chapter states that ECMAScript 6 includes a built-in Map implementation and then proceeds to illustrate how an Object-based Dictionary implementation can be written in JavaScript. As is the pattern in the other chapters on JavaScript data structures, this section also demonstrates use of the custom Dictionary class.

The seventh chapter also illustrates with text and code listings how to implement a HashTable and how to use it. This includes significant discussion on techniques for hash collision resolution.

Chapter 8: Trees

Learning JavaScript Data Structures and Algorithms's eighth chapter begins with an introduction to concepts and terminology associated with trees. The chapter illustrates creation of a BinarySearchTree class and how to traverse it, search it, and perform other basic operations on it. The chapter only briefly introduces the Adelson-Velskii and Landis' (AVL) tree, but states that an implementation of it can be downloaded from the source code associated with the book.

Chapter 9: Graphs

The ninth chapter of Learning JavaScript Data Structures and Algorithms introduces the last major data structure covered in this book: graphs. The chapter starts by introducing graph concepts and terminology and then describes directed and undirected graphs. The chapter explains two common implementations of graphs (adjacency matrix and incidence matrix) before illustrating development of a Graph class. The coverage of graph traversals is significant and detailed.

Chapter 10: Sorting and Searching Algorithms

The last chapter that is included in the PDF version of Learning JavaScript Data Structures and Algorithms that I was provided for my review is focused on common sorting and searching algorithms implemented in JavaScript. The covered sorting algorithms include bubble sort, selection sort, insertion sort, merge sort, and quick sort. I was surprised to learn from reading this book that each browser is free to implement Array.prototype.sort with its own choice of one of these or another sorting implementation.

The second part of the tenth chapter covers searching algorithms implemented in JavaScript and specifically covers sequential search and binary search. Big O notation is important in any discussion of algorithms and their performance and that is covered in more detail in the supplementary chapter and appendix.

Chapter 11: More About Algorithms

This eleventh chapter of Learning JavaScript Data Structures and Algorithms was referenced as a separate download (PDF) in the Preface. This chapter looks in more detail at recursion (introduced in Chapter 8) and Big O notation (introduced in Chapter 10) and discusses dynamic programming and greedy algorithms.

Chapter 11's coverage of recursion describes the situation in recursive code that can lead to stack overflow errors and describes the browser-specific limitations on call stack size. The recursion coverage also introduces ECMAScript 6's support for tail call optimization and revisits a classical recursive example with the Fibonacci sequence.

"Dynamic Programming" (DP) is described in Chapter 11 as "an optimization technique used to solve complex problems by breaking them in to smaller sub-problems." The chapter introduces the three main steps used to solve a DP problem and briefly describes some famous problems solved with DP. Code and text explanations cover implementing a solution for the minimum coin change problem.

The "Greedy Algorithms" section of the eleventh chapter states that a greedy algorithm "makes the locally optimal choice (the best solution at the time) at each stage with the hope of finding a global optimum (global best solution)." It then approaches the same minimum code change problem solved with dynamic programming approach with the greedy algorithm implementation and compares and contrasts the two approaches.

The supplementary Chapter 11 provides more in-depth discussion of Big O notation that includes a nice, simple table of the various notations and their corresponding names before describing each notation in more detail. There's a nice color chart comparing the complexities of the different levels of Big O notation and I like that the source code for generating that chart in JavaScript can be downloaded with the source associated with the book.

Chapter 11 concludes with a listing and brief description of online sites that allow developers to "[have] fun with algorithms".

Appendix: Big-O Cheat Sheet

Like Chapter 11, the Appendix is not included in the main book (at least in the PDF version I reviewed), but is instead available as a separate download (PDF) of two pages. This appendix begins with a table summarizing "the big-O notation for the insert, delete, and search operations of the data structures" covered in Learning JavaScript Data Structures and Algorithms. Similar tables exist for graph operations, sorting algorithms, and search algorithms.

General Observations

  • Learning JavaScript Data Structures and Algorithms provides an excellent introduction to data structures and to implementing and using common data structures in JavaScript. As such, it is a book that should appeal and be of use to a developer with minimal or no JavaScript experience or even someone who is not necessarily interested in JavaScript, but is interested in learning about data structures.
    • JavaScript turns out to be a pretty good language to teach and learn data structures. It's an added bonus that many of the generated data structures can be used in JavaScript code without being redundant with built-in language data structures. One of the drawbacks of teaching data structures with Java, for example, is that it's hard for the learner to see the benefit when he or she knows Java already has a rich collection of data structures.
    • Although the book is, at its title suggests, on JavaScript data structures, much of the content is largely applicable to many modern programming languages. There is clear, concise coverage of common data structures, issues to consider when implementing and using them, and the relative costs of operations (Big O notation) associated with these structures.
  • There are several screen snapshots included in Learning JavaScript Data Structures and Algorithms that are clear resolution and in color. Some of these are annotated to make them even easier to understand quickly.
  • The are several simple graphics included in Learning JavaScript Data Structures and Algorithms that are black symbols on white background. Although very simple, they communicate the information and concepts well. Indeed, their simplicity likely increases their communicative value even if they are not the most aesthetically pleasing. Given their simple nature, they probably appear very clearly even in a printed book with black text and white pages.
  • Code listings in Learning JavaScript Data Structures and Algorithms are black font on white background (no color syntax) without line numbers in the PDF version. For greater code readability, I recommend downloading the source code for this book from the Packt site and opening the code in your favorite text editor (such as Sublime) or IDE.
    • It is a common characteristic of Packt books to not include line numbers in code listings. However, Learning JavaScript Data Structures and Algorithms does place markers such as {1} after comment slashes (//) on certain lines that are later described in conjunction with that line number marking. This functions similarly to having line numbers on the source code in terms of making it easier to see clearly what lines of code are being referenced specifically in the discussion text.
    • It is recommended to download the code associated with the book not only to read it in your favorite editor, but because many included supplementary examples were mentioned but not shown in the book.
  • Several of the Packt Publishing books I've read and/or reviewed have significant editing issues such as numerous typos and misspellings. I was impressed, however, with the polish of Learning JavaScript Data Structures and Algorithms. It not only avoids typos and misspellings, but features easy-to-read prose and reads very quickly.

Conclusion

I have reviewed numerous Packt Publishing books, but feel comfortable stating that Learning JavaScript Data Structures and Algorithms is one of the best Packt Publishing titles I've read (probably in the top five). Learning JavaScript Data Structures and Algorithms is both an excellent introduction to JavaScript and to writing and using common data structures and algorithms in JavaScript. Perhaps what surprised me most about this book is that it would be a good introduction to data structures for anyone interested in learning about data structures regardless of their preferred programming language.

Thursday, December 3, 2015

Book Review: Learning TypeScript

Packt Publishing recently published Remo H. Jansen's Learning TypeScript with the subtitle, "Exploit the features of TypeScript to develop and maintain captivating web applications with ease." The book features well over 300 pages that constitute ten chapters and a preface.

Preface

The Preface of Learning TypeScript begins with an introduction to the current state of JavaScript and an explanation of why Microsoft developed TypeScript. The Preface provides short descriptions of what each of its ten chapters entails and states that Learning TypeScript "teach[es] TypeScript's core features" and "also explores the power of some tools, design principles, best practices and ... demonstrates how to apply them in a real-life application."

The "What you need for this book" section of the Preface states that TypeScript 1.5 is used for the book's examples and that the Atom text editor is used in the book (though any text editor should be fine).

The "Who this book is for" section of the Preface explains that Learning TypeScript is for an "intermediate-level JavaScript developer aiming to learn TypeScript to build beautiful web applications" who has "no prior knowledge of TypeScript," but does have "a basic understanding of jQuery."

Chapter 1: Introducing TypeScript

The first sentence of the initial chapter of Learning TypeScript states in that single sentence the intent of the book: "This book focuses on TypeScript's object-oriented nature and how it can help you to write better code." The chapter then opens with a discussion of the TypeScript architecture and TypeScript design goals. The discussed design goals include static typing, compatibility with JavaScript, code structuring mechanisms, avoidance of runtime overhead for "emitted" JavaScript code, and ability to execute on multiple platforms.

In the discussion of the TypeScript architecture, the book includes a copy of the diagram available in the Microsoft TypeScript Architectural Overview Wiki page and then provides a brief explanation for each layer in that architecture diagram.

Learning TypeScript advises the reader to use the TypeScript Playground as an easy way to play with and try out TypeScript code. A screen snapshot and explanatory text describe how a developer can type in TypeScript code on the left and see the corresponding JavaScript code on the right. This section of the first chapter also describes using the TypeScript compiler built into VisualStudio or downloaded separately via node.js's npm.

Chapter 1 of Learning TypeScript moves from its discussion of accessing the TypeScript compiler to introducing TypeScript types. This section introduces TypeScript's optional static type notation along with the variable declaration keywords var, let, and const. Union types, type guards, and type aliases are also introduced in this introductory chapter.

A good explanation of ambient declarations is presented and is followed by tables listing arithmetic operators, comparison operators, logical operators, bitwise operators, and assignment operators in TypeScript. The section on flow-control statements (if, if/else, switch, while, do/while, for, and for...in) briefly describes each and illustrates each with small code listings.

Chapter 1's survey of basic TypeScript language features wraps up with introductions to TypeScript's functions, classes, interfaces, and namespaces. The chapter concludes with an example that applies many of these language features.

Chapter 2: Automating Your Development Workflow

The second chapter of Learning TypeScript covers "some tools to automate our development workflow ... to reduce the amount of time that we usually spend on simple and repetitive tasks." The chapter begins by delineating tools to install for the development environment. These tools include Node.js, the Atom text editor and atom-typescript package, git and github, node package manager npm, Bower, DefinitelyTyped, Gulp, gulp-tslint, CommonJS, Karma, BrowserSync, and Travis CI.

Over ten years ago, Ruby on Rails introduced me to the power and convenience of scaffolding. "Scaffolding" is defined in Learning TypeScript as a tool "used to autogenerate the project structure, build scripts, and much more." The chapter states that "the most popular scaffolding tool these days is Yeoman" and then demonstrates applying Yeoman to generate a beginning source directory structure.

Chapter 3: Working with Functions

Chapter 3 of Learning TypeScript is dedicated to functions because "functions are the fundamental building block of any application in TypeScript." The chapter begins with some introductory information on TypeScript functions, how to declare them, and how to overload them. During its introduction to functions, Chapter 3 also explains the use of template strings (back tick characters) and placeholders (${}). The chapter also highlights some of the differences between TypeScript functions and JavaScript functions. For example, it explains, "Unlike JavaScript, the TypeScript compiler will throw an error if we attempt to invoke a function without providing the exact number and type of parameters that its signature declares" (unless, as the chapter explains, you indicate one or more parameters are optional using ? syntax, a default value, or rest parameters).

Chapter 3 also explains that TypeScript scope, like JavaScript scope, is function-based. Indeed, the author points out that "a TypeScript application is a JavaScript application at runtime" and that JavaScript’s variable hoisting is in force in TypeScript. Examples are provided of using let and const in TypeScript to explicitly specify different block scopes than the default function scope.

Immediately invoked function expression (IIFE) is introduced in the third chapter as a pattern "to avoid variable hoisting from within blocks" and "to prevent us from polluting the global scope." Chapter 3 also introduces generic function in TypeScript. The chapter looks at tag templates as well and notes that this is a feature anticipated for TypeScript 1.6.

The final major section of Chapter 3 is "Asynchronous programming in TypeScript." This section covers callback functions, higher-order functions, and arrow functions (fat arrow functions). The section also provides an example of "callback hell" related to loading a Handlebars template and then discusses use of Promises (and provides an example based on Q's implementation of Promise) to dress these callback issues.

The section of Learning TypeScript's third chapter on asynchronous programming looks at anticipated asynchronous functions in TypeScript and refers the reader to the TypeScript Roadmap for details on when this feature will be available (looks like TypeScript 2.0 at time of this writing). The chapter also covers generator functions (expected in TypeScript 1.6).

4. Object-Oriented Programming with TypeScript

Learning TypeScript's fifth chapter begins with a review of the SOLID principles of object-oriented programming and then begins to describe "how to write TypeScript code that adheres to these principles so that our applications are easy to maintain and extend over time." The chapter revisits classes, constructors, and interfaces in the context of the SOLID principles. It also introduces the object-oriented concepts of association, aggregation, composition, inheritance, mixins, generic classes, and generic constraints.

Chapter 4 of Learning TypeScript introduces TypeScript modules and contrasts them with namespaces. This section describes, compares, and contrasts TypeScript's "available module definition syntaxes": ES6 module (TypeScript 1.5), external modules (pre-TypeScript 1.6), AMD and require.js modules, CommonJS modules, UMD modules, and SystemJS modules.

The fourth chapter concludes with a discussion on circular dependencies and illustrates use of Atom's generation of a dependency tree graph to help identify circular dependencies.

Chapter 5: Runtime

Chapter 5 of Learning TypeScript opens with the reminder that "TypeScript runtime is the JavaScript runtime" and adds, "TypeScript is only used at design time; the TypeScript code is then compiled into JavaScript and finally executed. The JavaScript runtime is in charge of the execution. Is important to understand that we never execute TypeScript code and we always execute JavaScript code." For someone familiar with the JavaScript runtime who is reading Learning TypeScript to, well, learn TypeScript, this chapter may not be necessary.

The fifth chapter points out the importance of being aware of the environment to which a TypeScript (or JavaScript) application is being deployed as different variables and objects are available or not available in different types of environments. The chapter introduces JavaScript runtime concepts of heap, queue, stack, and frames and displays a simple color graphic of these concepts seemingly adapted from the image on the Concurrency model and Event Loop page of the Mozilla Developer Network. There is also brief discussion of how the JavaScript event loop works.

Chapter 5 discusses one of the nuances of JavaScript that I have found to be most challenging: its behavior depends on the context in which this is used and whether it's in strict mode or non-strict mode. There is brief discussion of using this in the global context and more extensive discussion of using this in the function context with emphasis on invoking the call, apply, and bind methods on the Function.prototype object.

Learning TypeScript's fifth chapter concludes with fairly detailed introductions to JavaScript Prototypes and JavaScript closures.

Chapter 6: Application Performance

Learning TypeScript's sixth chapter begins with summary of several types of resources that might have limited availability. The chapter then briefly describes several performance metrics before covering different types of performance analysis. It introduces the Resource Timing API, presents an image similar to that on the W3C Resource Timing specification page and very much like the image on the performance-bookmarklet project page, and demonstrates using the Performance-Bookmarklet browser extension.

The sixth chapter's discussion on "network performance and user experience" makes some interesting points based on a 2009 Akamai study and shows an interesting image that can also be seen in the NewRelic article Velocity Roundup: Real-Users, Performance and What Matters to You. The chapter also introduces and demonstrates using Google's PageSpeed Insights and YSlow to measure page loading times and offer suggestions for faster user experience.

Chapter 6's section on "GPU performance analysis" applies stats.js in an example demonstrating measuring of frames per second and the section on "CPU performance analysis" demonstrates using the Chrome Developer Tools Profiler. Google Chrome Developer tools are also used in this chapter's demonstration of tracking down JavaScript memory leaks.

Another section of Chapter 6 describes the JavaScript garbage collector and provides recommendations for achieving the best performance related to garbage collection. The "Performance optimization automation" section discusses automating performance monitoring and performance testing.

The concluding section of Chapter 6 covers exception handling in TypeScript and introduces the Error class, a pre-TypeScript 1.6 approach to extend the Error class for a custom error, and use of try, catch, and finally. Like Chapter 5, Chapter 6 is, for the most part, not specific to TypeScript but rather applies to JavaScript in general and also applies to TypeScript.

Chapter 7: Application Testing

The focus of Learning TypeScript's seventh chapter is "how to write unit tests for TypeScript applications." This chapter begins with a review of key terms related to unit testing such as assertion, spec, test case, test suite, test doubles (spy, dummy, stub, and mock) and test coverage.

The examples in Chapter 7 make use of a wide variety of tools and these are each briefly described in the chapter (including how to install, typically via npm install). The covered tools include Gulp for running tasks used by tests, Karma for executing tests, Istanbul for test coverage, Mocha for the test framework, Chai and Karma-Chai for test assertions, and Sinon for test doubles (test isolation) support, tsd to manage third-party tool dependencies, PhantomJS and karma-phantomjs-launcher for browser testing without starting a browser, and Selenium and Nightwatch.js for end-to-end testing.

Chapter 7's discussion of "testing planning and methodologies" briefly describes test-driven development (TDD) and behavior-driven development (BDD). There are also brief descriptions of different types of tests ranging from unit tests to end-to-end tests. The chapter concludes with explanations and discussion illustrating application of the tools to test TypeScript applications.

Chapter 8: Decorators

Chapter 7 of Learning TypeScript covers annotations and decorators. The chapter begins by explaining that decorators are planned for ECMAScript 7, but are available now in TypeScript 1.5. There is explanation regarding annotations (originated with AtScript, which is now part of TypeScript) and decorators and their subtle differences.

The seventh chapter of Learning TypeScript explains and illustrates decorators (including how to implement them) for classes, properties, methods, and parameters. The section on decorators also explains why and how a decorator factory is used and how to use a decorator with arguments.

Chapter 7 concludes with an introduction to the Reflection Metadata API. The ability to use Reflect.getMetadata is planned for ES7, but is available already in TypeScript 1.5.

Chapter 9: Application Architecture

Chapter 9 of Learning TypeScript contains discussion on application archictecture considerations for TypeScript applications. It covers the increasingly ubiquitous Single-Page Application architecture and discusses application of Ajax with Handlebars.js and jQuery.

Chapter 9's section on "MV* architecture" introduces "the Model-View-Controller (MVC) design pattern and some of its derivative versions, such as Model-View-ViewModel (MVVM) and Model-View-Presenter (MVP)." There are some useful diagrams in this section illustrating these concepts and versions of these diagrams are available in the presentations Working with AngularJS and About Flux.

After its introductory coverage of single-page applications and so-called MV* architectures, the ninth chapter looks at how to go about selecting a framework to use with single-page applications rather than writing one from scratch. This section provides a useful graphic that depicts different potentially desired characteristics of such a framework with one or more frameworks that meets satisfies those desires. I also appreciated this section's reference to TodoMVC for "helping you select an MV* framework."

Learning TypeScript's ninth chapter concludes with a sizable section on developing a custom MV* framework. There are several code listings and associated explanation associated with this example that the author warns is for learning purposes rather than for production use.

Chapter 10: Putting Everything Together

Learning TypeScript's final chapter "[puts] into practice the majority of the concepts ... covered in the previous chapters" via an example demonstrating development of a "a small single-page web application using the SPA framework" that was introduced in Chapter 9. The chapter contains several code listings and explanations of each part of the application. The chapter briefly covers unit testing of the application and preparing the application for a production release.

General Observations

  • The book that is the subject of this review, Learning TypeScript, should not be confused with Bryan Rayner's presentation Learning TypeScript: Or, An Appeal to Embrace Typed Languages.
  • Some chapters of Learning TypeScript are TypeScript-specific and others are more ECMAScript/JavaScript-general. Because TypeScript is a superset of JavaScript and an implementation of ECMAScript, this is a sensible approach for a book with a title that includes "TypeScript".
    • Developers with no TypeScript or JavaScript experience can use Learning TypeScript as a completely self-contained book to introduce themselves to TypeScript and the JavaScript ecosystem works in.
    • Developers who already have TypeScript experience may still find the book useful because of the details provided related to ECMAScript in general (particularly newer and future features).
    • Even developers who are not interested in TypeScript in particular, but who are interested in modern and future ECMAScript features, may find several of the chapters to be useful.
      • For example, while the chapter of Learning TypeScript on object-oriented concepts will not bring many new concepts to the attention of Java, C++, and C# developers, it could be very useful for a JavaScript developer with minimal class-based object-oriented experience who wants to learn more about where ECMAScript's class support is heading.
      • TypeScript already implements several proposed new ECMAScript features, so learning about TypeScript can often translate to better understanding JavaScript's future.
      • TypeScript has been adopted by Angular 2, making understanding of it important to many web developers who might not otherwise be interested in TypeScript.
  • There are numerous code listings in Learning TypeScript. Most are short and do a good job of illustrating the concepts. Even in the PDF version I reviewed, they are black font on white background with no color syntax and no line numbers. I encourage anyone following along with the code examples to download the source code and open it in their favorite IDE or text editor.
  • It's a minor pet peeve of mine, but I'll point it out here for completeness. Several of the examples in Learning TypeScript use the "legacy" example keywords "foo" and "bar." I was starting to think books were getting away from this, but this is a modern book that still uses those generic names. I've never liked these terms for multiple reasons, but perhaps my strongest reason to dislike them is that they have zero contextual meaning. I'd rather see contrived but still more realistic variable names in examples.
  • The TypeScript Language Specification (1.5 as of this writing), the TypeScript Tutorial, and the TypeScript Handbook are additional very useful resources.

Conclusion

Learning TypeScript provides a solid introduction to TypeScript. It goes beyond that, however, and, via discussion of TypeScript, examines modern ECMAScript and where ECMAScript is going in the near future. The reader of Learning TypeScript not only learns about TypeScript, but learns about recent and soon-to-be-added features for JavaScript. Learning TypeScript contains TypeScript-specific details as well as JavaScript/ECMAScript-general details. This demonstrates how TypeScript can be used in conjunction with other JavaScript tools and frameworks.

Wednesday, November 25, 2015

Book Review: Digital Java EE 7 Web Application Development

Packt Publishing recently (September 2015) published Peter Pilgrim's Digital Java EE 7 Web Application Development with the subtitle "Develop Java enterprise applications to meet the emerging digital standards using Java EE 7." Digital Java EE 7 Web Application Development contains 9 chapters and 4 appendices spanning over 400 pages. The book's subtitle is "Develop Java enterprise applications to meet the emerging digital standards using Java EE 7."

Preface

The Preface of Digital Java EE 7 Web Application Development states that the book is "a continuation of the first book Java EE 7 Developer Handbook" with Digital Java EE 7 Web Application Development focusing on "the Java presentation tier." The Preface adds that "the book was written for the developers who want to become superior and adept at building a web application on JVM with the standard Java EE 7 platform" and with specific focus on JavaServer Faces with some coverage of JavaScript and AngularJS.

The Preface contains short summaries (2-3 sentences each) of each of the book's chapters and appendices. It also lists the software necessary to run the examples provided in the book: Java 8, GlassFish 4.1, Gradle 2.6, Chrome DevTools, Firefox Developer Tools, "Chris Pederick's Web Developer and User Agent Switcher extensions", and "a decent Java Editor or IDE for coding."

In describing "who this book is for," the Preface of Digital Java EE 7 Web Application Development states, "this book is pitched at intermediate Java developers" with "a good command over the programming language" that includes Java SE knowledge of "classes, inheritance, and Java Collections" and Java EE knowledge of "Java persistence, Java servlets, and deployment of the WAR files to an application server."

Chapter 1: Digital Java EE 7

The initial chapter of Digital Java EE 7 Web Application Development begins with a high-level introduction to working in the digital domain and looks at how Java fits inside the digital domain. There is specific focus on recent changes in Java 8 including removal of PermGen, introduction of the G1 garbage collector and introductory code listing and explanatory text related to lambda expressions.

Chapter 1 also looks at JavaScript with specific focus on JavaScript libraries such as jQuery, AngularJS, and GruntJS. The chapter provides a brief overview of Java EE 7 features that are applicable in the digital design world and introduces application servers that implement Java EE 7 including GlassFish 4.1, Payara Server, and WildFly. There is also a brief introduction (with code listing) to JavaServer Faces.

Chapter 2: JavaServer Faces Lifecycle

JavaServer Faces (JSF) is the exclusive topic of Chapter 2 of Digital Java EE 7 Web Application Development. The chapter introduces JavaServer Faces with a historical perspective and references terminology such as responsive design, mobile web design, Digital by Default, and Default to Open. A brief history of JSF is outlined and some key new features of JSF 2.2 are listed. Arguments for choosing JSF over other Java-based web frameworks are also made and mostly center around JSF being a standard.

The second chapter introduces Model-View-Controller (MVC) and describes how JSF implements MVC. The chapter provides a high-level overview of Facelets and looks at the JSF request processing lifecycle. The chapter provides multiple JSF code listings and demonstrates JSF features such as JSF annotations, expression language, web descriptor, and JSF XML namespaces. After providing another JSF code example, Chapter 2 returns to more in-depth coverage of expression language.

Chapter 3: Building JSF Forms

Like the second chapter, Digital Java EE 7 Web Application Development's third chapter is JSF-centric, but does include some HTML5/CSS/Bootstrap code in the example that is built throughout the chapter. This chapter begins with an example of implementing a CRUD-oriented application using a JSF Facelet with Bootstrap via passthrough. Several JSF tags and tag libraries are introduced as they are applied in the example being built up in Chapter 3. The example constructed in Chapter 3 also demonstrates use of Java Persistence API (JPA) as functionality to allow contacts to be created, read, updated, and deleted is added.

One of the things that can be confusing to someone new to JavaServer Faces is the difference between the older JSF managed beans and the newer CDI-based beans. The third chapter concludes with a discussion that addresses this.

Chapter 4: JSF Validation and AJAX

The fourth chapter of Digital Java EE 7 Web Application Development begins with an explanation of why validation is important in web applications and then introduces and contrasts server-side validation with client-side validation. The chapter then details using the h:message and h:messages JSF tags to display errors.

Chapter 4's coverage of validation in JSF introduces "two main ways of achieving validation": Java EE 7's Bean Validation 1.1 and "traditional" JSF validation. The chapter provides a code example, a table of relevant annotations, and explanatory text in its introduction of bean validation. Explanatory text and code listings are also provided in the introduction to JSF validation.

This fourth chapter also introduces applying the attributes requiredMessage, validatorMessage, and conversionMessage for custom messages. The chapter looks at overriding validation messages globally via properties files and demonstrates adding a custom validation method to a rendering tag.

The section of Chapter 4 on custom validators demonstrates creating a custom validator by writing a class that implements javax.faces.validator.Validator and annotating it with @javax.faces.validator.FacesValidator. The "Converters" section of Chapter 4 describes converters as "JSF classes that convert between strings and objects" and then provides detailed discussion and code examples implementing a custom converter.

Chapter 4 also looks at "validating immediately with AJAX." This section introduces Ajax and JSF's "built-in support for AJAX requests and responses." It introduces <f:ajax>, provides multiple code listings demonstrating use of it, provides a table of its attributes, and looks at how it relates to the JSF lifecycle.

One of Chapter 4's major sections is "Handling views" and this concluding section of the chapter covers invoking and passing parameters to controller methods, invoking an action event listener, redirecting pages, and debugging JSF with <ui:debug>.

Chapter 5: Conversations and Journeys

Chapter 5 of Digital Java EE 7 Web Application Development focuses on "JSF conversation scope." The chapter begins by looking at how JSF integrates Context and Dependency Injection (CDI) context and dependency injection. The chapter defines "conversational scope" as "defined by a lifecycle that spans many HTTP requests to the server." The @javax.enterprise.context.ConversationScoped annotation and the CDI-injected javax.enterprise.context.Conversation interface are introduced and demonstrated early in this chapter before coverage of the lifecycle of a conversation scoped bean.

There are several screen snapshots and code listings in this chapter. These payday loan application examples include application of Bootstrap CSS and JSF 2.2's support for HTML5-friendly pages with new namespace http://xmlns.jcp.org/jsf. They illustrate applying Ajax, jQuery, and the HTML5 Range element.

The final major section of the fifth chapter covers JSF custom components. An example of a custom component implemented with an XHTML-defined Facelet view backed by a backing bean is provided. Another portion of this section introduces the JSF 2.2 ability to "generate the custom tag without specifying any XML declaration."

Chapter 6: JSF Flows and Finesse

Digital Java EE 7 Web Application Development's sixth chapter emphasizes what I think may be the most interesting feature of JSF 2.2: Faces Flows. The chapter begins by explaining the use cases and sample applications that inspired and can make most use of Faces Flow before delineating some key characteristics of Faces Flow. Several early sections use code listings to demonstrate use of annotations (such as @javax.faces.flow.FlowScoped) and other Faces Flow concepts in simple flows.

Chapter 6 demonstrates use of the <error-page> element to declare "an association between an exception type and a page view" that allows for nicer presentation of the "one of the most notorious exceptions" encountered in JSF-based applications (javax.faces.application.ViewExpiredException).

The sixth chapter also looks at complex flows. It demonstrates and describes flows that switch on different cases (conditional) and nest flows. In addition to the inline explanatory examples, the chapter includes a "a real-world example" with significant discussion on security-related aspects.

Faces Flows is not the only JSF 2.2 feature covered in Chapter 6. The chapter also introduces Resource Library Contracts. Several nuances are demonstrated with code listings and there is a section on using Resource Library Contracts in conjunction with Faces Flows. The chapter concludes with a bullet list of tips for using Faces Flows.

Chapter 7: Progressive JavaScript Frameworks and Modules

Chapter 7 of Digital Java EE 7 Web Application Development begins with the observation that "in the contemporary way of building a website, there is no escape from the language of JavaScript because it is a de facto standard of modern web browsers." Although earlier chapters touched on JavaScript and related technologies, this is the first chapter that focuses on something other than JSF. The focus of the chapter is JavaScript and JavaScript libraries and frameworks.

The seventh chapter begins with an introduction to the basics of JavaScript. After covering some of the basics and gotchas of JavaScript and introducing some JavaScript features in comparison to similar Java features, the chapter moves onto introducing jQuery (and sizzle) in more detail. Along with demonstrating use of jQuery for DOM manipulation and simple animation, the chapter also demonstrates including jQuery code in a JSF application.

RequireJS, Underscore, and Grunt are also introduced in Chapter 7. There are several pages devoted to each that introduce some key features and uses of each framework.

Chapter 8: AngularJS and Java RESTful Services

The eighth chapter of Digital Java EE 7 Web Application Development shifts from JSF to an "alternate design mode." I like the author's articulation (probably because it coincides with my own observations) of where JSF fits best and where this alternate design mode fits best. Pilgrim writes:

The design philosophy behind building an application on a single page such that it resembles a desktop application is in marked contrast to the JavaServer Faces' original design of navigation links between pages. ... JSF lends itself to applications that are extremely stateful in nature and design, where the customer journey is based on page-to-page navigation.

After successfully explaining why JSF in not a good fit for the single-page design model, the author lists some advantages and disadvantages of the single-page model. An example of a single-page application for this chapter is introduced and is related to managing case worker information. This example is defined in more detail after the AngularJS coverage.

Chapter 8 introduces one of the trendiest and most popular web development frameworks in AngularJS. The introduction is fairly thorough for only being part of a single chapter. Significantly more details of AngularJS are illustrated with a return to the caseworker application implemented with AngularJS. The casework sample application demonstrates use of Java EE features such as JPA, REST-based web services with JAX-RS, and WebSocket in addition to AngularJS.

Chapter 9: Java EE MVC Framework

The final chapter of Digital Java EE 7 Web Application Development is a future-looking chapter that looks past Java EE 7 and introduces the JSR 371: Model-View-Controller (MVC 1.0) Specification. The chapter explains why this specification is being planned for Java EE 8 and also introduces a reference implementation of it (Ozark). Because Java EE 8 (JSR 366) is still forthcoming, Pilgrim warns the reader of this chapter, "the information here is subject to change because MVC is evolving."

This ninth chapter introduces the new package called javax.mvc and the @javax.mvc.Controller annotation. The chapter then explains and illustrates applying MVC 1.0 with JAX-RS annotations. The examples and explanations also cover using JSF for the view portion and using the javax.mvc.Models interface for the model portion of an MVC 1.0-based application.

Chapter 9 introduces JavaScript-based Handlebars.js (and Mustache) and then segues from there into an introduction of the Java port of this. Several pages are devoted to building up the example of the application using Ozark and Handlebars for Java.

The final chapter concludes with a look at a couple ways in which the Java MVC 1.0 specification might still change and with a discussion of design considerations when using Java MVC.

The Appendices

Appendix A ("JSF with HTML5, Resources, and Faces Flows") is a "quick reference" on the JSF library. The first part of this appendix is a standard library reference with emphasis on Java EE 6 (JSF 2.0) and Java EE 7 (JSF 2.2) versions of JavaServer Faces. The second part covers Faces Flows with a thorough reference.

Appendix B ("From Request to Response") covers common topics associated with a web application's architecture. These topics include HTTP (protocol itself, advancements, request methods and response codes), standard Java EE web architecture (servlet + CDI + EJB), extended Java EE web architectures (NoSQL, Model-View-ViewModel, Java Temporary Caching, embedded server/containerless system, microservices), and brief discussion addressing the question, "Is it important to be known as a full stack developer?"

Appendix C ("Agile Performance – Working inside Digital Teams") consists on ten pages that look at structure of agile digital development teams and roles on those teams.

Appendix D ("Curated References") is over six pages of "references around the digital software development" that are categorized "into concepts and themes" of "Service delivery", "Agile and leadership", "Architecture", "User interface", and "Java EE and JVM technology." Because these references include URLs, it is easy to click on these in the electronic versions to be taken directly to the referenced resource.

What is Digital Java EE 7 Web Application Development and Who Is It Best Suited For?

One of the most valuable insights I believe I can provide to someone trying to decide whether to purchase or borrow this book is to articulate what this book covers and who it is best suited for. I attempt that here.

  • Digital Java EE 7 Web Application Development provides significant coverage of JavaServer Faces (JSF).
    • Chapters 2 through 6 are almost exclusively JSF-focused and JSF has significant presence in Chapter 1 and Chapter 9.
    • It's not just JSF, however, and covers using HTML5 and related popular frameworks and libraries with JSF.
    • Chapter 8 (single-page applications with AngularJS) is not JSF at all.
  • Digital Java EE 7 Web Application Development focuses on newer JSF features.
    • As its title implies, the focus of this book is on Java EE 7 and JSF 2.2 with some mention of Java EE 6 (JSF 2.0).
    • This is probably NOT the book you want to get to learn JSF for the first time, but rather is more suited for someone with minimal JSF experience through JSF 2.0 who wants to learn what's available in JSF 2.2 and Java EE 7.
  • Digital Java EE 7 Web Application Development provides broad coverage of developing Java EE 7 web application user interfaces.
  • Digital Java EE 7 Web Application Development is most likely to appeal to these types of developers:
    • Intermediate-level (or advanced) Java developers (as described in the book's preface) who have only basic familiarity with JSF and want to learn more about newer (Java EE 7/JSF 2.2) JSF features.
    • Java developers with only limited awareness of modern web client technologies and frameworks who want to learn how to use JSF with HTML5 and related technologies.
    • Java developers who want a broad view of the currently favored and standard Java EE 7 technologies for web development and want an introductory chapter on Java EE MVC coming with Java EE 8.
  • Digital Java EE 7 Web Application Development may not meet expectations of these types of developers:
    • Developers looking for an introduction to JavaServer Faces; this book is better suited to developers with at least minimal JSF experience as it's not an introductory book and it mostly focused on newer JSF features.
    • Developers looking for a reference on HTML5, JavaScript, CSS or related frameworks and libraries such as AngularJS, jQuery, and Bootstrap; although this book does introduce using these technologies with JSF, the coverage of these particular technologies is mostly introductory.
    • Developers looking for a book on developing Java-based web applications with JAX-WS (SOAP/WSDL-based web services), Java servlets, or one of the many available non-standard Java/JVM web frameworks; this book does not cover these older and/or non-standard technologies and instead focuses it coverage on JSF 2.2 and JAX-RS.

Other General Observations

The following are some general observations and opinions of mine formed while reading the PDF version of Digital Java EE 7 Web Application Development provided to me by Packt Publishing.

  • Digital Java EE 7 Web Application Development includes numerous code examples
    • Code listings tend to build upon same simple example applications that highlight benefits and advantages of the approach being demonstrated.
    • Code listings are black font on white background with no color syntax highlighting and no line numbers even in the electronic (PDF) version of the book that I read.
    • I liked that there are extensive code listings and understand that printed books would not be able to have color syntax highlighting, but having color syntax in the code examples (especially the larger ones) or otherwise emphasized code would have helped with code readability.
    • I recommend acquiring code listings in Pilgrim's GitHub repository so that an IDE or text editor can be used to view the code.
  • There are several color screen snapshots in the PDF version of Digital Java EE 7 Web Application Development to help illustrate the web pages rendered by JSF and related web technologies. There are also color graphics for illustrating concepts in the electronic version.
  • Although Digital Java EE 7 Web Application Development is a Java EE book, it does a nice job of inserting some Java SE 7 JDK libraries in the examples. In particular, the Java 8 date/time library is used multiple times and Chapter 1 introduces lambda expressions.
  • For the most part, Digital Java EE 7 Web Application Development reads fairly well. However, as with several other Packt Publishing titles I have reviewed, it could have used more editing. There are several typos in Digital Java EE 7 Web Application Development, but these rarely prevent the content from being understood. To help convey the type of typos for you to judge how they might impact your ability to understand what is being communicated, I include some examples here:
    • "SERVERITY_WARNING"
    • "these tags render the content from the javax.faces.HtmlMessages and javax.faces.HtmlMessages components respectively"
    • "The JavaScript support functions as a first-class citizen and rules on a and supports the declaration of functions..."
    • "JSF 1.0 was created in the early noughties,..."
    • "...new package structure reserved for MVC under javax.mvc. The @javac.mvc.Controller annotation..."
  • Each of the chapters has a set of questions and exercises at the end of the chapter. I didn't run through any of these and cannot comment on the benefits of them. I also am not aware of any answers or solutions being provided for any of the exercises or questions.
  • Pilgrim references his other Packt title, Java EE 7 Developer Handbook, several times in this book for readers who need additional background Java EE details, particularly in areas outside of the user interface.
    • Reviews for the earlier book probably provide good indicators of what one can expect from Digital Java EE 7 Web Application Development. Indeed, I noticed that several of the positive and negative comments on Amazon.com apply to the book I'm reviewing as well. As of this writing, there are 10 reviews of Java EE 7 Developer Handbook on Amazon.com with an average 3.8 (out of 5) rating.
  • Find the author's own description of his book in the blog post My Digital Java EE 7 book is now published.

Conclusion

Digital Java EE 7 Web Application Development provides a survey of the most popular Java EE 7 approaches for developing web applications. JavaServer Faces receives the lion's share of the discussion with focus particularly on Java EE 7's version of JSF (2.2). Digital Java EE 7 Web Application Development introduces JavaScript and AngularJS and discusses using HTML5 technologies, libraries, and frameworks with JSF. There is also forward-looking chapter on Java EE 8's forthcoming Java EE MVC. Digital Java EE 7 Web Application Development is a book largely on modern JSF, but it's not accurate to say it's only a JSF book and it's probably not the best introductory book for those who have never used JSF. Instead, this book is best suited for Java developers with some awareness of JSF, but who want to learn more about JSF 2.2 and integrating JSF with other non-Java web languages, libraries, and frameworks.

Tuesday, November 10, 2015

Does PostgreSQL Have an ORA-01795-like Limit?

The Oracle database requires that no more than 1000 entries be used in a SQL IN portion of a WHERE clause and will throw an ORA-01795 error if that number is exceeded. If a value needs to be compared to more than 1000 values, approaches other than use of IN must be applied. I wondered if this limitation applies to PostgreSQL and decided to write a simple application to find out.

For my simple test application, I wanted a very simple table to use with both an Oracle database and a PostgreSQL database.

Oracle: Creating Single Column Table And Inserting Single Row
CREATE TABLE numeral(numeral1 number);
INSERT INTO numeral (numeral1) VALUES (15);
PostgreSQL: Creating Single Column Table and Inserting Single Row
CREATE TABLE numeral(numeral1 numeric);
INSERT INTO numeral (numeral1) VALUES (15);

Building the SQL Query

Java 8 makes it to build up a query to test the condition of more than 1000 values in an IN clause. The next code snippet focuses on how this can be accomplished easily.

Java 8 Construction of SQL Query
final String queryPrefix = "SELECT numeral1 FROM numeral WHERE numeral1 IN ";
final String inClauseTarget =
   IntStream.range(1, numberOfInValues+1).boxed().map(String::valueOf).collect(Collectors.joining(",", "(", ")"));
final String select = queryPrefix + inClauseTarget;

The string constructed by the Java 8 code shown in the last code listing looks like this:

SELECT numeral1 FROM numeral WHERE numeral1 IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,975,976,977,978,979,980,981,982,983,984,985,986,987,988,989,990,991,992,993,994,995,996,997,998,999,1000,1001)

Running the Query

When the above SQL query statement is executed against an Oracle database, the ORA-01795 error is manifest:

java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000

The PostgreSQL database does not have this same limitation as shown by its output below:

15

The full Java class I used to demonstrate the above findings in available at https://github.com/dustinmarx/databasedemos/blob/master/dustin/examples/inparameters/Main.java.

Conclusion

There are numerous ways to avoid the ORA-01795 error when using an Oracle database. However, I was curious if the same limitation existed for PostgreSQL and apparently it doesn't (I'm using PostgreSQL 9.4.4 in these examples). In fact, when I tried as many as one million IN values, PostgreSQL was still able to process the query, albeit noticeably slower than with a smaller number of IN values.