Question

I was trying to use a if...else statement with arrays without having to declare the arrays. I got it to work this way:

if(['banana','apple','lemon'].indexOf(somevar) >-1)
{
 //code
}
else if(['chicken','dog','elephant'].indexOf(somevar) >-1)
{
 //code
}
.
.
.

And it keep going this way until some dozens of if...elses. The code is working fine, no problem noticed. Is it a bad pratice? Is it really an array? Can it cause some performance loss, memory leaks, or reference problems? Does the "not declared" array used in this code, if it is really an array, have a proper name in programming?

Was it helpful?

Solution

It seems pointless in my opinion, since you know exactly what element you want.

As for memory, the array should be deallocated as soon as you move to the next statement, and I would personally consider this bad practice in this instance, since, like I said earlier, it doesn't do anything since you know which will be selected.

If it's a static list that the user is going to select an element from, this is alright but I would probably define the array elsewhere and just reference that when needed, so you don't have to create the exact same array over and over again.

I consider it bad practice since if I wanted to add/remove/change an element in an array, I would rather just change the array when it's declared at the top, or change the data source. By sprinkling it through your code, you allow the possibility of hard to maintain code.

OTHER TIPS

How about

switch(somevar) {
    case 'banana': case 'apple': case 'lemon'
        //...
        break;
    case 'chicken': case 'dog': case 'elephant'
        //...
        break;
}

You're just declaring/using arrays on-the-fly. Nothing really wrong with it, it's just one of many coding styles. e.g.

if (somevar == 'banana') || (somevar == 'apple') || etc...) {
  ...code...
} else if (somevar == 'chicken') || (somevar == 'dog') || etc...) {
  ... code
}

or perhaps

switch(somevar) {
    case 'banana':// fall-through
    case 'apple': // fall-through
    case ...
         .... code ...
         break;
    case 'chicken':
    case 'dog': 
       etc...
}

They're all valid. It comes down to what your project's code style guidelines are and how many of these comparisons you need to do.

I see no problem, but I would declare these arrays for readability and maintenance:

var fruits = ['banana','apple','lemon'], 
    animals = ['chicken','dog','elephant'];
if(fruits.indexOf(somevar) > -1)
{
 //code
}
else if(animals.indexOf(somevar) > -1)
{
 //code
}

Now it's clearer why you check if someVar is in one array or the other, and it's easier to update the arrays - I want add another animal, I go to the animals array, not "the first else if block".

Yes, ['banana','apple','lemon'] is an array, but your code will fail when somevar === 'chicken' because ['banana','apple','lemon'].indexOf(somevar) === 0, which is a falsey value.

Also, your else if statement is redundant. You should check the indices by doing:

if(['banana','apple','lemon'].indexOf(somevar) >= 0 ) { ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top