This is a pet peeve of mine.
I think the code is the documentation, and names of variables, functions, and classes used in the code are important. These names communicate the purpose of the things being named.
It makes sense that Node’s package manager is called “npm” because that denotes “node package manager.” Easy to decipher, and if you know what a package manager is, then you know what npm is. No need to think about it.
The names of variables in a program implemented in any particular language are also important. i and j indicate loop indices. Variables like req and res might indicate a request and a response object, respectively, though in many cases I would prefer to just see request and response.
Ok
var http = require('http'), server = http.createServer(); function handleRequest(req, res) { res.writeHead(200, { 'content-type': 'text/plain'}); res.write('Hello, World!'); res.end(); } server.on('request', handleRequest); server.listen(8080);
Better
var http = require('http'), server = http.createServer(); function handleRequest(request, response) { response.writeHead(200, { 'content-type': 'text/plain'}); response.write('Hello, World!'); response.end(); } server.on('request', handleRequest); server.listen(8080);
Or, if you look at Java, something like an exception is usually named with an e, or an exc, sometimes followed by a numeric suffix. Like so:
try { arf me = new arf(args); me.Run(); } catch (java.lang.Exception exc1) { System.out.println("Exception:" + exc1.toString()); exc1.printStackTrace(); }
It makes sense.
To err is human.
Which brings me to the point. There are numerous examples showing how to code a callback function in Node, that look like this:
fs.readFile('/etc/passwd', function (err, results) { if (err) { handleError(err); return; } console.log('File contents:', results); });
As you can see, the variable that holds the error is called err. Now if English is one of the languages you use, which is probably true if you are reading this, then you probably know that “error” is a noun and “err” is a verb.
Using the verb form of that word as a “shorthand” for the noun is confusing to code reviewers, and therefore wrong. Yes, everyone is assumed to know that “err” really refers to an error, but everyone incurs a small but not negligible mental burden in reconciling that difference, every time they look at that variable. Why? To save 2 characters? If really we are economizing on variable name length, and I respect efforts to do so, then why not just use the letter e, which is often used to represent errors or exceptions?
Either e or error is preferred over err.
An error does not become truth by reason of multiplied propagation, nor does truth become error because nobody sees it. – Mahatma Gandhi
Evan Plaice 2013-December-4
It’s just a language-wide style convention. Technically, since you’re declaring the callback you can name the argument whatever you want.
Personally, I think it’s a causes a lot less brain damage then some of the common conventions found in other languages.
Such as testing for -1 to discover errors in C. Or checking for null in C# (which makes up approx 10% of all C# code /s).
IMHO, explicitly defining a way to return error info to the caller is a good design.
dpc 2013-December-6
I get that it’s a convention. That’s the reason for the rant. In my ivory tower, nouns get noun names and verbs get verb names. The thing getting returned is an error code. A noun. Therefore the name ought to be a noun, and it especially shoudn’t be a verb. To err is human, but this is an unnecessary complexification of the “language wide convention”.
Isaiah Odhner 2015-March-25
And npm uses the ‘bin’ field to specify the “binary” folder where it searches to find textual scripts to link to a .bin folder to make them executable. Hey, why not call it ‘executables’?