Friday, January 17, 2014

Awesome talks

There are a lot of screencasts, recordings of user group gatherings and conference talks available online. I try to commit myself watching at least two new talks every week, and I've been doing this for quite some time now. I created this list of online talks that I really enjoyed watching. I'll also be updating this list whenever I've watched another *awesome* talk that is worthy enough.

Suggestions are always appreciated through a pull request.

Until next time.

Thursday, January 02, 2014

Clojure Kata #2 – The Bowling Game

As mentioned in the previous blog post, I’ve been learning Clojure and I decided to do so while practicing a couple of coding katas. The second kata that I want to walk through is the bowling game.

First, we needed to decide which testing framework to use. Our choice fell on Midje, whose syntax looked very compelling to me on first glance. So we added the following line to .lein/profiles.clj :

{:user {:plugins [[lein-midje "3.0.0"]]}}

Initializing a new project is as simple as issuing the following command:

$ lein new midje bowling_game

We’re now ready for our first test.

Test #1: Gutter game

Here’s the code for our first test.

(facts "When calculating the score of a bowling game"
    (fact "it returns a zero score for a complete gutter game"
        (score(repeat 20 0)) => 0))

Running this test without having the score function implemented obviously results in an error. In order to satisfy this test, we have to implement this function.

(defn score [rolls]
     0)

We’re putting out the least amount of code in order to satisfy this test. So just returning zero will do just fine for now.

Test #2: A game of two pin frames

For our next test, we create a complete game of only two pin frames.

(fact "it returns a score of twenty for a game of two pin frames"
      (score(repeat 20 1)) => 20))

In order to make this test pass, we have to return a score that is the sum of all the pins knocked down for each roll.

(defn score [rolls]
     (reduce + rolls))

The tests all pass, including the test for the gutter game and we still don’t have anything to refactor. Let’s move on to the next test case.

Test #3: A game with a spare

The next test is going to create a game that includes a spare.

(fact "it returns a score that includes the next roll for a spare"
     (score(concat (5 5 3) (repeat 17 0))) => 16))

The score of a spare is calculated by taking the 10 pins from the frame and adding the number of pins of the first roll of the next frame.

At this point we felt the need to actually divide up the scores and bring in the notion of a frame and a game. So we commented out our new test and went on a refactoring spree. This is what we came up with:

(defn make-frames[rolls]
    (lazy-seq (cons (take 2 rolls)      (make-frames (drop 2 rolls)))))

(defn make-game[rolls]
    (take 10 (make-frames rolls)))

(defn calculate-frame-score [frame_rolls]
    (reduce + frame_rolls))

(defn calculate-game-score [frame_scores]
    (reduce + frame_scores))

(defn score [rolls]
     (calculate-game-score (map calculate-frame-score (make-game rolls))))

As you can see, we created separate functions for creating a game (make-game) that consists of 10 frames (make-frames). Therefore we also added separate functions for calculating a frame score (calculate-frame-score) and ultimately the total score of a game (calculate-game-score).

All previous tests still pass, so we’re able to uncomment the third test that tests the scenario of a game with a spare.

(defn spare? [rolls]
    (= 10 (apply + (take 2 rolls))))

(defn number-of-roles-for-frame-score [rolls]
    (cond 
        (spare? rolls) 3 
        :else 2))

(defn make-frames[rolls]
    (lazy-seq (cons (take (number-of-roles-for-frame-score rolls) rolls)      (make-frames (drop 2 rolls)))))

(defn make-game[rolls]
    (take 10 (make-frames rolls)))

(defn calculate-frame-score [frame_rolls]
    (reduce + frame_rolls))

(defn calculate-game-score [frame_scores]
    (reduce + frame_scores))

(defn score [rolls]
     (calculate-game-score (map calculate-frame-score (make-game rolls))))

Implementing this scenario was quite easy after our refactoring. We added a function that checks for a spare? frame. We also introduced an additional function that uses the new spare? function for determining how many rolls need to be incorporated for the score of the frame. Now we simply need to replace “(take 2 rolls)” with “(take (number-of-roles-for-frame-score rolls) rolls)” in the make-frames function.

All tests are green now. Before moving on to the next test case, we also performed a small refactoring to the test code as well.

