14

I am curious as to if I am using too many if/else if statements. I am writing a tic-tac-toe program using javascript, and to determine if the computer should block the player I am using about 9 if statements and I use about 9 when determining if there is 3 in a row.

For example:

if(r1c1V === xOrO && r1c2V === xOrO && r1c3V === xOrO)
{
    is3InARow = true;
}
else if(r2c1V === xOrO && r2c2V === xOrO && r2c3V === xOrO)
{
    is3InARow = true;
}
else if(r3c1V === xOrO && r3c2V === xOrO && r3c3V === xOrO)
{
    is3InARow = true;
}
.
.
.
.

and so on.

So my question is, am I using too many if statements? or is there no better way to do this? My friend was telling me that I shouldn't really use that many if statements, but I am not sure if he is true or not, I can understand why in some cases that it would be slower or bad, but I am not sure.

Thanks in advance!!

  • It's not bad programming, it depend on the logic. You could try to store these combinations in an array and then loop over, but in all cases you have to do these checks. – Hacketo Nov 26 '15 at 09:29

8 Answers8

16

This is rather subjective, so is not really ideal for Stack Overflow.

What your friend is probably suggesting is that programs which use long if/else statements, nested ifs etc. are sometimes difficult to maintain, and are not always very readable. You can sometimes replace long if statements with a separate function, which can be much easier to maintain.

For instance in tic-tac-toe:

function checkRow(rowToCheck){
    if (rowToCheck[0] === rowToCheck[1] && rowToCheck[1] === rowToCheck[2]){
        return true;
    } else {
        return false;
    }
}

Not to say that is perfect or even good code, but you can see how it would allow you to reduce the number of if statements in your code. It could be factored further:

function checkRow(rowToCheck){
    var allMatch = true;
    for (var i=0;i<rowToCheck.length && allMatch;i++){
        allMatch = rowToCheck[i] === rowToCheck[0];
    }
    return allMatch;
}

That allows for rows of varying length, and cuts down of the if statements. Anyway I hope that explains some of the ways you can remove if statements.


Edit

Further in the future, I'd suggest that there is yet another, better way to check the equality of the elements in a row:

const o = 'o'
const x = 'x'

const rows = [
  [o, x, x],
  [x, x, x],
  [o, o, x]
]

const rowIsEqual = row => !row
  .some(square => square !== row[0])
  
const results = rows.map(rowIsEqual)
  
console.dir(results)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
4

Programming is all about automating processes. You will not hear me say that the way you are doing this is wrong. If it works then it is oke. But of course it would be more beautiful if you could find a way to make it check everything automatically. Like looping through all x cords and just check if all of those are checked. This may not be easier, but it will be more extendable. If you'd ever wish to play a game on a ten by ten grid. then you would only have to say that that is the size of the field. Right now you would need to add all those other lines of code.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
StijnvanGaal
  • 441
  • 3
  • 17
2

There's no hard-and-fast rule. Do bear in mind that && and || are short-circutted meaning that evaluation stops once the result is known. That and the fact that you are able to order the if statements in any way you please means that you can optimise things by considering frequent comparisons first.

Always do what's clearest, and if performance is crucial, what's fastest.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

In general, yes, "too many" ifs are bad. Any problem can be solved just using ifs, but everything you see around you, functional programming, recursion, object modelling, is there to stop the if's getting out of control and leaving you with sprawling incomprehensible code. You should be more worried about the depth of nested ifs than the length of a sequence. Here's tic-tac-toe on rosetta code, if you want to see real cleverness.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
0

So many apps are built with long if then else statements? Why? Wouldn't a dictionary be better? Look up the keyword and perform the associated action. I'm surprised I can't find a simple python library that does this for multiple back-ends including databases.

Micheal Bee
  • 550
  • 5
  • 11
-1

I guess using too many if- else statements decrease the readability of the program.You can use switch-case statement instead of using if-else statements.Just google switch-case in javascript and you will get many useful links to get an idea about it.

-1

In your scenario, its not bad at all. You can add return on your code if it can so that it won't execute other condition... JUST if its possible on your scenario.

rjps12
  • 587
  • 6
  • 15
-4

In these cases we use 'Recursion'.

To answer your question; if you use to many if statements you are doing something wrong. You can use other methods to get the to goal you want.

Example: you got a grid of 2,2 (0,1,2 and 0,1,2). So what you should do is create a algorithm that calculates the score. A very popular one is MiniMax.

E. van der Spoel
  • 260
  • 1
  • 15
  • I don't understand how this answers the question in any way. An explanation would go a long way towards avoiding further downvotes. – spender Nov 26 '15 at 09:25