I've been tasked with refactoring/simplifying the architecture of a (relatively) large node.js codebase and it makes ample use of global variables. I don't have much experience with javascript so just wanted to get a general sense of whether this is indeed super confusing and bad practise or if I'm in the wrong.
Here's a dummy example of a pattern I see throughout the codebase:
eg. 3 files: A.js, B.js, C.js:
A.js:
A_init = () => { // sets global variable a a = 1 } exports.A_init = A_init
B.js:
B_init = () => { // sets global variable b b = 2 } exports.B_init = B_init
C.js:
C_init = () => { // sets global variable c // depends on global variables a and b being set c = a + b } exports.C_init = C_init
In reality there are many more files in the library (around 40) and some are thousands of lines long with random functions spread throughout the code which set variables at a global level which other library files have methods to utilise.
So for eg. if a developer wishes to utilise the library, they might write something like this:
UserScript.js
const A = require("./A"); const B = require("./B"); const C = require("./C"); A.A_init() C.C_init() B.B_init() someMethod = () => console.log(a, b, c) // expected: 1, 2, 3 actual: ReferenceError: b is not defined
My experience of using the codebase is these sorts of ReferenceErrors surface very frequetly due to people either not knowing they need to call certain functions in order to set global variables other functions depend on or calling them in the wrong sequence.
There are probably around 50-100 such global variables and it's a nightmare to try and extract out... If this question is too vague I appologise, but what strategies would you suggest I adopt to address this? Is some use of global variables like this acceptable? Are there standards people adopt to constrain their usage? Would adopting a more OO approach and using classes/constructors be something you would recommend here?
what strategies would you suggest I adopt to address this?
Address what? the refactor or the amateurship?