(fact "it returns a score that includes the next roll for a spare frame"
    (score(concat '(5 5) '(3 0) (repeat 16 0))) => 16)

This better communicates the notion of two frames, the first with a spare and the second being a regular frame.

Test #4: A game with a strike

The next one is going to set up the scenario of a game with a strike.

(fact "it returns a score that includes the next two rolls for a strike frame"
    (score(concat '(10) '(5 3) (repeat 16 0))) => 26)

The score of a strike is calculated by taking the 10 pins from the frame and adding the number of pins of the next two rolls.

(defn spare? [rolls]
    (= 10 (apply + (take 2 rolls))))

(defn strike? [rolls]
    (= 10 (first rolls)))

(defn number-of-roles-for-frame-score [rolls]
    (cond
        (strike? rolls) 3 
        (spare? rolls) 3 
        :else 2))

(defn number-of-rolls-for-frame [rolls]
    (if (strike? rolls) 1 2))

(defn make-frames[rolls]
    (lazy-seq (cons (take (number-of-roles-for-frame-score rolls) rolls)      (make-frames (drop (number-of-rolls-for-frame rolls) rolls)))))

(defn make-game[rolls]
    (take 10 (make-frames rolls)))

(defn calculate-frame-score [frame_rolls]
    (reduce + frame_rolls))

(defn calculate-game-score [frame_scores]
    (reduce + frame_scores))

(defn score [rolls]
     (calculate-game-score (map calculate-frame-score (make-game rolls))))

We added a function that checks for a strike?, which we added to the number-of-roles-for-frame-score function just as we did for the spare? function. Besides this, we also needed to add a function named number-of-roles-for-frame that determines how many rolls to skip when recursively calling make-frames.

All tests are green again, and our story has come to an end.

The full code

We slightly refactored our tests by introducing a function named zero-frames that makes it easier to fill up a game with frames of zero pins. Here’s the complete code of our tests.

(defn zero-frames [number]
    (repeat (* 2 number) 0))

(facts "When calculating the score of a bowling game"
    (fact "it returns a zero score for a complete gutter game"
        (score(zero-frames 10)) => 0)

    (fact "it returns a score of twenty for a game of only two pin frames"
          (score(repeat 20 1)) => 20)

    (fact "it returns a score that includes the next roll for a spare frame"
        (score(concat '(5 5) '(3 0) (zero-frames 8))) => 16)

    (fact "it returns a score that includes the next two rolls for a strike frame"
        (score(concat '(10) '(5 3) (zero-frames 8))) => 26)

    (fact "it returns a score of three hundred the perfect game"
         (score(concat (repeat 12 10))) => 300))

And here’s the complete code of our implementation.

(defn spare? [rolls]
    (= 10 (apply + (take 2 rolls))))

(defn strike? [rolls]
    (= 10 (first rolls)))

(defn number-of-roles-for-frame-score [rolls]
    (cond
        (strike? rolls) 3 
        (spare? rolls) 3 
        :else 2))

(defn number-of-rolls-for-frame [rolls]
    (if (strike? rolls) 1 2))

(defn make-frames[rolls]
    (lazy-seq (cons (take (number-of-roles-for-frame-score rolls) rolls)      (make-frames (drop (number-of-rolls-for-frame rolls) rolls)))))

(defn make-game[rolls]
    (take 10 (make-frames rolls)))

(defn calculate-frame-score [frame_rolls]
    (reduce + frame_rolls))

(defn calculate-game-score [frame_scores]
    (reduce + frame_scores))

(defn score [rolls]
     (calculate-game-score (map calculate-frame-score (make-game rolls))))

While there’s probably a better way to implement this, I really learned from following the TDD flow. I’ve used Sublime Text as my editor along with the SublimeREPL package. I found being able to run tests and execute functions directly from the REPL to be very powerful with Clojure. I probably just scratched the surface.

Until next time.

Thursday, December 19, 2013

Clojure Kata #1 – Fizz Buzz

I’ve been learning more about the ins and outs of Clojure lately, so I decided to practice a couple of katas. The obvious first one is the Fizz Buzz kata.

Here’s the code of my first take:

(defn fizz? [number]
    (zero? (rem number 3)))

