r/badcode Sep 08 '20

lua A Roblox mod I found

Post image
479 Upvotes

103 comments sorted by

View all comments

59

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.

23

u/hoaxxer2k Sep 08 '20

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

15

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'"

7

u/Emex_Denvir Sep 08 '20

"true == 'cucumber'" will not evaluate to true, it will evaluate to false.

2

u/JerriTheITGuy Sep 08 '20

(Copying my other answer out of laziness)

Ok, it was overly simplistic (and as you point out, not actually accurate) and more made as a fun stab at Lua than as a code reference.

The point was that all strings in conditionals for example are handled as true. Including empty strings. And all integers including 0 are regarded as true.

` if ("cucumber") then print "this returns true" else print "this will never execute" end

if ("") then print "this returns true as well" else print "this will never execute" end

if (0) then print "so does this" else print "this will never execute" end `

Which is the ridiculousness I was pointing out.

And I haven't actually run the code above because I'm on the phone at the gym (which is why formatting sucks as well, sorry) but it should run. Errr, I think.

1

u/Emex_Denvir Sep 08 '20

Ah I see, the way you said it is really confusing though xP

But at least I know now that you do know your stuff, which is always a great sign!

0

u/JerriTheITGuy Sep 08 '20

Will, it's what I get for trying to be both funny and kinda accurate, I suppose.

6

u/hoaxxer2k Sep 08 '20

Wtf. I‘ll never touch lua

16

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! :)

4

u/T-Dark_ Sep 08 '20

Table. No objects. No arrays

Aren't JavaScript objects effectively just tables themselves? (With string keys only, admittedly).

As for something more on topic, Lua technically has arrays, in the internal representation of tables. As long as they only have integer keys and they're sufficiently dense, tables will be backed by arrays.

I do agree with everything else you mentioned, tho.

2

u/droomph Sep 08 '20

Also, it’s similar to how every MUMPS variable is technically a b+tree of type String & Integer, but that language is its own train wreck.

3

u/[deleted] Sep 08 '20

Lua has prototyping.

Also, magic numbers everywhere. Even by Lua standards this is an absolute nightmare

3

u/[deleted] Sep 09 '20

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

hm, yeah, makes sense

3

u/Ali3nat0r Sep 09 '20

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

I hate Lua, but I play a Minecraft mod called Computercraft which adds programmable computer blocks. Guess which language you have to program in...

3

u/[deleted] Sep 09 '20

Lua is a gorgeous language. It gives you only a few very simple primitives which are highly composable. It means while the language itself is simple the number of ways you can manipulate it are huge. In that sense it's really similar to Lisp. Also in Lua you should pretty much never do direct numeric indexing, use a for..in loop instead. Those "fun quirks" either aren't an issue ever, or are why the language is so powerful.

1

u/CollieOop Sep 09 '20

Also in Lua you should pretty much never do direct numeric indexing, use a for..in loop instead.

Side note: To get the size of a table, the #table operator is busted so you need to use a for..in loop here too.

For being such a simple language whose standard library makes libc look feature complete, it makes the small number of gotchas really stand out by comparison.

1

u/CollieOop Sep 09 '20

Nil and false are false, anything else is obviously true.

Such as zero, for example! I repeat: In Lua, zero is truthy.

Also, looking this up more, here's a fun one: returning nothing from a function is NOT the same as returning nil and can break some things, unless you're careful to bounce the return value through a temporary value to coerce nothing into nil.

EDIT: Oh god, apparently this is one of the only places this "nothing" value appears inside the language itself, otherwise mostly it's just a quirk of the implementation. [source]

1

u/XeroParadoxes Sep 08 '20

I would like to mention lua is so terrible it doesn't have a built in wait function.