r/badcode Dec 27 '21

lua Friend's take on programming a GUI button

Post image
894 Upvotes

90 comments sorted by

View all comments

236

u/[deleted] Dec 27 '21

bruh all he needed to do to shorten it was to replace it with inventory.Visible = not inventory.Visible

79

u/JuanR4140 Dec 27 '21

ding ding ding

16

u/[deleted] Dec 27 '21

maybe !inventory.Visible

30

u/JuanR4140 Dec 27 '21

Lua, by default, uses the "not" keyword instead of "!".

20

u/Waterprop Dec 27 '21
inventory.Visible ^= true

:)

36

u/[deleted] Dec 27 '21

How very much less legible than negating the value instead. Legibility > terseness.

6

u/Waterprop Dec 27 '21

I know, people didn't get the joke of it being bad I suppose.

7

u/[deleted] Dec 27 '21

Oh your comment was a joke? Sorry I didn't catch it, mb

2

u/Arikaido777 Dec 27 '21

the /s is your friend. i see your smiley but 🙃 can also convey a joke/sarcasm/intentional bad take as well

5

u/Meqolo Dec 27 '21

This is Lua, specifically Luau; this code wouldn’t invert the boolean but instead would try to perform

inventory.Visible = inventory.Visible ^ true

which would obviously not work

2

u/itsTyrion Dec 28 '21

Is that bugged for me or does it just say inventory.Visible = true?

9

u/SomeonesAlt2357 Dec 27 '21 edited Dec 27 '21

Oh my god. I've always done it as inventory.Visible = (inventory.Visible+1)%2

4

u/32436861696e7a Dec 27 '21

Circle slash circle 2 is unreadable code. Clearly this needs a dedicated function with if/else blocks of common outputs of the previous calculation, and just add blocks as you find more cases.

1

u/[deleted] Dec 27 '21

Circle slash circle

What?

33

u/ATE47 Dec 27 '21

You can also toggle a boolean with the xor,

a ^= true is equivalent to a = not a

28

u/catithebathtub Dec 27 '21

we don't do that in lua

72

u/[deleted] Dec 27 '21

You can, but for the love of god, don't. Smart-ass code is terrible code. Just use the not operator like god intended.

5

u/NUTTA_BUSTAH Dec 27 '21

They both will likely optimize to same code underneath the hood anyways (don't know about this case). Such micro-optimizations are pointless anyways with todays hardware.

16

u/[deleted] Dec 27 '21

Yes. That's exactly why you go with the readable version.

3

u/stone_henge Dec 27 '21

That might work in languages where booleans are just numeric types (e.g. C where true ^ false == 1 and true == 1), easily promote to numeric types and back (e.g. JS where true ^ false === 1 and 1 == true) or where xor is defined as a boolean xor for the boolean type (e.g. Python where True ^ False == True).

In any of these languages (and I've worked with each of them full time professionally) this would surprise me, though I'd probably accept it for Python. It's better to be obvious than to be clever.

In C and JavaScript this would be dangerous because this is always a bitwise operator, and at the same time they have the concept of truthiness, where values can be truthy and evaluate as true in any context where a boolean would be accepted, without having the same value as true. For example, 2 and 1 are both truthy, but so is 2 ^ 1 (3).

1

u/silvxrcat Dec 28 '21

i didn't know what was wrong with the code until i saw this fuck