(defn buzz? [number]
    (zero? (rem number 5)))

(defn fizz-buzz? [number]
    (and (fizz? number) (buzz? number)))

(defn fizz-buzz [number]
    (if (fizz-buzz? number) "fizzbuzz"
    (if (fizz? number) "fizz" 
    (if (buzz? number) "buzz" number))))

(defn run-fizz-buzz []
    (map fizz-buzz (range 1 101)))

 

For this first attempt, I set out the goal to come up with some very small functions to accomplish the task at hand. This kind of worked out fine except for the fizz-buzz function, which doesn’t feel very idiomatic to me probably caused by the nested if statements.

So for the second attempt, I tried  to get rid of these if statements, and this was the result:

(defn fizz? [number]
    (zero? (rem number 3)))

(defn buzz? [number]
    (zero? (rem number 5)))

(defn fizz-buzz? [number]
    (and (fizz? number) (buzz? number)))

(defn fizz-buzz [number]
    (cond
        (fizz-buzz? number) "fizzbuzz"
        (fizz? number) "fizz"
        (buzz? number) "buzz"
        :else number))

(defn run-fizz-buzz []
    (map fizz-buzz (range 1 101)))

 

Using the cond macro I was able to remove the nested if statements, and it also slightly reduced the number of parenthesis. So far, so good.

But maybe having so many functions is overkill. So for the next take I wanted to get rid of the fizz?, buzz? and fizz-buzz? functions and move the meat into the fizz-buzz function itself without giving up too much on readability. Here’s the code of my third attempt:

(defn fizz-buzz [number]
    (let [fizz? (zero? (rem number 3))
          buzz? (zero? (rem number 5))
          fizz-buzz? (and fizz? buzz?)]
        (cond
            fizz-buzz? "fizzbuzz"
            fizz? "fizz"
            buzz? "buzz"
            :else number)))

(defn run-fizz-buzz []
    (map fizz-buzz (range 1 101)))

Here I used the let special form in order to define the fizz?, buzz? and fizz-buzz? lexical parameter bindings that can then be used inside the expression of the function body. Notice the conciseness of this last attempt compared to the first.

There’s probably someone that could write a single-line function in Clojure that does exactly the same thing, but that’s not the point. Code katas are fun!

Until next time.

Thursday, December 12, 2013

Node-m-r – A Simple CQRS Example using Node.js

Anyone learning about DDD, CQRS and/or event sourcing has probably read the source code of Greg Young’s simple CQRS example at some point or another. This is one of the simplest examples possible demonstrating the practical side of CQRS and event sourcing, originally developed using .NET.

I made an attempt to rewrite the source code of this example using Node.js, trying to apply as many features of the core library as possible, particularly streams and event emitters. For example, the implementation of the base aggregate root object uses an event emitter internally for applying events in derived objects. An aggregate root is also a writable stream, which is used by the repository for replaying events. The report aggregators are also writable streams, which receive events from the message bus.

I’ve put the resulting source code up on GitHub. This is still very much a work in progress though. Some improvements that I’m considering is spawning a separate child process for both the command handlers as well as the report aggregators. No data is being persisted at the moment, so everything is stored in memory. Adding physical data stores is something that I’m also looking into. There’s no UI available either. You can run the example by executing the following command:

$ node src/application.js

Have a look at the source code and let me know what you think.

Until next time.

Thursday, December 05, 2013

Either.js

Some time ago, I was watching this excellent video course by Neal Ford titled “Functional Thinking – Functional Programming using Java, Clojure and Scala”. In one of the modules on functional data structures, Neal talks about this container type in Scala named Either.

The Either type in Scala represents one of two possible values, a Left value or a Right value.  The convention dictates that the Left value is used for error or exception objects, while the Right value is used for normal values. Why is this useful?

In many programming languages, like Java, C#, JavaScript, Scale, etc. …, there are generally two ways a function or method call returns something. The first one is through a regular return value that is returned by a function/method, be it some primitive type or some object. The second one is by throwing an exception, which means that the return value of the  function/method in question is not available to the calling code. In order to deal with this dual behavior, the calling code has to verify/use the value returned when invoking a function or method as well as dealing with possible exceptions  that this function/method might throw.

