Nodebestpractices: Code style practices: Add required current folder notation

Created on 8 Nov 2017  路  5Comments  路  Source: goldbergyoni/nodebestpractices

After 3.9 I suggest to put required current folder notation:

Node.js required the ./ notation when you are requiring a module that is in the same folder. With multiple requires from different folders, you can easily put your self into writing it differently every time.

Always put the ./ notation in front of requires.

// Avoid
module.exports.ModuleInTheSameFolder = require('./ModuleInTheSameFolder');
module.exports.ModuleInAnotherFolder = require('../ModuleInAnotherFolder');
module.exports.AnotherModuleInAnotherFolder = require('../../../AnotherModuleInAnotherFolder');

// Do
module.exports.ModuleInTheSameFolder = require('./ModuleInTheSameFolder');
module.exports.ModuleInAnotherFolder = require('./../ModuleInAnotherFolder');
module.exports.AnotherModuleInAnotherFolder = require('./../../../AnotherModuleInAnotherFolder');

Great job gathering all that into a single place :)

Most helpful comment

Personal opinion, but I feel it _harms_ readability because at first glance, it appears that the required file in question is one additional level away.

Using the distance test: from a distance ./ and ../ are indistinguishable, which approximates "at a glance" visual parsing. One should be able to count the /s to know how many directory levels away a file is.

All 5 comments

@kbariotis thanks for joining the battle :)

can you kindly clarify why is this an issue? you also wrote 'always put ./' but in the //DO section the example doesn't contain ./:
module.exports.ModuleInAnotherFolder = require('../ModuleInAnotherFolder');

I am sorry @i0natan. They were upside down :/. So the issue is just a visual preference. IMO it enhances readability when you know that every require will start with ./.

Personal opinion, but I feel it _harms_ readability because at first glance, it appears that the required file in question is one additional level away.

Using the distance test: from a distance ./ and ../ are indistinguishable, which approximates "at a glance" visual parsing. One should be able to count the /s to know how many directory levels away a file is.

@jasonkarns I really don't want to argue about that cause it's really a matter of preference and opinion and it's only a suggestion. I like the paths to be similar, much like others aligning their = signs. :)

But, I would argue that ./ and ../ should be distinguishable especially to people coming from a *nix background. You don't count the /'s but rather the ..'s to find how many folders you have to go up.

Either way, nice talking to you :). Great repo!

@kbariotis I personally adore those aesthetic improvements. I'm not sure whether we should focus on 'widely agreed best practices' vs 'suggestions for better programming'. I tend to believe that the first fits our DNA better.

Anyway, would love to discuss other ideas :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bikingbadger picture bikingbadger  路  6Comments

Luifr picture Luifr  路  5Comments

goldbergyoni picture goldbergyoni  路  4Comments

YukiOta picture YukiOta  路  3Comments

noway picture noway  路  3Comments