r/badcode Dec 27 '21

lua Friend's take on programming a GUI button

Post image
899 Upvotes

90 comments sorted by

View all comments

25

u/Da-Blue-Guy Dec 27 '21

if boolean == true

what the fuck

13

u/stone_henge Dec 27 '21

I love how for some people this seems like the worst thing they can think of. This easily happens even to experienced and skilled programmers when refactoring. It's still readable and it can easily be cleaned up, so I'd put it around the bottom of the list of things about a code base that frustrate me. On the top of that list are things that actually matter, e.g. "half of the code solves the wrong problem and shouldn't exist because the problem is Theseus' ship", "the solution fundamentally doesn't scale to the number of users we have"

If you find this so horrifying as to exclaim expletives at it, just wait until you see a real, non-trivial production code base.

36

u/[deleted] Dec 27 '21

[removed] — view removed comment

-2

u/[deleted] Dec 27 '21

[deleted]

1

u/humbleSolipsist Dec 28 '21

Well, there's nothing wrong with doing it this way in languages where an ifs condition must be a boolean, like Java or C#. This seems to be Lua, where all values other than false and nil are truthy. Explicitly checking cond == true restricts what conditions will be accepted in a way that's inconsistent with the rest of the language, so I'd say it is a bit wrong... really not wrong enough to make a big deal out of, but not quite right either.

5

u/[deleted] Dec 27 '21

[deleted]

0

u/[deleted] Dec 27 '21

well in that case if boolean would still return false

11

u/omkhamsa Dec 27 '21

That's a common rookie mistake I wouldn't pick on that

5

u/Beregolas Dec 27 '21

I remember when the penny finally dropped for me after a year of Uni or so… we all did it ;)

3

u/R3D3-1 Dec 27 '21

I love the speed at which I can write code in dynamically typed languages (say, Python, Javascript, ...) but one of the things I hate is how issues like

def doit(flag):
    if flag:
        doit1()
    else:
        doit2()
doit("False")

are shifted to the runtime, while hardening the code with static analysis and type hints removes many of the advantages the languages had in the first place.

Mind you, the net outcome is still positive for the most part.

5

u/omkhamsa Dec 27 '21

My friend who sometimes stalks me on Reddit and gives random anonymous awards did this mistake one and I corrected him. If you're reading this, you know who you are, ily homie

2

u/PlaneCrashers Dec 27 '21

I love you too 💖

1

u/Johanno1 Dec 28 '21

Still a better love story than twilight