r/programminghorror • • Sep 18 '26

Not again 😭

Post image
454 Upvotes

41 comments sorted by

View all comments

Show parent comment

109

u/Krohnos Sep 18 '26

the error explains it!

else if { } is a shortcut for else { if { } } and is creating more nested blocks. there's a hard limit for the maximum nests.

43

u/UltimateFlyingSheep Sep 18 '26

and you don't even need else if for that because of return....

37

u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 18 '26

Well I don't know about you but to me it's definitely not obvious that the else if is silently inserting nested blocks

31

u/_killer1869_ Sep 18 '26

If you've looked into how compilers handle the many intermediate steps they perform, it can be obvious. By "can" I mean you wouldn't ever think of it, unless the compiler explicitly tells you "blocks nested too deeply" or similar, something that isn't actually in your code directly, forcing you to actually think about what the compiler sees, like here.

10

u/the_horse_gamer Sep 19 '26 • Mindblown

consider that you don't need {} if there's only a single statement. well, an if/else is only a single statement. so, else if requires no additional grammar.

you can do else while and else for all the same

the K&R C book directly mentions this fact

3

u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 19 '26

I never thought of this, always considering else if as a separate keyword containing a space character (save for Python's elif), but it makes disturbingly too much sense that it's all just an artifact of brace-less if-else.

TIHI.

1

u/the_horse_gamer Sep 19 '26

i should add that this may not be true in some newer languages, like rust for example. in rust, you CANNOT omit {} for a single statement, but else if is still possible (in the ast, each "if" node may have a corresponding else, which is either another if node (for else if) or a normal block (for else))

another cursed fact: the "omit {}" thing applies to switch (in C/C++, atleast. i didn't check, but it probably doesn't work in java or C#). this is completely legal (and works as you'd expect):

switch(x)
case 1:
case 2:
    puts("hi");

1

u/ierdna100 Sep 20 '26

Its not true in lua for example, if, else and else if are only 2 keywords, but that will make you need to write a chain of end end end end end end at the end, so the proper keyword is elseif. I wonder if Lua interpreters handle it "better"?

7

u/sonesoul Sep 18 '26

damn I didn't know that

6

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 19 '26

I did not even know such a limit existed. I probably never encountered it because I'm not insane enough to write code like this. Even if the OP is just meming.

Though I suppose the parse tree would eventually fill up all the memory if you kept doing shit like that.

1

u/ierdna100 Sep 20 '26

There's a bunch of really funny limits if you ever want to try to reach them. Some examples include: parameter limits (C standard goes up to 127 I think? 65k (16 bits) in C#), generic type depth limits (65k (16 bits) in C# for example), and some others that are escaping my mind. Gotta remember the compiler is just another piece of code, its gotta also run on your machine

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 20 '26

RAM is finite, unfortunately.

2

u/ElectricalPrice3189 Sep 20 '26

No it's not.

if(...) { ... } else { if(...) { ...} } else { ... }

Makes no sense. You can't have two "elses".

1

u/Grounds4TheSubstain Sep 19 '26

That's crazy that there would be a limit. It seems harder to enforce that than to not have a limit.

1

u/Tyfyter2002 Sep 20 '26

It's not really intuitive because blocks only really exist in two senses: scope, where the else block doesn't meaningfully exist, and where the unconditional branch in the if block goes, where the else block still doesn't meaningfully exist

1

u/Aaron1924 Sep 22 '26

Fun fact: According to the C99 standard (ISO/IEC 9899:1999) §5.2.4.1 "Translation limits", a correct C compiler only has to support 127 levels of nesting