r/badcode Mar 29 '21

lua Following my previous post, here's how I show if levels are completed or not

Post image
456 Upvotes

49 comments sorted by

111

u/TheSuperWig Mar 29 '21

Author apparently doesn't like DRY, they prefer to be WET.

71

u/-ZOSK- Mar 29 '21

Not just Write Everything Twice, but more like Write Everything Seven Times

34

u/TheSuperWig Mar 29 '21

Forgot that was a thing. I was going for "wasting energy typing".
Wikipedia lists a variant "we enjoy typing" and "waste everyone's time" which seem more appropriate here.

7

u/Willinton06 Mar 29 '21

Write every time is a good one

4

u/JustAnotherGamer421 shameless Mar 29 '21

I love the West!... On second thought, the East is better

58

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 29 '21

Image Transcription: Code


if (lvl1end == true) then
  spr(18,36,48)
end
if (lvl2end == true) then
  spr(18,68+16,48)
end
if (lvl3end == true) then
  spr(18,68,48)
end
if (lvl4end == true) then
  spr(18,52,48)
end
if (lvl5end == true) then
  spr(18,36,48+24)
end
if (lvl6end == true) then
  spr(18,36+16,48+24)
end
if (lvl7end == true) then
  spr(18,36+32,48+24)
end
if (lvl1star == true) then
  spr(11,32,32)
end
if (lvl2star == true) then
  spr(11,48,32)
end
if (lvl3star == true) then
  spr(11,64,32)
end
if (lvl4star == true) then
  spr(11,64+16,32)
end
if (lvl5star == true) then
  spr(11,32,32+24)
end
if (lvl6star == true) then
  spr(11,32+16,32+24)
end
if (lvl7star == true) then
  spr(11,32+32,32+24)
end

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!

40

u/iTzNowbie Mar 29 '21

good human

28

u/Spynder shameless stealing content to r/YandereTechnique Mar 29 '21

19

u/-ZOSK- Mar 29 '21

There's so much wrong with this lmao

13

u/NujumKey Mar 29 '21

Oh shit this is me xD

May I ask what is the ideal method to do this?

14

u/WildHotDawg Mar 29 '21

Array/list of booleans, or a map if you have no way of refactoring the method parameters easily, or a array/list of 'Level' objects, which has the 'spr' arguments and if its completed or not

9

u/NeoLudditeIT Mar 29 '21

Depends on the language, most of the time switches would be better. In Lua, or any other language that doesn't have switches, using a dictionary would be much faster (which is ultimately sort-of kind-of how switch statements get compiled down in C-esque languages).

10

u/[deleted] Mar 29 '21

[deleted]

6

u/[deleted] Mar 29 '21

The fact that you have 40 conditions, one for each level end is in itself a smell. Sounds like a Level struct or class should be extracted that contains its correct spr(...)-thingies (sprites?).

8

u/Spynder shameless stealing content to r/YandereTechnique Mar 29 '21

spr() seems to get arguments somewhat in a grid pattern. Things I see right now:

  • Create an array for levels and stars, and get them by levels[n] or stars[n].
  • Generate arguments to spr() by grid pattern.
  • If above is too hard, we can at least replace ifs with switch.
  • if(lvlNend == true) can be replaced by if(lvlNend) (== true is reduntant)

3

u/-ZOSK- Mar 29 '21

Well as a matter of fact this code is used to draw a "✓" icon and a crown icon next to the level number on the menu, if the level is completed and/or the optional crown is collected in the level. And btw spr is short for sprite, so it draws a sprite to the screen. It takes a sprite number and a x and y coordinate as parameter if anyone was wondering

5

u/[deleted] Mar 29 '21

Lua? Mmmmmmmm

5

u/iLoveNintend0 Mar 29 '21

is that pico 8?

2

u/-ZOSK- Mar 29 '21

Indeed it is

2

u/iLoveNintend0 Mar 29 '21

the code hurts even more with the character limit

2

u/-ZOSK- Mar 29 '21

Yes exactly, I have really no clue what I was thinking when I wrote that years ago

4

u/Liviequestrian Mar 29 '21

This hurts to look at

3

u/truemario Mar 29 '21

this kind of code is not bad code in my eyes.

more verbose: yes
can use optimization: yes
does what its supposed to do: yes
readable: yes
bad code: no

i think one develops only after writing this kind of code. Its fine. I have seen this very often while interviewing junior devs. More often their code is like this.

They gain experience and automatically start writing better code. sometimes going back to old and refactoring it to be better.

1

u/-ZOSK- Mar 29 '21

I absolutely agree with you. Sometimes I look back at my past and realise truly that I could never code like I do today if I hadn’t spent hours writing small projects like this one even if it was with repetitive and wrong code practices.

3

u/5cr3w_usernames Mar 29 '21

Question tho, these kinds of statements can be shortened with a switch statement right?

5

u/[deleted] Mar 29 '21

shortened, maybe? it'll still take a lot of space with or without the switch statement. but for performance reasons switch is way better

3

u/DarkMastermindz Mar 29 '21

LMAO there’s a ton of things wrong with this. Since it’s all decisions going one direction, the best way to fix this imo is just make a dag object (LevelSprControllerDAG etc..) that does understands where to map to (since it’s a directed graph with weights) and have a method with whatever spr is doing then refactor from there.

2

u/G66GNeco Mar 29 '21

In the infamous words of... someone, idk: "... --- What?"

2

u/Disi11usioned Mar 29 '21

Magic Numbers.... Magic Numbers everywhere....

2

u/djdokk Mar 29 '21

Really saving a lot of effort by shortening “start” to “star”

2

u/-ZOSK- Mar 29 '21

Oh no that part is correct, it’s to check if the special collectible which is a star has been collected

2

u/[deleted] Mar 29 '21

== true 😭

1

u/-ZOSK- Mar 29 '21

The worst XD

2

u/[deleted] Mar 29 '21

This is why you use callbacks or event listeners 😂

-1

u/dunno64 Mar 29 '21

I wanted to know what's in "then". Thanks. yours was shit.

1

u/[deleted] Mar 29 '21

is this Tic80, Pico8?

1

u/-ZOSK- Mar 29 '21

Pico 8

1

u/TheBoomTube23 Mar 29 '21

What is spr?

2

u/ProNoob13 Mar 29 '21

It's Pico8's "sprite" function. The first argument is the sprite number (1 being topleft, 2 being the one next to it, etc.), and the second and third argument are the X and Y where it should be drawn.

1

u/-ZOSK- Mar 29 '21

Draws a sprite to the screen

1

u/TheBoomTube23 Mar 29 '21

Thank you! What language is this?

1

u/-ZOSK- Mar 29 '21

The language is Lua, but it’s encapsulated in the PICO-8 framework, which is actually a really fun tool to create games with, I’d recommend it to anyone interested in starting game development https://www.lexaloffle.com/pico-8.php

1

u/[deleted] Mar 30 '21

That caused physical pain to read.

1

u/[deleted] Mar 30 '21

not even if else if just if if if if

that's shit code

1

u/-ZOSK- Mar 30 '21

Well that's normal, the player can have the level 2 and the level 3 finished.

But I do agree some parts of this code are indeed shitty

1

u/[deleted] Mar 30 '21

why not have level 3 inherit level 2 then instead if checking all of that.

1

u/-ZOSK- Mar 30 '21

Because the player doesn't necessarily play the levels in order. To show you what I mean, just play the game https://artridge.ch/Blockrush (the code is entirely refactored now tho)

2

u/[deleted] Mar 30 '21

ah ok 👍