Why to use elseif

whoward69

DLL Minion
Joined
May 30, 2011
Messages
8,699
Location
Near Portsmouth, UK
Consider the following code that tests a unit's domain type and executes some associated code
Code:
if (iDomain == DomainTypes.DOMAIN_AIR) then
  -- Do something for air units
end
if (iDomain == DomainTypes.DOMAIN_LAND) then
  -- Do something for land units
end
if (iDomain == DomainTypes.DOMAIN_HOVER) then
  -- Do something for hovering units
end
if (iDomain == DomainTypes.DOMAIN_SEA) then
  -- Do something for sea units
end
Each unit will only have one domain type so the necessary code, and only that code, will get executed as required.
BUT each of the four conditions will be evaluated for every unit. If we have 30 units, that's 120 conditions to evaluate

However by simply using the elseif part of the if-then-elseif-else-end statement we can reduce this considerably
Code:
if (iDomain == DomainTypes.DOMAIN_AIR) then
  -- Do something for air units
elseif (iDomain == DomainTypes.DOMAIN_LAND) then
  -- Do something for land units
elseif (iDomain == DomainTypes.DOMAIN_HOVER) then
  -- Do something for hovering units
elseif (iDomain == DomainTypes.DOMAIN_SEA) then
  -- Do something for sea units
end
If the unit flies, it will pass the first condition, execute the code for air units and then skip to the closing end. If it sails, it will test for air, land and hovering before finally passing the last condition, execute the code for sea units and skip to (which it's already at) the closing end.
So the best case is we test only one condition, the worst case is we test all four, so the average is two and a half, so for our 30 units we test 75 conditions - a 37% reduction

This also brings up another consideration. ALWAYS place the most likely to occur condition(s) first.

Assuming we have 60% land, 30% sea, 8% naval and 2% hovering units (a not unusual kind of balance) and an army of exactly 100 units, if we arrange our code as
Code:
if (iDomain == DomainTypes.DOMAIN_HOVER) then
  -- Do something for hovering units
elseif (iDomain == DomainTypes.DOMAIN_AIR) then
  -- Do something for air units
elseif (iDomain == DomainTypes.DOMAIN_SEA) then
  -- Do something for sea units
elseif (iDomain == DomainTypes.DOMAIN_LAND) then
  -- Do something for land units
end
we will test 2 * 1 + 8 * 2 + 30 * 3 + 60 * 4 = 228 conditions
However, by putting the most likely conditions first
Code:
if (iDomain == DomainTypes.DOMAIN_LAND) then
  -- Do something for land units
elseif (iDomain == DomainTypes.DOMAIN_SEA) then
  -- Do something for sea units
elseif (iDomain == DomainTypes.DOMAIN_AIR) then
  -- Do something for air units
elseif (iDomain == DomainTypes.DOMAIN_HOVER) then
  -- Do something for hovering units
end
we will test 60 * 1 + 30 * 2 + 8 * 3 + 2 * 1 = 146 conditions - a 36% reduction

Rule of thumb 1: If your if - then - end statements are mutually exclusive, ALWAYS use elseif.
Rule of thumb 2: When using elseif ALWAYS place the most likely to occur conditions first
 
Top Bottom