Saturday, May 21, 2016

Revisibility in Node declarations

One of the things that strikes me about Node.js is how variable declarations are done messily in the canon. Consider:

  var express = require('express'),
      app = express();

For some reason, Node programmers (or is that JS programmers) don't want to use too many var keywords. Perhaps there is a shortage.

Problem is, when it comes time to add in a new variable, we're up against revisibility problems. To add another variable thing,

  var express = require('express'),
      app = express(),
      thing = 'thing';

involves changing two lines, because of the need to add a comma at the end of the second line to continue the variable declarations.

This is just nuts. Working with extant code - reading it, changing it - is always more important than writing it. The remedy is simple: use one var per line (as has been advocated strongly for pretty much every other C-family language since the dawn of time). Now, changing

  var express = require('express');
  var app = express();


  var express = require('express');
  var app = express();
  var thing = 'thing';

is at maximum revisibility, and your revisiology (which is something you will do a lot if you are writing code professionally, or in teams, or in professional teams) will be much eased.

No comments:

Post a Comment