This is where the Either type comes in, unifying both behaviors by offering a Left value containing a possible error/exception object or a Right value containing the regular output of a function/method. In his course, Neal further elaborates on this concept by implementing a similar Either type in Java using interfaces because Java has no support for lambda’s yet (ouch).

While working on some JavaScript code a while back, I ran into this situation where the code in question could really benefit by using the concept of an Either object. So I decided to implement such an object in JavaScript and extracted the result into a Node.js module named either.js that I recently published on npm.

Take a look at the following code sample:

_this.insertReport = function(dataArea, inventoryReport, callback) {
    simulateAsynchronousIO(function() {
        var reportsCollection = _dataAreas[dataArea];
        if(!reportsCollection) {
            var error = new InvalidDataAreaError('The specified area is unknown.');
            callback(error);
            return;
        }

        reportsCollection.push(inventoryReport);
        callback();
    });
};

There were a couple of functions like this one where an array was being retrieved from an object named _dataAreas which contains a number of array properties. In case the specified property name could not be found on the containing object, the callback was invoked with an error object. Using an Either object, this code got revamped into something like this:

_this.insertReport = function(dataArea, inventoryReport, callback) {
    simulateAsynchronousIO(function() {
        getReportsCollectionFor(dataArea).fold(
            function left(error) { 
                callback(error); 
            },
            function right(reportsCollection) {
                reportsCollection.push(inventoryReport);
                callback();        
            }
        );
    });
};

function getReportsCollectionFor(dataArea) {
    reportsCollection = _dataAreas[dataArea];

    if(reportsCollection)
        return either.right(reportsCollection);
    else
        return either.left(new InvalidDataAreaError('The specified area is unknown.'));
}

I admit that there’s definitely some getting used to at first, but I find this approach to be very clean and useful. In fact, this kind of reminds me to the beauty of using promises for asynchronous code, but for synchronous code instead.

Until next time.

Monday, October 14, 2013

Detecting the End of a Rainbow Inside a Writable Stream

I was implementing a custom writable stream in Node.js the other day when I ran into this issue where I wanted to know whether more data was coming or that we were actually done writing stuff. When looking at the recently revised API for stream implementers, the only thing that gets mentioned in the docs is the _write method.

After some intensive DuckDuckGoing (yes, this might become an actual word in the future), I ran into this thread on the Node.js user group and this issue on GitHub. In short, it is possible and even advisable to listen to the ‘finish’ event inside a Writable stream. Apparently, this happens all the time in the Node.js library itself. This means that by catching the ‘finish’ event, I was able to implement the flush functionality that I was looking for.

So without further ado, here’s a simple example of a custom Readable and Writable stream that are piped together. The Writable stream listen to the ‘finish’ event in order to flush stuff down the drain.

var stream = require('stream'),
    util = require('util');

//
// Reading stuff
//

var ReadingStuff = function() {
    this._data = [1, 2, 3, 4, 5];

    stream.Readable.call(this);
};

util.inherits(ReadingStuff, stream.Readable);

ReadingStuff.prototype._read = function() {
    if(0 === this._data.length) {
        this.push(null);
        return;
    }

    this.push(this._data[0].toString());
    this._data.shift();
};


//
// Writing stuff
//

var WritingStuff = function() {
    stream.Writable.call(this);

    this.on('finish', function() {
        console.log('Finished writing stuff!!');
    });
};

util.inherits(WritingStuff, stream.Writable);

WritingStuff.prototype._write = function(chunk, encoding, next) {
    console.log(chunk.toString(encoding));
    next();
};

//
// Application
//

var readingStuff = new ReadingStuff();
var writingStuff = new WritingStuff();

readingStuff.pipe(writingStuff);

 

Notice that inside the _read method of our custom Readable stream, we call this.push(null) in order signal the end of the data.

Hope this helps. 

Thursday, August 15, 2013

Taking Toddler Steps with Node.js – The Towering Inferno Revisited

Soon after I started using Node.js, I ran into the phenomenon of multiple nested callbacks that create some kind of horizontal tower effect. The solution I came up with in order to improve the readability of my code was using a library called step, as described in this blog post that I wrote at that time.

Over the past years I switched over to a couple of other control flow libraries that solve the same problem as step, but  eventually I settled on using the async library.

