4

TL/DR: When requiring another script in Node.js and defining it as a const should the variable name still be in camelCase like it was usual with var or should it instead be in SCREAMING_SNAKE_CASE as is more usual for a const?

const someVariable = require('...js');

vs

const SOME_VARIABLE = require('...js');

Longer version: In Node.js another script is usually imported using

var someScript = require('path/to/someScript.js');

With ES6 being available in Node.js such a var could instead be declared as a const (or in some cases as a let if it's ever redefined).

Now as we're working on defining a style guide for our development we came to the conclusion in other languages like Typescript (Angular 2) that whenever we declare a const we write the variable name in SCREAMING_SNAKE_CASE like

const SOME_CONSTANT_VARIABLE = '123';

Now we would conclude that this means that in Node.js we should define our script variables like

const SOME_SCRIPT = require('path/to/someScript.js');

As you're usually using those script variables a lot in Node.js code can become quite difficult to read. Also I couldn't see a lot of examples of other people doing it similarly and instead often use it like

const someScript = ...;

so still writing the variable name in camelCase.

My question is as a What is the preferred / best practice way of writing those variable names using ES6 const?

asked Sep 14, 2017 at 11:53
4
  • 1
    I've never heard it called SCREAMING_SNAKE_CASE before but 1) that's hilarious and 2) that gives me great mental images. Commented Sep 14, 2017 at 12:36
  • "Constant" doesn't necessarily mean a const variable, but a value that will be unchanged for all runs of the program – basically named literals. Often the value of constants is known at design time. Reserving UPPERCASE variables for these kinds of constants might be a good idea. After all, nearly all vars could usually be const. Commented Sep 14, 2017 at 13:50
  • 1
    The 'SCREAMING_SNAKE_CASE' had traditionally been used only for pre-processor macro identifiers in languages such as C, where the pre-processor is a pre-compilation step which operates before the compiler itself starts running. A pre-processor step essentially re-writes source code before feeding into the compiler. Pre-processor macros do not follow the same "rules" as the rest of the language syntax; all-uppercase identifiers put a clear distinction so that programmers can instantly recognise the difference between compiled statements and pre-compiiled statements. Commented Sep 15, 2017 at 5:30
  • 1
    Fabulous question. We are currently facing the very same problem. Commented Jan 25, 2018 at 13:31

1 Answer 1

7

You are not really declaring a variable, but what other languages call an alias.

Example in Python:

import numpy as np

Example in C#:

using Project = PC.MyCompany.Project;

For your case, from the official node.js module API documentation:

const square = require('./square.js');
const mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);

Or

console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done=%j, b.done=%j', a.done, b.done);

Different languages use different conventions. The official node.js API documentation uses camelCase, you should use camelCase.

answered Sep 14, 2017 at 12:35
1
  • Thank you for the quick and expressive answer. That's kind of what I thought but I haven't had any written proof for it. Didn't notice the official documentation was already updated to use const. Commented Sep 14, 2017 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.