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.