Let’s look back at the problem I used in my original blog post:

image

Here’s the slightly refactored equivalent using async:

http.createServer(function(request, response) {
    async.waterfall([
        assembleFilePath,
        readFavoritePodcastsFromFile,
        addNewFavoritePodcastToFile
        ], 
        function(error, favoritePodcasts) {
            if(error)
                return response.end(error);

            response.writeHead(200, {
                'Content-Type': 'text/html', 
                'Content-Length': favoritePodcasts.length
            });

            response.end(favoritePodcasts); 
        }
    );
})
.listen(2000);

function assembleFilePath(callback) {
    var filePath = path.join(__dirname, 'podcasts.txt');
    callback(null, filePath);
}

function readFavoritePodcastsFromFile(podcastsFilePath, callback) {
    fileSystem.readFile(podcastsFilePath, 'utf8', function(error, data) {
        if(error)
            return callback(error);

        callback(null, podcastsFilePath, data);
    });                     
}

function addNewFavoritePodcastToFile(podcastsFilePath, favoritePodcastData, callback) {
    var favoritePodcasts = favoritePodcastData;

    if(-1 == favoritePodcasts.indexOf('Astronomy Podcast')) {
        favoritePodcasts = favoritePodcasts + '\n' + 'Astronomy Podcast';       
        fileSystem.writeFile(podcastsFilePath, favoritePodcasts, function(error) {
            if(error)
                return callback(error);

            callback(null, favoritePodcasts);
        });                     
    }
    else {
        process.nextTick(function() {
            callback(null, favoritePodcasts);
        });     
    }
}

 

Here I’ve used the waterfall method of the async library in order to pass results from one function to the next. Other functions that I often use are series and parallel. Notice that in the addNewFavoritePodcastToFile function I used process.nextTick instead of just invoking the callback. This is done in order to prevent inconsistent behavior of the function. I also wrote about this in the past.

There has been a lot of buzz lately around promises, so I decided to drink some of this kool-aid. Basically, we can achieve the same kind of solution as with the async library.

http.createServer(function(request, response) {

    assembleFilePath()
    .then(readFavoritePodcastsFromFile)
    .then(addNewFavoritePodcastToFile)
    .then(function(favoritePodcasts) {
        response.writeHead(200, {
            'Content-Type': 'text/html', 
            'Content-Length': favoritePodcasts.length
        });

        response.end(favoritePodcasts); 
    })
    .done();
})
.listen(2000);

function assembleFilePath() {
    return Q.fcall(function() {
        return path.join(__dirname, 'podcasts.txt');
    });
}

function readFavoritePodcastsFromFile(podcastsFilePath) {
    var deferred = Q.defer();

    fileSystem.readFile(podcastsFilePath, 'utf8', function(error, favoritePodcasts) {
        if(error)
            return deferred.reject(new Error(error));

        deferred.resolve({
            favoritePodcasts: favoritePodcasts,
            podcastsFilePath: podcastsFilePath
        });
    });

    return deferred.promise;
}

function addNewFavoritePodcastToFile(data) {
    var deferred = Q.defer(),
    favoritePodcasts = data.favoritePodcasts;

    if(-1 == favoritePodcasts.indexOf('Astronomy Podcast')) {
        favoritePodcasts = favoritePodcasts + '\n' + 'Astronomy Podcast';       
        fileSystem.writeFile(data.podcastsFilePath, favoritePodcasts, 
        function(error) {
            if(error)
                return deferred.reject(new Error(error));

            deferred.resolve(favoritePodcasts);
        });                     
    }
    else {
        process.nextTick(function() {
            deferred.resolve(favoritePodcasts);
        });
    }

    return deferred.promise;
}

 

I’ve used the Q library for this code sample. For an excellent introduction to promises and the Q library, check out this great article on the StrongLoop blog. I think the approach using promises looks, uhm … promising as well.

Are you, dear reader, using a control flow library, which one and why?

Until next time.

Saturday, July 27, 2013

Byte Magazine Archives

Earlier this week I accidentally stumbled on archive.org where they are hosting an extensive backlog of old issues of Byte magazine. This magazine was an American microcomputer magazine, influential in the late 1970s and throughout the 1980s (and well before my time).

