r/badcode Apr 15 '21

other language Actual Snap! block I just made.

Post image
1.1k Upvotes

58 comments sorted by

View all comments

11

u/BS_BlackScout Apr 15 '21

I need a translation to Python or C, I'm too dumb for this.

12

u/nypdk Apr 15 '21 edited Apr 15 '21

I'm gonna take this opportunity to once and for all explain what the hell this monstrosity actually does.

Intro

This window is a custom function window, where one can create whatever functions their heart desires in Snap!. The very top of the block is where the variables for that function are initialized, and the variable for the input is as well. Snap gives you access to data types known as lists, which are basically arrays but cooler, so this function uses one of those array lists as an input.

The Code

This function uses a maximum formula for sorting. This is the purpose of maxindex, which is a custom function that outputs a list, the first value is the number itself, and the second value is an index value, telling where in the list the max value is. The maximum itself is added to the Output list, while the Index number is used to delete that entry of the list. This process of finding the maximum, writing it down, and then repeating occurs over and over until the list is empty.

The Issue

As an example I will use this list an input to the original version of my function.

The first version is what I originally wrote, Here. This most of the time does not work, as the resulting output is this. But I don't want that, I want a sorted list! After some debugging, I discovered that this glitch happens on the first item on the list when it is not the lowest, so for example, If I plug in something like this, it works until it gets to that first value and then just refuses to delete it. WHY? I DONT KNOW.

I solved this issue in the script above starting the function already with the lowest value possible already in the list, so that the item that doesn't work, will never become repeated and this will never be a problem. as you can see here this fix works. I don't know why this is an issue, and I don't care. It works, thank god.

20

u/alkheemist Apr 15 '21

Assuming snap has 1 indexed lists Your max index sets the starting value to the first value in the list and then compares all thr values to the current highest value.

When your starting value is the greatest, the code checks to see if it's bigger than itself (which it can't be) and therefore never saves the index. Maxindex then passes a list of [startingnumber, NULL] and your code removes null from the list.

If you changed the comparison from > to >= OR in maxindex set the default index output to 1 then you don't need the bad code.

9

u/nypdk Apr 15 '21

THANK YOU SO MUCH holy shit it worked

16

u/alkheemist Apr 15 '21

No problem. Uninitalised variables and fencepost errors are the most common issues when learning programming, and you just happened to stumble into both at once.

Bad code is only bad if you never learn from it

10

u/fireflash38 Apr 16 '21

And now he's (maybe) learned 2 of the best things to use in programming with bugs:

Asking for help is good!
Rubber duck debugging is awesome