1

I prefer to declare all global variables at the beginning of my code using self-explanatory, full-named variables:

var $interval_start_dates          = [];    // date intervals
var $interval_end_dates            = [];

var $interval_start_milli          = [];    // convert dates to milliseconds
var $interval_end_milli            = [];

var $interval_start_milli_sorted   = [];    // sort millisecond dates
var $interval_end_milli_sorted     = [];

This naming convention often results in long variable-names which I find unhandy when using them in the code. Therefore I also prefer to use abbreviations for the variables in the code:

var _isd  = $interval_start_dates;
var _ied  = $interval_end_dates; 

var _ism  = $interval_start_milli; 
var _iem  = $interval_end_milli;

var _isms = $interval_start_milli_sorted;
var _iems = $interval_end_milli_sorted;

My questions in this regard are the following:

(1) Is it possible to output the intermediate variable (e.g. $interval_start_dates) using the abbreviation (e.g. _isd)?

(2) Does this naming convention result in worse performance of the code (e.g. speed) and if so is there a better way to use abbreviations?

One remark: I know that I could just use comments to inform about the full name of an abbreviated variable. But this means that I would have to repeat those comments many times in my code. I am looking for a more "slick" solution which allows me to use for example console.log to display the full name of a variable (if this is possible).

rabbitco
  • 2,790
  • 3
  • 16
  • 38
  • If the issue is that you get bored typing out long variable names every time you need to use them, find an editor like Sublime Text that stores variable names and allows you to auto-complete them from a dropdown list when you start typing the name. IMO, your variable names should be descriptive and abbrebriations don't allow for that. Imagine someone else trying to make sense of your code. – Andy May 12 '15 at 09:45
  • I use brackets which allows for auto-completion. But I think it is easier to maintain a good overview of code in a compact form - and long variable names work against that. But I know the IMO - thats why I need a solution which makes it easy to obtain the full variable name. – rabbitco May 12 '15 at 09:47

3 Answers3

3

why not use some data structure. the advantage is the more readable format and you can use abbreviation for accessing the variables.

var interval = {
    start: {
        dates: [], // date intervals
        milli: [], // convert dates to milliseconds
        milli_sorted: [] // sort millisecond dates
    },
    end: {
        dates: [],
        milli: [],
        milli_sorted: []
    }
};
// use
var ie = interval.end;
alert(ie.dates);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    That is brilliant thinking. I mark this answer as the most helpful because it helps me both: (1) obtaining an even better overview of my data; (2) using abbreviations to access that data in my code and thereby keeping the code more compact and - in my view - easier to read. Thank you. – rabbitco May 12 '15 at 10:23
  • 1
    Excellent lateral thinking. – T.J. Crowder May 12 '15 at 10:35
  • @nina school thank very much for your answer. The suggested data structure has been implemented. However from the perspective of my question T. J. Crowder has provided the correct answer. Your data structure is a variant of my own solution - all be it a much better one! – rabbitco May 12 '15 at 13:26
1
  1. Is it possible to output the intermediate variable (e.g. $interval_start_dates) using the abbreviation (e.g. _isd)?

Yes. They both refer to the same array, so it doesn't matter which you use to output the array.

(2) Does this naming convention result in worse performance of the code (e.g. speed) and if so is there a better way to use abbreviations?

No. They both refer to the same array, each is just as direct a reference as the other.

But, there are other potential issues. For instance, if you had need of filtering or mapping those arrays, it would be really easy to update one reference and forget the other, e.g.:

_isd = _isd.filter(function(entry) { return entry.foo < bar; });
// OOOPS! Now `_isd` and `$interval_start_dates` don't refer to the same array anymore!

If it's just typing out the names, any good IDE can make that easier for you.

I am looking for a more "slick" solution which allows me to use for example console.log to display the full name of a variable (if this is possible).

I'm not sure what you're asking here, but if you mean that you want to use _isd somewhere (for instance, a function) and have that function display $interval_start_dates in its output instead of _isd, then you can't (reasonably) do that. There is no link between _isd and $interval_start_dates, other than that they refer to the same array. But then, you can't do that anyway — if you passed $interval_start_dates into a function, it still can't infer the name of it, because the function's argument that receives the value isn't linked in any way to $interval_start_dates (other than that they refer to the same array).


Side note: If all of these things are arrays, and you want to have some way of knowing a "name" of the array based on an array reference, you can take advantage of the fact that standard arrays in JavaScript aren't really arrays at all, they're just objects and assign them a property with the name in, for instance:

var $interval_start_dates          = [];    // date intervals
$interval_start_dates.name = "$interval_start_dates";

// ...

var _isd = $interval_start_dates;
console.log(_isd.name); // "$interval_start_dates"

If you do that, be sure you're not using for-in to loop through the arrays (but there are better ways anyhow). Or use Object.defineProperty to define the name instead:

var $interval_start_dates          = [];    // date intervals
Object.defineProperty($interval_start_dates, "name", {
    value: "$interval_start_dates"
});

...and properties added that way are non-enumerable by default.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • ad 1) Was hoping it would be possible to output "$interval_start_dates" and not the array it refers to. But I guess your answers also answers that: both $interval_start_dates and _isd refers directly to the array :( – rabbitco May 12 '15 at 09:54
  • @David: Yes, that's right; there's no link between the two variables (except that they both point to the same array). I did just add something to the end that's array-specific, though, which might be useful. – T.J. Crowder May 12 '15 at 09:56
  • 1
    Thank you @T.J. Crowder. This was also very helpful. And thank you for the link to the very good overview of how to loops through arrays. I have starred it. – rabbitco May 12 '15 at 10:27
  • Upon further investigation and testing I have changed my decision and marked your solution, @T.J. Crowder, as the most helpful. It is the one that enables me to define abbreviations once and then access easily access the full name of the variable any where via for example console.log. Thank you! – rabbitco May 12 '15 at 13:24
0

Performance-wise this does not matter. Your code will becoming less readable though. So you will have to decide what is more important to you: readability or coding faster. You can achieve both though by using an IDE which autocompletes your variable names.

Also, most of the time your variable names can be made shorter while not losing any meaning, for example:

var $interval_start_milli could be changed to var interval_start_ms or even var start_ms, since an interval has a start and an end, which would mean you would either have var interval_ms or start_ms and end_ms. Where var interval_ms is an array of objects with a start and an end property.

But that also depends on which other global variables you have declared though.

Another thing is that you shouldn't rely on global variables too much in javascript.

keewnn
  • 600
  • 3
  • 8