how many 'if' statements in a function? [ANSWERED]

Started by EnterTheStory (aka tolworthy), Mon 17/03/2008 14:53:45

Previous topic - Next topic

EnterTheStory (aka tolworthy)

Is there a limit to the number of "if" statements I can use in a function?

My previous game engine stored such things as bytes, so would allow up to 255 and no more.

Example: one of my functions has 33 top level "if" statements. Each of these leads to another 10-20 'if's, and each of those leads to more "if"s containing the actual code.  Should I split the functions up if it they grow over a certain size?

Khris

Are you sure you actually need that many ifs?

I'm not sure about your programming skills, but such a high number of ifs usually suggests that the code isn't aware of a much simpler way of doing things... ;)

An if-related restriction I'm aware of is the number of else-ifs in a single block. It can't be higher than 8 or something like that.

Pumaman

The only "if"-related restriction is that you can only have up to 75 nested if's (that is, an if inside another if -- but this also applies to "else" statements which means if you had 75 "else if"s in a row that would breach the limit).

But if the "if"s are not related to each other then there's no limit to the number you can have in one function. I've seen some repeatedly_execute functions with hundreds of "if" checks.

EnterTheStory (aka tolworthy)

Quote from: KhrisMUC on Mon 17/03/2008 16:07:45
Are you sure you actually need that many ifs? I'm not sure about your programming skills, but such a high number of ifs usually suggests that the code isn't aware of a much simpler way of doing things... ;)

That would be me. :) However, in my defense, I've tried a lot of different methods, and this one seems the most appropriate for the task.

Quote from: Pumaman on Mon 17/03/2008 16:10:59
The only "if"-related restriction is that you can only have up to 75 nested if's (that is, an if inside another if -- but this also applies to "else" statements which means if you had 75 "else if"s in a row that would breach the limit). But if the "if"s are not related to each other then there's no limit to the number you can have in one function.

Thanks! Then a "tree" with 33 branches, each leading to 20 branches, each of THOSE has 20 branches, that would be perfectly safe.  Just 3 levels, not 75.

Quote from: Pumaman on Mon 17/03/2008 16:10:59
I've seen some repeatedly_execute functions with hundreds of "if" checks.

How did you get to see my code? :)

Pumaman

Quote from: tolworthy on Mon 17/03/2008 17:35:16
Thanks! Then a "tree" with 33 branches, each leading to 20 branches, each of THOSE has 20 branches, that would be perfectly safe.  Just 3 levels, not 75.

It should be, yes. The only gotcha is the "else if" thing I mentioned, because this code:

if (something)
{
}
else if (something_different)
{
}
else if (another_thing)
{
}

is actually compiled like this:

Code: ags

if (something)
{
}
else 
{
  if (something_different) 
  {
  }
  else 
  {
    if (another_thing)
    {
    }
  }
}


which actually creates 3 levels of nesting.

But in the unlikely event of you running into this limit, you can easily workaround it by setting some sort of "continue = 1" variable in the 75th "else" clause, and then starting a new if/elseif chain using that variable.

EnterTheStory (aka tolworthy)

Quote from: Pumaman on Mon 17/03/2008 23:49:28The only gotcha is the "else if" thing I mentioned, because this code:

if (something)
{
}
else if (something_different)
{
}
else if (another_thing)
{
}

is actually compiled like this:

Code: ags

if (something)
{
}
else 
{
  if (something_different) 
  {
  }
  else 
  {
    if (another_thing)
    {
    }
  }
}

Funny you mention that, as a beginner I always add those brackets. I don't trust myself to use strings of naked 'else's. I leave that to the experts.

SMF spam blocked by CleanTalk