I’ve been skimming through a couple of these old magazines, and I got quite intrigued by some of these old topics. Probably one of the more popular issues was the one on Smalltalk, but there are others on Lisp, Pascal and several varying topics as well. These old issues have been scanned in and can be downloaded as a PDF, ePub, Mobi, etc. … .

If you’re as interested in computer history as I am, then you should definitely check these out.

Friday, July 19, 2013

Seven Languages in Seven Weeks

A while ago, I was thinking about which programming language I wanted to learn next. At first I was doubting between Scala, Ruby or perhaps Clojure. But I couldn’t really make up my mind. So I decided to pick up Seven Languages in Seven Weeks. This way I was able to take a small bite from a couple different cakes in order to decide which flavor I liked the most.

The seven languages that are discussed  in this book are Ruby, Io, Prolog, Scala, Erlang, Clojure and Haskell.

I really liked Ruby and it’s nice syntax as this programming language is not a complete stranger to me.

I had great hopes for Scala, but it kind of left me disappointed. While exploring the syntax of this academic newcomer, I had this constant nagging feeling that Scala reminded me too much of C++. Don’t get me wrong, I do like C++. But while C++ was intended to bridge the gap between procedural programming (C) and object-oriented programming, Scala does seem to bring the same kind of awkwardness as it tries to bridge the gap between object-oriented programming and functional programming. I still find it a really fascinating language to learn, but it kind of fell down in my personal ranking.

The big surprise for me was Erlang. Although it’s syntax has its quirks, this is a truly powerful and fascinating programming language that piqued my interest.

Clojure was kind of was I expected and brought back some fine memories from the small, entry-level Lisp programs I wrote back when I was in college.

While I was reading this book, I briefly jumped into some other programming languages as well. Go, D and yes, even Smalltalk are other programming languages that sparked my interest.

I very much enjoyed reading this book and had a lot of fun trying to do those small exercises at the end of each day. The only down-side now is that the list of programming languages that I want to take a closer look at basically doubled. Oh well, nothing that flipping a coin can’t handle.

I want to end this blog post with the question: What programming language are you learning at the moment?

Until next time.

Friday, May 31, 2013

Taking Baby Steps with ScriptCS

I’ve been following the ScriptCS project with great interest over the last couple of months. As you may know by now, I’ve been searching for a more lightweight .NET development experience for quite some time. The ScriptCS project is here to fulfill this desperate need. Driven by the open-source .NET community, this wonderful initiative promises to unleash C# from Visual Studio which is exactly what I’ve been looking for.

Being heavily inspired by the node.js development workflow, ScriptCS is built on top of Roslyn and makes heavy use of NuGet for installing packages and script packs. You can get up and running in no time by installing ScriptCS using Chocolatey. If you didn’t have Chocolatey installed already (like me), then it can be easily done by running the following Powershell command:

@powershell -NoProfile -ExecutionPolicy Unrestricted -Command "iex ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin 

Now we can install ScriptCS by simply running the following command:

cinst scriptcs

That’s all we need!

Now in order to do something useful with it, we can clone the samples repository to our dev machine and try to run the Nancy sample application. Navigate to the Nancy folder in a command prompt and run the command “scriptcs -install” to install all package dependencies defined in the packages.config file. Next run the following command to start the web application:

image

Now open up your favorite browser, navigate to http://localhost:1234 and et voilĂ :

image

Amazing isn’t it?

I do recognize that the project is still very much a work in progress. Also something that’s not entirely clear for me is the deployment story. The road map mentions some kind of export functionality to an .exe or a Visual Studio Project. In any case, there should be an easy way to convert the code files into binary form.

Something that I also would love to see is support for Mono. How cool would it be to develop and run C# code using ScriptCS on Mac and Linux?

The future for ScriptCS certainly looks bright and I’m very much looking forward to where this particular road is taking the .NET universe. Take a look at the wiki and start familiarize yourself with ScriptCS as it’s going to open up very interesting opportunities for future .NET development.

Until next time.

Friday, May 03, 2013

Introducing node-validation

Some time ago I was looking for a validation library/module for use in a small Express application that I was writing at the time. I couldn’t find anything that suited my taste so I decided to write one myself just for kicks. The goal was learning how to publish a module to npm and making a futile attempt to contribute something back to the vibrant Node.js community. node-validation is a minimal but slightly opinionated validation library for Node.js.

