javascript

JavaScript, Node.js, and the Flying Wedge Anti-Pattern

The "Flying Wedge" is the name of a military formation in which troops would march into battle in a V-shaped formation. That same V-shaped "formation" unfortunately has a tendency to show itself in JavaScript and CoffeScript. And in the vast majority of cases, it is bad practice.

Code that's easy to read, modify, re-use, and debug is better code. A convention that hampers those goals without offering an overriding benefit is an anti-pattern. That, in a nutshell, is why the flying wedge ought to be avoided.

Here's an explanation of the anti-pattern, along with some suggestions for avoiding it.

Node.js Debugging with the Built-in Debugger

Node debuggerNode debuggerThe HTTP version of the app worked, but the commandline version did not. What went wrong? It was hard to say. The application simply hung, unresponsive. Try/catch and event handlers didn't find anything wrong, and my typical console.log() approach wasn't cutting it, either. I need to fire up Node.js's command line debugger.

This is a short tutorial explaining how to use the debugger that comes built-in to Node.js. It explains how to use the command line debugger to set breakpoints , step through the code, and analyze the debugging output. By the time you're done reading, you should be able to quickly debug your own code.

The Basics

  • Node has a built-in debugger
  • It can be executed using node debug your_scriptfile.js
  • In your app, you can set breakpoints using debugger;

Now let's look at each step of debugging....

Node.js: Five Things Every PHP Developer Should Know

I recently started working on a few Node.js applications. Coming most recently from PHP (and Drupal in particular), I found the transition to Node.js to be surprisingly easy. Pleasurable, in fact. But I had to learn to think differently about a few things.

Below I list the five things I think every PHP developer should know about Node.js.

Node.js: Connection Pools and MongoDB

Node.js can interact well with MongoDB using the MongoDB native driver. The driver comes with plenty of documentation. However, one feature that isn't explained clearly is the pooling system.

The MongoDB Node library can create a pool of connections to the database that can then be shared throughout your Node application. That means that you can open a series of connections once (at startup) and then use those connections as needed.

Here are the mechanics:

mongodb = require('mongodb');
 
// Define options. Note poolSize.
var serverOptions = {
  'auto_reconnect': true,
  'poolSize': 5
};
 
// Now create the server, passing our options.
var serv = new mongodb.Server('localhost', 27017, serverOptions);
 
// At this point, there is no connection made to the server.
 
// Create a handle to the Mongo database called 'myDB'.
var dbManager = new mongodb.Db('myDB', serv);
 
// NOW we initialize ALL 5 connections:
dbManager.open(function (error, db) {
  // Do something with the connection.
 
  // Make sure to call db.close() when ALL connections need
  // to be shut down.
  db.close();
});

The salient detail of the above code is that the database connections are not opened until Db.open() is called. At that point, all five (our poolSize is 5) will be opened at once. These will be left open until the database is closed.

Internally, the MongoDB library will manage which requests go to which connections.

Note that the variables dbManager and db above both point to the same object. To really make use of pooling, your application will have to do something more sophisticated than the above. After all, there's no sense in opening five connections, executing a single operation, and then closing all five connections.

In-depth resource on how browsers work

How Browsers Work is an in-depth resource -- about 50 printed pages long -- explaining how a browser begins with HTML, CSS, and JavaScript files, and ends up with a display.

The clear explanation, based on FireFox and Chrome/webkit, explains many of the nuances that frequently cause edge-case bugs in CSS rendering or JavaScript code. It also provides a good introduction to parsing techniques (browsers use various kinds of parsers and parser generators), along with other lower-level implementation details.

jQuery Checkboxes: Checking and unchecking the right way

When working with checkboxes in jQuery and JavaScript, sometimes all you really want to do is toggle the checked state of the checkbox. There are many examples of strange ways of accomplishing this, many of which are wrong or will only work on some browsers. Here is a correct (XHTML-correct) and compact way of doing this.

Check to see if a checkbox is checked

// Returns a boolean, true if checked, false otherwise
jQuery('#my-checkbox').is(':checked');

Check a checkbox

A PHP jQuery Library: QueryPath Overview

jQuery is a JavaScript library for efficiently working with HTML and CSS. Its chainable and compact API has made it a popular choice for web developers seeking to quickly build rich web applications. But did you know there is a PHP jQuery library? QueryPath is a PHP implementation of jQuery's interface. It provides all of the DOM manipulation functions, a full CSS selector engine, and as much of jQuery's other features as is practically implemented server-side. But that's not all. This powerful library delivers many server-side features designed to make working with XML services simple, robust, and reliable.

Dave Hall: Review of "Drupal 6, JavaScript, and jQuery"

Dave Hall recently reviewed my Drupal 6, JavaScript, and jQuery book.

Dave Hall's BlogDave Hall's Blog

I found this observation particularly apt:

This book is definitely not for the copy and paste coder, nor the developer who just wants ready made solutions they can quickly hack into an existing project. Some may disagree, but I think this is a real positive of this book. Matt uses the examples to illustrate certain concepts or features which he wants the reader to understand. I found the examples got me thinking about what I wanted to use JS and jQuery for in my Drupal sites.

This is what I want to accomplish in the book. My goal is to get coders thinking about what they can do (and how they can do it) rather than simply re-using code. I'm so glad that Dave noticed this and brought it into focus. If you are interested in taking a peek at the book, you can download one of the chapters.

Using Flashy to Play Video inside Lightbox2

By default, the Drupal Lightbox2 module supports the flvPlayer flash movie player, which is not Open Source. However, Drupal has a 100% Open Source video player called Flashy. Here's a quick method of overriding Lightbox's setup to use Flashy instead of flvPlayer. Update: Fixed for Internet Explorer.

Chris Shattuck's review of Drupal 6 Javascript and jQuery

Chris Shattuck wrote a review of my Drupal 6 JavaScript and jQuery book. It is an insightful review, and will certainly influence what I do in the second edition. One of the commentors on the blog noted that the review was very detailed and must have taken a long time to write. I agree. Thanks, Chris, for taking the time outlining and evaluating.

While the review was mostly focused on the content of the book, my favorite quote is this:

I was excited enough about the new stuff I learned that I picked up a module that had been languishing for a while and re-wrote a bunch of the code using the principles addressed in this book.

It makes me happy to know that the book prompted people to write (or re-write) code.
David Shattuck ReviewDavid Shattuck Review

Syndicate content