Coding Standards Variables

From BOARD18 Project WIKI
Jump to navigation Jump to search
Coding Standards Index edit


BOARD18 uses var to define all JavaScript variables. This practice should be continued if only for consistancy.

There are a lot of "best practices" documents on the web that warn coders to avoid variables that are scoped outside of any function. OOP is all about "locality of reference". This makes sense if you are including libraries which may, by chance, use the same variable names. That can cause chaos.

But BOARD18 is a maverick in this respect. JavaScript functions in BOARD18 often access "global" variables. Information is passed in massive structures that are updated in various places.

BOARD18 uses the following trick to partially mitigate this potential problem.

All board18 'global' variables are contained in one 'master variable' called BD18. This isolates them from global variables in other packages.

At the front of the board18com.js file, which is included in every BOARD18 page, is the following 5 lines of code:

var BD18 = {};
BD18.noteTimeout = null; // Used by doLogNote().
BD18.welcomename = null; // Used by doLogNote().
BD18.help = "https://wiki.board18.org/w/Player%27s_Guide_V2.5";
BD18.version = "2.5.11";

Thus the BD18 object becomes the only global variable in BOARD18. Many objects and arrays will be added to this object. But it still remains only one variable.