Installing node-validation can be done using the canonical package manager:

$ npm install node-validation

Validation rules must be defined in a custom validator by deriving from the base Validator.

var MyObjectValidator = function() {
    Validator.call(this);

    this.ruleFor('stringProperty').isNotEmpty();
    this.ruleFor('otherStringProperty').hasMaximumLength(10);

    this.ruleFor('numericStringProperty').isNumber()
        .withMessage('Oops, something is wrong ...');
    this.ruleFor('dateStringProperty')
        .matches(/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/);

    this.ruleFor('numberProperty').isInteger();
    this.ruleFor('otherNumberProperty').isMaximum(5);

    this.ruleFor('exoticProperty').is(function(value) {
        return 3 === value.propertyA + value.propertyB;
    }).withMessage('Either propertyA or propertyB has an incorrect value.');
};

util.inherits(MyObjectValidator, Validator);

After creating a validator object, an object that needs to be validated (the subject) can be passed to the validate method. The validate method returns an array of validation errors specifying a message and the name of the violating property.

//
// Validation subject
//
var subject = {
    stringProperty: '',
    otherStringProperty: 'Some string value that is too long ...',

    numericStringProperty: '65.85 invalid',
    dateStringProperty: '2013-04-30 invalid',

    numberProperty: 'Some invalid number',
    otherNumberProperty: 48,

    exoticProperty: {
        propertyA: 1,
        propertyB: 1
    }
};

//
// Now it's time to validate
//
var validator = new MyObjectValidator();
var validationErrors = validator.validate(subject);

for(var i=0; i < validationErrors.length; i++) {
    console.log('Property name: ' + validationErrors[i].propertyName 
                + ', Message: ' + validationErrors[i].message);
}

There you go. Head over to the GitHub repository and give it a try. I’m definitely looking forward to hear your feedback.

Friday, March 22, 2013

Basic JavaScript: Prototypical Inheritance vs. Functional Inheritance

Inheritance in JavaScript has been the topic of many discussions in the past and will continue to be the source of future debates and arguments. While we do value composition over inheritance, we don’t want to throw the baby out with the bathwater either. So, from time to time, we run into these cases where we want some notion of inheritance in JavaScript. Now what?

As with many things in JavaScript, there is not a single straight answer. We can choose between a couple of options and many different variations of these solutions. But one thing’s for sure: we can’t have it all!

In this blog post I want to discuss two different styles of inheritance that I have a hard time choosing from when programming JavaScript. And as with everything in life, both styles have their own pros and cons. 

Prototypical inheritance

In ‘classical’ programming languages, one class can directly inherit from another class. JavaScript doesn’t have this notion of classes (yet). Instead, JavaScript has prototypes which you can augment to fit your own needs. This means that having a single augmented object as the prototype for other objects, which ‘inherit’ all members of the augmented prototype object, kind of simulates a pseudo-classical inheritance pattern. Let’s talk code in order to demystify this concept.

// validator.js
var Validator = exports.Validator = function() {
    this._rules = [];
};

Validator.prototype.addRule = function(rule) {
    this._rules.push(rule)
};

Validator.prototype.validate = function(instance) {
    ...
};

// specificValidator.js
var util = require('util');

var SpecificValidator = function() {
    Validator.call(this);
};

util.inherits(SpecificValidator, Validator);

SpecificValidator.prototype.filter = function(instance) {
    ...
};

// client.js
var validator = new SpecificValidator();

// Calls function on derived object
validator.filter( { ... } );        

// Calls function on base object
validator.validate( { ... } );        

Here we have a constructor function named Validator which is the base object for other ‘derived’ objects. We augment the prototype with two functions (addRule and validate). Next we define another constructor function named SpecificValidator. We ‘derive’ this new  constructor function by calling the base constructor function and wiring the prototype by using the util.inherits() function from the Node.js core library.

We have to use the new keyword in order to instantiate a SpecificValidator object. Now we can use the functions that we added to the prototype.

Functional inheritance

This pattern is advocated by Douglas Crockford in his book JavaScript, The Good Parts. There he offers this particular style as the way to go for inheriting objects. Let’s look at an example.

