3

When importing modules in Node.js, it's fairly common (and greatly aids in readability) to use capitalization when importing classes and camel case when importing anything else:

let Transform = require('stream').Transform;
let util = require('util');
let fs = require('fs');

And yet, imported modules really should be constants, as it makes little sense to allow the value of a function or class from an imported module to change:

const Transform = require('stream').Transform;
const util = require('util');
const fs = require('fs');

But naming conventions would dictate that constants should always use all caps and underscores:

const PI = 3.14159265358979;
const AVOGADRO_NUMBER = 6.02214086e23;

So therein lies a conflict: It is typically semantically confusing to use all caps and underscores in naming imported modules, and yet it is semantically illogical to not make an imported module, class, or function a constant.

So either imported modules should use all caps and underscores, sacrificing some readability and knowledge of which role (function/variable or class) is filled by the imported module:

const RIEMANN_ZETA = require('riemann-zeta');
let nonTrivialZero = RIEMANN_ZETA(s);

Or imported modules should use camelCase or capitalization based on role, sacrificing congruence between constant status and notation:

const riemannZeta = require('riemann-zeta');
let nonTrivialZero = riemannZeta(s);

Or imported modules should be imported as non-constant, maintaining all naming conventions but sacrificing logical use of modules as immutable once imported:

let riemannZeta = require('riemann-zeta');
let nonTrivialZero = riemannZeta(s);

Which makes the most sense?

TheEnvironmentalist
  • 2,694
  • 2
  • 19
  • 46
  • 1
    This is subjective so it probably isn't the right place for this question, but I'll give you my take. I use caps for literal values like `PI = 3.1415` or `DEFAULT_TAB = 'search'` to avoid magic numbers and strings. For everything else that isn't mutable I use `camelCase` like I would any other variable. (Edit: for classes I use `PascalCase`) – DonovanM Jul 07 '17 at 02:26
  • This feels like the right mix of convention (camelCase and PascalCase for objects and classes, respectively), and logic (uppercase for constant literals). – Matt Jul 23 '18 at 18:14

0 Answers0