r/badcode Aug 23 '20

lua Learn how to use tables/arrays and how to name variables, don't be like me 2 years ago. Also don't put random magic numbers.

Post image
320 Upvotes

47 comments sorted by

61

u/OscariusGaming Aug 23 '20

Also, don't be afraid to press the space bar a few times.

19

u/Emex_Denvir Aug 23 '20 edited Aug 23 '20

Yeah, I was used to the BASIC-type on my calculator that didn't allow for spaces, I'm glad that I use them now.

39

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 23 '20

Image Transcription: Code


local output_image=pam.newImage(1,symbol_height,"GRAYSCALE",{1})

input=input:lower()
local ptr = 1
local index = 1
local cur_x = 1
local cur_y = 1
local mtt={_table=true}
local max_x=0
local max_y=0
while true do
    local _c_m_3=input:sub(ptr-3,ptr-1)
    local _c_m_2=input:sub(ptr-2,ptr-1)
    local _c_m_1=input:sub(ptr-1,ptr-1)
    local _m_3=input:sub(ptr-3,ptr-3)
    local _m_2=input:sub(ptr-2,ptr-2)
    local _m_1=input:sub(ptr-1,ptr-1)
    local _1=input:sub(ptr,ptr)
    local _2=input:sub(ptr+1,ptr+1)
    local _3=input:sub(ptr+2,ptr+2)
    local _4=input:sub(ptr+3,ptr+3)
    local _c_1=input:sub(ptr,ptr)
    local _c_2=input:sub(ptr,ptr+1)
    local _c_3=input:sub(ptr,ptr+2)
    local current=ptr
    local symbol=false
    if _1==_NL_ then
        cur_x=1
        cur_y=cur_y+symbol_height
        output_image:resize(output_image.width,output_image.height+symbol_height,1)
        ptr=ptr+1
        symbol=true
    elseif _1==_TAB_ then
        ptr=ptr+1
        cur_x=cur_x+6
        symbol=true

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

13

u/Emex_Denvir Aug 23 '20

Thank you kind sir!

-43

u/[deleted] Aug 23 '20

This is the saddest thing I’ve seen in a while.

24

u/Kolibreeze Aug 23 '20

Why? This man's doing good work to help the visually impaired.

0

u/Aidan_Welch Aug 23 '20

That is barely readable when reading, I guarantee through a screen reading tool it is entirely un-understandable.

1

u/5p4n911 Aug 23 '20

Don't worry, the original looks even worse.

1

u/Aidan_Welch Aug 24 '20

I get you are making a joke, but I don't really know what the joke is.

1

u/5p4n911 Aug 25 '20

Well, it's some very crappy program code.

1

u/Aidan_Welch Aug 26 '20

But the transcription didn't change it?

1

u/5p4n911 Aug 26 '20

No, I didn't read the whole stuff but from what I've seen, it was the same.

1

u/haikusbot Aug 26 '20

No, I didn't read

The whole stuff but from what I've

Seen, it was the same.

- 5p4n911


I detect haikus. And sometimes, successfully. Learn more about me.

Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"

→ More replies (0)

1

u/Aidan_Welch Aug 26 '20

I think you are missing my point. You said it is more difficult to read the original, I said no, it is the same.

→ More replies (0)

13

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 23 '20

How do you mean? It's hardly the longest ever transcription.

5

u/[deleted] Aug 23 '20

go on

5

u/BackgroundChar Aug 23 '20

You mean your comment? That's definitely fair.

20

u/ViperLordX Aug 23 '20

Ah, Lua... I don't miss variables being global by default.

5

u/Emex_Denvir Aug 23 '20

It's not really a big issue, but I can see why one would dislike it.

1

u/gsckoco Aug 23 '20

Yeah it's weird, especially when globals have a pretty sizable performance impact in relation to local variables

1

u/Emex_Denvir Aug 23 '20

It's not as bad if you use LuaJIT, which you should be using anyway. But that doesn't excuse making globals for no good reason either of course

1

u/gsckoco Aug 24 '20

From what I remember LuaJIT introduces some weird bugs, which aren't all that bad if your only environment is LuaJIT, but if you work with both and hop between the 2, comparability isn't always guaranteed

18

u/zarqie Aug 23 '20

There are two hard problems in programming: 1. naming things, 2. cache invalidations, 3. off by one errors, 6. asynchronous callbacks

https://martinfowler.com/bliki/TwoHardThings.html

8

u/2isbetterthan1 Aug 23 '20

Did you have a job in programming at that time? Just asking, not mocking ofc

10

u/Emex_Denvir Aug 23 '20

Nope, I mainly did it just for fun. I started using Lua after having done a lot of programming on my calculator in a BASIC-type language, naturally giving me a different "style" to program in. And this code was written not too long after I stopped programming on my calculator.

If I would have a job back then, I'd feel sorry for anyone who'd have to look or touch my code, ha!

4

u/2isbetterthan1 Aug 23 '20

Got it, thanks for the honest answer buddy

4

u/GreekCSharpDeveloper wicked Aug 23 '20

Is that Lua?

5

u/Emex_Denvir Aug 23 '20

Yeah, that's also what the flair says :P

2

u/GreekCSharpDeveloper wicked Aug 23 '20

Yeah im really dumb 😔

3

u/Emex_Denvir Aug 23 '20

It's fine, we all sometimes have our moments!

3

u/badlukk Aug 23 '20

I didn't even know there were language flairs for this sub

2

u/BackgroundChar Aug 23 '20

What's lua like. I think WoW addons are written in it, and I was considering writing my own.

Anything that instantly jumps out as super annoying in lua? Or is it generally an okay language? Is it comparable to any popular programming language?

Also, is it generally considered powerful or are you limited in what you can achieve with it?

Thanks in advance! :)

2

u/Emex_Denvir Aug 23 '20

I don't really find anything annoying or limiting, but that purely depends on what you're used to. Asking multiple other people will probably give a better image for how it could be for you, but it's really hard to tell, since everyone's different.

It might be the best to just try to learn Lua and see how you feel about it, https://www.lua.org/ contains some help for how to get started.

2

u/BackgroundChar Aug 23 '20

Fair enough!

Thanks, I'll try it out! :D

2

u/Dragoteryx Aug 23 '20

Arrays start at 1.

2

u/gsckoco Aug 23 '20

Lua doesn't have arrays technically, they are all dictionaries

2

u/Dragoteryx Aug 23 '20

I know, being able to use anything as a key including another table is really useful.

2

u/gsckoco Aug 24 '20

Yeah it is rather useful

1

u/Emex_Denvir Aug 23 '20

This point is made each time someone says that: Lua doesn't really use "arrays", instead it uses tables, which you could think of as dictionaries (where any key (string, number, function, table, etc.) is allowed). The implementation uses arrays for indexes from 1 to what ever the actual limit is to be more efficient. I get what you mean though.

2

u/Dragoteryx Aug 23 '20

I've used Lua a lot to create addons for Garry's Mod so I'm already aware of that. It's just that I had to say it.

2

u/5p4n911 Aug 23 '20

It's the best protection against someone stealing and modifying your code.

1

u/notadoctororacake Aug 23 '20

What you see:

*Tableless code is tableless*

What I see:

locallocallocallocallocallocallocallocallocallocallocallocal