// validator.js
module.exports = function() {
    var rules = [], my = {};

    my.addRule = function(rule) {
        rules.push(rule);
    };

    my.validate = function(instance) {
        ...
    };

    return my;
};

// specificValidator.js
var validator = require('...').validator;

var specificValidator = function() {
    var my = validator();

    my.filter = function(instance) {
        ...
    };
    
    return my;
};

// client.js
var validator = specificValidator();

// Calls function on derived object
validator.filter( { ... } );    

// Calls function on base object
validator.validate( { ... } );        

The base constructor function returns an object that is augmented with functions and is returned at the end. The derived constructor function simple calls the base constructor function and further augments the retrieved object before returning it to the calling code. Here we don’t have to use the new keyword to instantiate anything. Just calling the right constructor function gives us an object which we can use in our client code.

Conclusion

The most important benefit of prototypical inheritance, at least in my humble opinion, is performance. By augmenting the prototype with functions, we only create these functions once. Not matter how many times we instantiate a constructor function, the same functions get (re)used every single time. Functional inheritance on the other hand creates new functions every time a constructor function is called, which is several orders of magnitude slower compared to the prototypical inheritance pattern.

On the other hand, the prototypical approach doesn’t come with encapsulation. Looking at the example shown earlier, the ‘_rules’ property is publicly available to the client code and can be manipulated at will. By using a simple convention, like prefixing with an underscore, we can indicate that these private members should not be touched in order to guarantee a correct behavior. But again, nothing can be enforced. Using functional constructors, we can have private variables and functions that cannot be manipulated by the calling code.    

There are more pros and cons, but for me, these are the most important ones to be aware of. You can see that both styles have their strengths and weaknesses. I usually tend to go with prototypical inheritance as this is the ‘JavaScript way’, but I like using the functional approach as well for those cases were I know in advance that not too many objects are created or when I don’t care about performance.

I would love to hear other takes on this. What particular styles do you use? When do you use them and why?

Until next time.

Friday, February 22, 2013

Confessions of a Sublime Text-aholic

It’s true. I’m a Sublime Text addict. It’s by far my favorite development tool. End of story!

Just to illustrate, earlier this week, a member of our development team asked how to quickly remove all empty lines from a very large text file. I quickly came up with the following:

  1. Press CTRL-F.
  2. Enable regular expressions (the button entirely in the bottom-left corner).
  3. Search for ^\s*$
  4. Press ALT-ENTER (click on the “Find all” button).
  5. Hit the backspace button.
  6. Done!
  7. Be merry …

Don’t just take my word for it. Just start using it!

Friday, February 08, 2013

Writing Fast, Memory-Efficient JavaScript

Earlier this week, I read this great article titled “Writing Fast, Memory-Efficient JavaScript” by Addy Osmani. This is a highly recommended read for anyone involved in writing JavaScript code.

The topics that I found to be particularly interesting were the apparent fact that it’s better to avoid the delete keyword and cached functions in the module pattern. The major down-side that I see when using cached functions is that you can’t have any private variables within your module. But this is highly interesting stuff, nonetheless.

Friday, February 01, 2013

Taking Toddler Steps with Node.js - Express Routing Revisited

Last year I wrote this blog post where I described a couple of ways on how to tackle routing with Express. In the mean while I moved on from the “Plain Old School” approach to an approach where I replaced underscore.js with node-require-directory.

Setting up node-require-directory is quite easy. In the routes folder, we just need to add an index.js module with the following two lines:

var requireDirectory = require('require-directory');
module.exports = requireDirectory(module);

Setting up the routes for Express then looks like this:

var routes = require('./../routes');

// Setting up an application ...

application.get('/', routes.root);
application.get('/home', routes.home);
application.get('/signin', routes.authentication.signin);
application.post('/signout', routes.authentication.signout);

// More route registrations

Here we simple reference the index.js module. The node-require-directory module takes care of building up a tree of functions which we can now access for our route registrations. Adding a new route is as simple as creating a new module somewhere inside the routes folder or one of its subfolders and creating a new route registration. Have a look at this example.

I found this little gem to be quite useful and it might be helpful for some of you as well.

Until next time.