103

My programming teacher wrote an "else if" like this... Should I quit???

Comments
  • 21
    If 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.
  • 29
    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.
  • 9
    @AlmondSauce That's what I'm thinking.
  • 5
    Well, it never hurts to double-check... /s
  • 3
    @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.
  • 3
    These bla bla bla remember me Dracula from hotel Transilvania
  • 1
    Just a sugar free else if so what
  • 1
    escalating to quitting the course is a bit exaggerating.
  • 4
    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) {...}
    ....
    }
  • 3
    Why not just ask the teacher why they wrote it this way? Why automatically question their competence as a teacher and quit?
  • 0
    switch is way better than this
  • 0
    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
  • 1
    @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.
  • 0
    @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.
  • 0
    @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.
  • 0
    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.
  • 0
    eslint: no-lonely-if, brace-style, *autistic-screeching*
  • 1
    I 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.
  • 0
    @irene for everything. Every type of loop. Even for loops.
Add Comment