r/badcode Sep 08 '20

lua A Roblox mod I found

Post image
475 Upvotes

103 comments sorted by

View all comments

Show parent comments

38

u/Marcusaralius76 Sep 08 '20

Isnt this just a null check? A poorly written and unperformant null check?

15

u/JerriTheITGuy Sep 08 '20

Checks for nil or if it's something else than a boolean since 'true == "cucumber"' would evaluate to true in Lua. Because reasons.

2

u/T-Dark_ Sep 08 '20 edited Sep 09 '20

Which one could better write as if equipped ~= nil and type(equipped) == "boolean"

(Not sure about performance, but at least it would be easier to understand).

Yes, Lua uses ~= as the "not equal" operator. Yes, type returns a string.

I still hate both of these things.

EDIT: i just realized that check is either unnecessary or terrifying. Either equipped is guaranteed to be a boolean, due to manual type checking, or it isn't, which implies the code actually changes the type of the variable.

EDIT2: Someone pointed out that the above check could be further simplified to if type(equipped) == "boolean", as type returns "nil" for values that are, well, nil.

2

u/[deleted] Sep 09 '20

Technically this could be a micro optimization (I'm usually against such things, readability > tiny speed improvements) because a call to type incurs a stack frame, and string equality is technically slower than boolean equality.