Which practice is better and why?
if statement = 1 then
print(1)
elseif statement = 2 then
print(2)
end
or
if statement = 1 then
print(1)
end
if statement = 2 then
print(2)
end
Which practice is better and why?
if statement = 1 then
print(1)
elseif statement = 2 then
print(2)
end
or
if statement = 1 then
print(1)
end
if statement = 2 then
print(2)
end
This question is a bit vague and seems to conflate things, while also being a bit too open ended.
Firstly, the two examples you provided are not equivalent.
Consider the following
if x > 10 then
print(">10")
elseif x > 5 then
print(">5")
end
-- versus
if x > 10 then
print(">10")
end
if x > 5 then
print(">5")
end
In this example, if we use 15 as an input, the first one prints ">10", while the second one prints both ">5" and ">10".
While this may be what you want, they're not the same thing.
The actual comparison you want is this:
-- Standard if-elseif-else
if conditionA then
print("a")
elseif conditionB then
print("b")
elseif conditionC then
print("c")
end
--versus
-- Nested if statements
if conditionA then
print("a")
else
if conditionB then
print("b")
else
if conditionC then
print("c")
end
end
end
I added an extra condition branch to highlight the flaw in nested if statements over else-ifs -- nested if statements get very unweildy very quickly.
They also sort of run contrary to the principles of indentation by over-indenting conditionB and conditionC, but that's a bit abstract so we won't dwell on that too much.
I think it's pretty obvious that the first one is more readable since it isn't ridiculously verbose.
The caveat you've probably spotted in section 1 is that sometimes, they are the same. That's when conditionA and conditionB and mututally exclusive.
Mututally exclusive basically means that if conditionA holds, then conditionB cannot possibly under any circumstances ever hold.
Now for trivial examples, mutual exclusivity is pretty easy to spot. The problem comes when you have either more complicated conditions, or when you have one set of conditions, and then 3 months down the line you need to add an extra one. When you make the change, you forget about the previous requirement of mutual exclusivity and introduce a bug into your code.
And that's basically what this comparison comes down to -- basically bugs. A huge number of bugs in software development come down to assumptions being made that are then broken, such as the assumption of mutual exclusivity we've just made.
A huge amount of recent developments in languages and tooling is just to stop developers making stupid mistakes. Entire languages exist (e.g., Ada) which are deliberately difficult to write, just so they're easier to read, and harder to write incorrectly.
If you have a situation where you want the possibility of conditionA and conditionB both holding, and both corresponding blocks of code executing, use an if-if-if type thing.
If not, use if-elseif, because it's there for a reason.
I'm aware this answer turned into a bit of a rant at the end. Whoops.
Nope perfectly explained, thank you for the clarification!
This discussion is preserved as it was. New answers, comments, and votes are closed.
7 comments
Generally speaking else-ifs, because it means that if your conditions aren't actually mutually exclusive, you don't end up accidentally running code you don't want to run. As a rule, if a contsruct exists in a language that does a specific thing, there's probably a reason for it existing, and you should probably use it when you can.
To be honest, it depends on the situation
^^ Should have posted that as an answer @fredfishy
So I understand, code can accidentally run by accident because two statements aren't together unless I use elseif, thank you very much!
in simple terms, yes.
Also, using elseif takes up less lines.
Bad argument