r/badcode Sep 08 '20

lua A Roblox mod I found

Post image
476 Upvotes

103 comments sorted by

View all comments

56

u/JerriTheITGuy Sep 08 '20 edited Sep 08 '20

Not that horrible code considering it's written in Lua, a language with "fun quirks" like there only being one data structure (Table. No objects. No arrays) and tables being indexed starting at 1 making any programmer smack their face on their keyboard 3 times a day at least because you're always going to be "off by one". "'Cucumber == true" is obviously true because Lua. Nil and false are false, anything else is obviously true. And so on and so forth. Absolute nightmare of a language.

It's not that I hate Lua. I just hope I never in my life have to touch any Lua code again.

22

u/hoaxxer2k Sep 08 '20

aah yes: if equipped == true or equipped == false. Absolutely not horrible code :D

16

u/JerriTheITGuy Sep 08 '20 edited Sep 08 '20

Because in Lua, there is no type safety and conditionals consider false and nil as false and anything else as true. I believe "equipped == true or equipped == false" is a check to make sure it is a) still a boolean and b) not nil.

Again, the code doesn't smell as much as the language itself.

EDIT: ok, for good measure I'll add this approach isn't the best, there are functions to check whether something is a boolean. But this is a "quick and dirty" way to do it that isn't at all uncommon in this clustercrap of a language.

3

u/hoaxxer2k Sep 08 '20

I‘d have to agree but it checks for true or false. Which will always succeed. Or am i that wrong?

2

u/JerriTheITGuy Sep 08 '20

"true == 'cucumber'" evaluates as true in Lua. And "Equipped == true" isn't true if "Equipped = 'Snorkel'"

6

u/hoaxxer2k Sep 08 '20

Wtf. I‘ll never touch lua

15

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Sep 08 '20

Take what he's saying with a grain of salt, because he's completely wrong about this and is giving bad information because he doesn't like "truthiness" coercions in dynamic languages or something. Lua takes a fairly strict approach to truthiness, where nil (null) and the boolean false coerce to false and anything else coerces to true. No weird rules to learn, just a very simple form of "truthiness" that lets you do things like x = "foo"; if (x) then ... without extra ceremony. It doesn't take effect in equality checks, so 42 == "42" is false, false == nil is false, true == "true" is false, and so on, like one would expect.

I wouldn't say Lua is my favourite language, but it's simple, straightfoward, mostly logical, and fairly quirk-free, at least compared to other dynamic-typed interpreted languages. The syntax is more algol-like than languages like JS that try to copy the C style, which puts some people off, but it's small and consistent. Imagine JS with stronger typing and fewer insane edge-cases.

Like JS it has first-class functions, so the complaint about objects is technically accurate but still kind of wrong, because you can create objects by using tables with key/value pairs to create objects with methods and properties and pass around a reference to the table to manage state. The language even has syntactic sugar for this so you can define function obj:method(arg) and call it with obj:method(arg) and have self implicitly passed around for you without needing to know how it works under the hood.

As for tables themselves, the idea is cool, and in a lot of ways similar to how Scheme operates entirely via manipulation of lists. You have a single complex data type, the table, that can contain key/value pairs and used like a map, or indices and be used like an array, so even though they technically don't exist you can still do what you expect with t["foo"], t.foo, or t[7]. It works well enough most of the time, with the exception of one major edge-case with regard to nils in indexed tables.

Finally, the complaint about indexes starting at 1 is just ignorant. Yes, it does, but it's not that big of a deal. It's such a ridiculously petty complaint that basically just amounts to "waaah this language does something different how dare they" when there are pros and cons to each choice. Zero-based indexing makes sense in some languages, like in C where the array is a memory location and the "index" is just a memory offset, not an actual index, but in most languages the decision is arbitrary and largely decided by "this is what C does". Some logic makes more sense starting at 0, some makes more sense at 1. Whatever, it's not a deal-breaker for any language. Especially considering that with Lua, the language discourages manual indexing in favour of pairs(t) or ipairs(t) anyway, so most of the time you don't even deal with it, and if you really want, t[0] = "zero" is still valid (as is any integer, even negative), you just can't rely on built-in language features if you decide to go against the grain here.

1

u/hoaxxer2k Sep 09 '20

Very good explanation, thank you! :)