0

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?

Samuel
  • 13
  • 3
  • 1
    Is this your actual code? Because "if(['banana','apple','lemon'].indexOf(somevar))" is the same as "if(somevar !== 'banana')" – Prinzhorn Sep 20 '13 at 14:52
  • I see no problem (other than maybe maintenance) if the code is not in a function that gets called multiple times. – pawel Sep 20 '13 at 14:54
  • @Prinzhorn, haha! You're right, it made no sense. I really posted the wrong code. It was missing the `>-1` part. Thanks. – Samuel Sep 20 '13 at 17:37

5 Answers5

0

How about

switch(somevar) {
    case 'banana': case 'apple': case 'lemon'
        //...
        break;
    case 'chicken': case 'dog': case 'elephant'
        //...
        break;
}
Prinzhorn
  • 22,120
  • 7
  • 61
  • 65
  • I know I could use "switch", and do it other ways, but I was always afraid of using "switch"... don't even remeber why. I think I read that it has some limitations compared to "if...else". Anywaw, thanks for answering. – Samuel Sep 20 '13 at 15:21
0

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.

Dakotah Hicock
  • 380
  • 2
  • 15
  • I forgot to tell this piece of code is inside a for...in statement. Then, the "somevar", in fact, changes in every iteration. The arrays are needed just once, they are not referenced in any other occasion in the code. Thanks for the answer. – Samuel Sep 20 '13 at 15:30
  • Sorry, I didn't understand what you meant, you're right, it's pointless. The `>-1` was missing. Edited. – Samuel Sep 20 '13 at 17:24
0

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Fine. I know I could use "switch", and do it other ways, but you got it, one reason is the code style guideline. Another reason is that I was always afraid of using "switch"... don't even remeber why. I think I read that it has some limitations compared to "if...else". Thanks for the answer. – Samuel Sep 20 '13 at 15:18
0

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 ) { ... }
Bucket
  • 7,415
  • 9
  • 35
  • 45
0

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".

pawel
  • 35,827
  • 7
  • 56
  • 53