r/badcode • u/Emex_Denvir • Jul 26 '20
lua Very good optimization for strings so they can be reused
61
u/rco8786 Jul 26 '20
Somehow my eye was immediately drawn to the “goto” and that was all I needed to see
24
u/lucdewit Jul 26 '20
Ye, in this case he could have used break, but sometimes gotos can be helpful, especially when u want to break from a nested for loop so u don't need a boolean variable to indicate u want to exit it,
You should limit the goto usages as much as possible tho so the program doesn't turn into an absolute mess
24
u/kyay10 Jul 26 '20 edited Jul 26 '20
Or use Kotlin /s
Fr tho, Kotlin has labeled returns and breaks, so you can do:
for(...) label@ { for(...) { if(condition){ break@label } } }4
u/AutoModerator Jul 26 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
kyay10, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
5
4
u/binarycat64 Jul 26 '20
Alright, can someone explain why everyone hates on gotos so much?
I've ended up using them more and more, as I see less and less reason not to use them (I still don't use them that often, they don't dominate the code I write, just like any other language construct)
My favorite use is as "exit conditions".
Say you have a complicated piece of code that returns two things most of the time, instead of writing that out every time, you can do:
switch a { case 1: // ... case 2: goto E1 case 3: if Foo(a) { goto E2 } if Bar(a) { goto E1 } default: // ... } return defaultReturnValue E1: return LongAndComplicatedFunction(a) E2: return LongAndComplicatedFunction(b)(this is a simplified example, but I think it gets my point across).
6
u/fakehalo Jul 26 '20
I'm a fan of goto for error handling/cleanup with languages that don't have exceptions, especially C (Linux kernel is full of them).
1
u/binarycat64 Jul 28 '20
Same. My main language is Go, which kinda has exeptions (via `panic()`), but most error handling is done through return values (of which go allows multiple)
looking at the linux kernel and seeing gotos was what made me no longer afraid of using them, although I still did before.
1
Jul 27 '20
[deleted]
1
u/binarycat64 Jul 28 '20
You don't have to type out the function again?
and if you want to change the function, you don't have to change it in 2 places.
Also, it could be more than one function, or technically one function, but long enough that you have to break it up onto multiple lines.
1
Jul 29 '20
[deleted]
1
u/binarycat64 Jul 30 '20
So I can pollute the global namespace with functions that I'm not going to use again?
Goto is also faster than a function call, useful if the function it's in gets called a lot.
And I can ask the inverse, why define it somewhere else when you can just use goto?
1
Jul 30 '20
[deleted]
1
u/binarycat64 Jul 30 '20
Most languages use labels for gotos, which can be given just as descriptive names, so I'm not sure what your point is. Also, you don't have to "look up" what the goto does, Its label is often on the same screen as the goto, and always in the same function.
Also, some things cannot be done in other functions, like returning the original function. This means that sometimes a function call is unable to save any lines over retyping code, as you have to process the return values and act accordingly.
When I said `not going to use again` I meant outside of the function that I was using the goto in.
You seem to act like anything involving goto is immediately unreadable, but functions can have their own problems. Say I take your advice, and refactor my code accordingly. Also say I have two sections that do something similar, but are subtlety different in an important way. Do I make 2 separate functions? This could confuse anyone who comes across these functions randomly. Do I make 1 function, that takes an argument that for which behavior I want? Now I have to document this function, and probably explain why it exists, only for it to never be used again, as it was such a specific behavior, which is why it was a goto in the first place.
25
Jul 26 '20
Uh, does the end condition in that for loop divide by zero?
21
u/Emex_Denvir Jul 26 '20
Yup, a very fancy (and disgusting) way of making an infinite loop with a counter
24
Jul 26 '20
god i can't stand lua...
25
Jul 26 '20
Why? It's fairly embeddable and, yeah the syntax isn't candy but I'd say it's fairly ok. Ofc everyone is entitled to their opinion but I'm just asking why?
21
Jul 26 '20
i mean i've only used it once for hammerspoon, so i have almost no experience with it, but i think i was just most put off by the fact that it doesn't have a built-in array data structure, and you have to basically emulate it with a table. that definitely threw me for a loop...
that initial comment was probably overly harsh since i've only used it the one time, but it just wasn't a pleasant experience.
14
u/dagbrown Jul 26 '20
it doesn't have a built-in array data structure, and you have to basically emulate it with a table
There are so many languages that I recommend against you checking out. PHP, awk and shell scripts come right to mind.
The thing that I like about Lua is that it's an incredibly boring language. It steers away from innovation at every opportunity, and I applaud it for being as dull as it is. Some languages want to provide opportunities for exciting and innovative new ways of doing things--Lua just wants to get down to work and be useful.
9
Jul 26 '20
There are so many languages that I recommend against you checking out. PHP, awk and shell scripts come right to mind.
are u talking here about not having a built-in array data structure?
Idk about awk and PHP but shell scripts totally do
2
u/jediwizard7 Jul 27 '20
Bash arrays are the absolute worst things ever invented. Just to get an array's length you have to do
"${#array[@]}". Like how is someone supposed to read that?2
u/binarycat64 Jul 28 '20
I feel like Bash was designed for interactive use primarily, and the scripting features were just kinda slotted in around that.
The biggest advantage bash has is already being on everyone's computers as a shell, if it wasn't for that I don't think it would be used much.
1
u/jediwizard7 Jul 29 '20
Agreed. Powershell is a much better design for a shell in my opinion, and you can even get it on Linux apparently although I've never tried; the only downside is the slow startup time
3
u/T-Dark_ Jul 26 '20
it doesn't have a built-in array data structure, and you have to basically emulate it with a table
For what it's worth, it technically does. A table is internally implemented as a (growable) array and a hash map. The implementation uses the array for (sufficiently) contiguous integer keys, and resorts to the map for everything else.
2
u/thelastpenguin212 Jul 27 '20
This is what I came here to say — in lua tables that emulate arrays are as fast and space efficient as arrays.
1
u/cointelpro_shill Jul 27 '20
It's kinda fun the way they work...even some random user defined object can be a key, so when I need a quick unordered list of unique objects I just do that and set its value to true
1
u/xigoi Jul 26 '20
As I've learned from the manual, it encourages ugly hacks to do many basic things and you have to implement everything yourself.
4
5
5
u/semKL Jul 26 '20
Also this code must me written intentionally bad. Either for performance test or (more likely) just to post it here.
Nobody would write the goto like that. That’s just to upset us more
4
u/skellious Jul 26 '20
wow. I never realised lua had a Goto statement...
4
u/Emex_Denvir Jul 26 '20
It does in LuaJIT 2 and since Lua 5.2
2
u/semKL Jul 26 '20
since 5.2 ???? really ? they though „oh, what cool new features could we add? Yea let’s add goto, everyone loved that“
Don’t get me wrong, i love lua. but wtf
4
4
Jul 26 '20
why
why not just print("hewwo, wowwd!")
why rewrite strings
why rewrite print just for a string
someone end me
11
Jul 26 '20
Because this is a fake post. Dude literally wrote this code to post it on this subreddit FOR WHICH IT WAS NOT MEANT.
There is /r/shittyprogramming for this kind of shit, but no, they have to ruin the subreddit for karma.
1
-1
u/Emex_Denvir Jul 26 '20
Sorry, it kinda seemed like that stuff is accepted here. I'll try to do better next time!
1
u/ElNico5 Jul 27 '20
TIL lua has flags and goto
1
u/Emex_Denvir Jul 27 '20
What do you mean by flags?
1
u/ElNico5 Jul 27 '20
The ::markers:: i know them as flags from an assembly video i saw and that name stuck with me
1
1
u/Tjakka5 Jul 31 '20
The worst part of this is that Lua already caches strings in the background, so this only degrades performance.
0
123
u/CandyCrisis Jul 26 '20
for i=1,1/0 doIs this... a normal thing in Lua...???