Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
C0D4669016yIf you, yourself do this then yes. Since you’re already aware of this being bad design and can lead to highly inefficient performance (Spoilers if you don’t) then no, stay and teach the teacher something new.
-
It may be deliberate.
I've sometimes written "else if" statements this way when teaching others - especially if it's just one level deep like this. It often makes the flow much easier to explain and understand for beginners than just writing "else if" on one line as you normally would. -
@C0D4 How does it lead to lesser performance? "else if" is just syntactic sugar for an "if" inside an "else" anyway. Frodo gives the detailed explanation.
-
If you have multiple if after the same else condition that are not mutually exclusive it's the correct way to go:
if (cond1) {...}
else
{
if (cond2) {...}
if (cond3) {...}
if (cond4) {...}
....
} -
Why not just ask the teacher why they wrote it this way? Why automatically question their competence as a teacher and quit?
-
If you have something watching the memory register for the Boolean of the first if and then making bla bla bla exist because the Boolean was set to false it would work.
It would be *useless* but it would work -
C0D4669016y@Teabagging4Life , @irene
for comparison in PHP at least, using 100k iterations (code in pastebin) with the exact same logic.
nested if/else
https://pastebin.com/eibbLbGm
execution time is ~43.38 seconds
un-nested (if / elseif / else)
https://pastebin.com/Tt8udNEJ
~35.07 seconds
now take this further and the gap increases. -
@irene Yeah, I think PHP is the exception here, very few languages I know have "elseif" as a separate keyword (Perl is the only other one I know of.)
Not really needed in anything that's compiled, as it should be trivial for any half decent compiler to optimise this whether a separate keyword is used or not. -
@irene Haven't had much exposure to Python. Or Ruby now I think of it. That strikes me as another one that's probably got a separate keyword.
-
I'm going out on a limb here and say that guy main language is c# and that's why he formats his code like that.
-
TheMiper1876yI had a fight with my programming teacher three years ago because she forced people to write loops like this:
while True:
do_stuff
If(bla bla bla):
brake
I won.
My programming teacher wrote an "else if" like this... Should I quit???
rant