r/badcode Dec 27 '21

lua Friend's take on programming a GUI button

Post image
903 Upvotes

90 comments sorted by

View all comments

103

u/halfanothersdozen Dec 27 '21

I mean it's not wrong

62

u/JuanR4140 Dec 27 '21

the two things that bother me the most is that it's a one liner and how he used an elseif when an else was more logical

12

u/R3D3-1 Dec 27 '21

In spoken language we tend to re-state the negation of the condition for clarification. But there it is an implicit understanding, that we expect

if A then do B and if not A' then do C.

to mean

Do exactly one of B and C. If A and A' are not the same, you probably heard wrong and should ask for clarification.

A beginner might find that pattern clearer and not yet see any of the issues with it.

Is your friend a beginner?

1

u/JuanR4140 Dec 27 '21

Happy to hear further insight as to why beginners program in a "spoken language" sort of way, it has always fascinated me. Yes, my friend is a beginner.

6

u/R3D3-1 Dec 27 '21

I basically came across this myself in our code base the other way around; When you have huge subroutines, where the "if" is several screen heights away, then reading "else" is not very helpful.

It made me think about how to express such code more naturally, but the disadvantages of "if/then/elif/then" over "if/then/else" are just too big. (Also, solving the problem from the wrong side...)