Audite Marlow
Audite Marlow

Reputation: 1054

If else shorthand code for 3 situations

How would I go on about shortening the following piece of code?

if (typeof dIndex === 'undefined') {
    dPolylines = [];
} else {
    if (typeof rIndex === 'undefined') {
        dPolylines[dIndex] = [];
    } else {
        dPolylines[dIndex][rIndex] = [];
    }
}

I feel that these 8 lines of code can easily be converted to 3 lines maximum, but I have no proper idea on how to handle this.

I'm going to need this piece of code a couple of times throughout my code.

Upvotes: 0

Views: 89

Answers (5)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

To make it shorter without loosing any readability, I'd recommend just trimming it slightly to this...

if (typeof dIndex === 'undefined') {
    dPolylines = [];
}
else if (typeof rIndex === 'undefined') {
    dPolylines[dIndex] = [];
} else {
    dPolylines[dIndex][rIndex] = [];
}

You could take it one step further and remove the braces like this...

if (typeof dIndex === 'undefined') dPolylines = [];
else if (typeof rIndex === 'undefined') dPolylines[dIndex] = [];
else dPolylines[dIndex][rIndex] = [];

I personally don't like that as it can lead to mistakes much easier than when braces are used, but each to their own. It's still readable and is now only 3 lines long.

For the best of both worlds, you could simply reformat the first suggestion I made and make me cringe if I ever see any of your code :p

if (typeof dIndex === 'undefined') { dPolylines = []; }
else if (typeof rIndex === 'undefined') { dPolylines[dIndex] = []; }
else { dPolylines[dIndex][rIndex] = []; }

Upvotes: 1

Audite Marlow
Audite Marlow

Reputation: 1054

So there are a couple of solutions possible. The first one is to use the ternary operator, but it isn't all that clean:

typeof dIndex === 'undefined' ? dPolylines = [] : (typeof rIndex === 'undefined' ? dPolylines[dIndex] = [] : dPolylines[dIndex][rIndex] = []);

As you can see, it's just far too long. Considering Archer's answer, you'll have the following:

if (typeof dIndex === 'undefined') {
    dPolylines = [];
} else if (typeof rIndex === 'undefined') {
    dPolylines[dIndex] = [];
} else {
    dPolylines[dIndex][rIndex] = [];
}

This comes down to 6 lines, which is still pretty long. But since I'm going to need this piece of code multiple times throughout my original code, I converted it into a function:

dPolylines = clearDirectionsArray(dPolylines, dIndex, rIndex);

function clearDirectionsArray(array, dIndex, rIndex) {
    if (typeof dIndex === 'undefined') {
        array = [];
    } else if (typeof rIndex === 'undefined') {
        array[dIndex] = [];
    } else {
        array[dIndex][rIndex] = [];
    }

    return array;
};

This would eventually lower the amount of lines drastically throughout the code. Although this was not originally put up in the question.

Upvotes: 0

vp_arth
vp_arth

Reputation: 14992

function straightFill(obj, keys) {
   obj = obj || {};
   var cur = obj;
   var key = keys.shift();
   while (typeof key != 'undefined') {
     cur[key] = {};
     cur = cur[key];
     key = keys.shift();
   }
   console.log(JSON.stringify(obj));
   return obj;
}

var a = {};
straightFill(a, ['key1', 'key2']);              // {"key1":{"key2":{}}}
straightFill(null, [undefined, 'key2']);        // {}
straightFill(null, ['key1', undefined, 'key2']);// {"key1":{}}

Why I use objects instead arrays?
You should not use arrays with insequenced or nonnumeric keys:

>> a = []
<< []
>> a['key'] = 5
<< 5
>> a
<< []
>> a.key
<< 5
>> a[2] = 4
<< 4
>> a
<< [undefined × 2, 4]
>> a.length
<< 3
>> Object.keys(a)
<< ["2", "key"]

Ternary solution:

typeof dIndex == 'undefined' ? dPolylines = [] :
    typeof rIndex == 'undefined' ? dPolylines[dIndex] = [] :
        dPolylines[dIndex][rIndex] = [];

Upvotes: 0

das-g
das-g

Reputation: 9994

Archer's comment applied to your code:

if (typeof dIndex === 'undefined') {
    dPolylines = [];
} else if (typeof rIndex === 'undefined') {
    dPolylines[dIndex] = [];
} else {
    dPolylines[dIndex][rIndex] = [];
}

Not much shorter, but still comprehensible (maybe even more so than the original code).

Upvotes: 1

Abhijit Muke
Abhijit Muke

Reputation: 1214

typeof dIndex !== 'undefined' ? dPolylines = [] : ElseCheck()


function ElseCheck(){
    typeof rIndex !== 'undefined' ? dPolylines[dIndex][rIndex] = [] : dPolylines[dIndex] = []
}

Just trying ternary condition

Upvotes: 0

Related Questions