r/badcode • u/No_Internal_3532 • Jul 11 '21
python Boy, this is some real code for generating recursion errors
185
u/haigha-earwicket Jul 11 '21
Is it bad code if it does exactly what it was designed to do?
27
u/chaussurre Jul 11 '21
I'm not so sure it actually what it is supposed to do ? Is Python able to read tail recursive functions as such ? Because if yes that's just an infinite loop.
55
u/haigha-earwicket Jul 11 '21
It's supposed to print "Error: You're Screwed" in an infinite loop. That's exactly what it does. Useful? Probably not. But much like the commercial says: "It says what it does; Does what it says."
27
u/turunambartanen Jul 12 '21
No, it will print "Error: you're screwed" 999 or 998 times (unless the default limits are changed) and then raise an recursion/stack overflow error.
9
u/v_maria Jul 12 '21
Python doesn't optimize tail recursion (last time i checked), some context http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html
13
u/Rykaar Jul 12 '21
Is Python able to read tail recursive functions as such ?
No, python does not support proper tail recursion.
8
Jul 12 '21
Not this, per se, but how many times have you seen "DON'T REMOVE THESE NONSENSE LINES OR YOU WILL BREAK EVERYTHING"? Or, more relatably, extremely non-optimized code? It does exactly what the use case needs, but it could do it in half as many lines or iterations or such. Or, most simply, very compact yet unreadable code.
0
u/sebamestre Jul 12 '21 edited Jul 12 '21
Are you familiarized with big-O notation?
2
Jul 12 '21
Well, I'm five years out of college, so yes. I think most people are.
0
u/sebamestre Jul 12 '21 edited Jul 12 '21
Great! You mentioned 'twice as many iterations' as an example of 'extremely unoptimized', so I thought it'd be cool to tell you about big-O, in case you weren't.
Nevermind, though. :p
I think most people are.
Well, it is a surprisingly misunderstood concept. Even if most people might've heard of big-O, they usually don't have even basic intuition about it.
To give an example: the other day I heard a professional programmer say that mergesort ran in O(logN). (No, they did not mean the recursion depth, or something else)
In my opinion, even without knowing how mergesort works, one ought to understand that sorting involves looking at every element of a sequence, so it can't be less than O(N).
1
Jul 13 '21
Doesn’t it actually matter? I have been programming for a couple of decades now, and it’s very rare to have millions or billions or records that need to be sorted in memory more than once. If you have a lot of data it’s probably in a database, otherwise if it’s in memory, just by the fact that it’s in memory it won’t be a lot of data so any sort or whatever sort is in the built in language will do. It will take about 0 seconds in any case or it will be a one time sort before probably storing in a database or outputting to a file.
If you need the same data sorted on a regular basis, you are probably doing something wrong.
Is there a scenario where the speed of the sorting would impact you enough to care?
I find that if the design and structure is good and well thought out, you never have to worry about performance, everything will run quickly.
1
u/sebamestre Jul 13 '21
The difference between O(logN) and O(N) is VERY large
If you have even 10k records, an O(N) algorithm inside an n-iteration loop is a big no-no, as it will take several seconds to finish. (Because the whole loop will be O(N2))
For example, it was recently found that most of GTA5's several-minute startup times were due to an accidentally quadratic algorithm
1
Jul 13 '21
the question was, does it actually matter. I skimmed an article about gta 5 load time discovered by a user t0ast, if that’s what you are referring to, but it proves my point. The program flow was wrong doing a repeated unnecessary task 63k times and hashing some things 63k times in a loop, so it’s not sorting, and if something is slow it’s due to design and structure, logic, flow, .etc.
The solution to the problem, I didn’t complete the article, but the solution should be not doing the unnecessary tasks, it should not be doing the unnecessary tasks faster. It doesn’t matter if it’s o of nm^2-30log29001
The loading would not have been an issue if he was doing the necessary steps once and only once
1
u/sebamestre Jul 13 '21 edited Jul 13 '21
Yeah, that's the one. I think it does matter, let me explain my thoughts, I apologize if I go on for too long
In the GTA5 example, there were records that needed to be deduplicated. This work was supposedly necesary, and not some kind of oversight.
How would you implement this? I can think of 3 ways of the top of my head.
For every element iterate over the other elements, looking for an equal element, and discard the element if there is a match.
Sort the elements, then discard any element that is equal to its neighbor in the sequence
Make a hash table, then iterate over the elements. If an element is in the hash table, discard it. Otherwise, put it in the hash table.
Why would one pick one over the others? I can think of two reasons. First, simplicity, in which case solution number 1 is the way to go imo. Second, speed, in which case solution number 1 is a no-go.
Let's say, for the sake of argument, that we go with solution 2. Which sorting algorithm would you use? Certainly not a quadratic one, right?
You might say that you'd use the one from you language's standard library, so no algorithm needs to be picked. But that is just wrong.
It's not that no algorithm needs to be picked, but that the implementers of the standard library already thought about it, and picked an algorithm that runs in O(NlogN).
It's not that it doesn't matter, but that it matters so much that someone else, or more likely a group of very smart people, already made that choice.
In GTA5's case, they went with number 3, but they made a mistake in their hash table implementation, making it do essentially the same amount of work as solution 1.
Does this mean what I've said is irrelevant? No, people can make mistakes which makes their code slower than they intended, that's just how things are with human beings. We should still strive to use the right algorithms and data structures for the problem. (Otherwise, we might say that since humans make mistakes, we shouldn't try to write correct code)
Note that we can pick any of the 3 approaches, with no effect on system design or architecture. Also, no choice in design or architecture would remove the need for deduplicating the data.
The obvious conclusion is that this is not about design or architecture, but about picking the right algorithm.
1
Jul 13 '21
From what I read from the article I skimmed they double looped it, so the inner loop is unnecessarily doing the same task 63k times with every iteration.
All they needed to do, as far as I got into the article, was read and hash 63k items once with multi threading instead of in one thread, if they needed to also download missing items from the item store, they can do that in a queue with 5-10 connections, so as you are going, you are looking for the items, reading items, hashing something, downloading something else, so you are utilizing the cpu, io, network, but also, and most importantly, not double looping if that’s a term or doing unnecessary steps, but I know gta 5 likes it’s micro transactions so their managers were problems like
Manager: Loading seems to take time Dev: it’s to make sure the store includes all the cosmetics and cars Manager: double and triple check it if need be, we are gonna get that money
→ More replies (0)1
Jul 12 '21
Yes, I realize 2n reduces to n. Feel free to explain it if you want, but it would be missing the point of the comment.
0
u/sebamestre Jul 12 '21 edited Jul 13 '21
Hey, I just wanted to share something useful, in case you didn't know about it already. (You might've been one of today's lucky 10000)
Sorry if I came across as disrespectful
65
39
14
u/rightfor Jul 11 '21
That's not bad code. Couple of error print statments and recursion is just a bad attempt at humour.
43
u/invented-damage Jul 11 '21
Image Transcription: Code
def __init__():
print("Error:")
print("You’re Screwed")
print('')
__init__()
__init__()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!
12
4
u/green_kerbal Jul 12 '21
Why underscores? Is this some python fuckery?
9
u/sandmasterflash_ Jul 12 '21 edited Jul 12 '21
Underscores are perfectly valid identifiers in python. You can write an entire program with all your variables and functions as underscores.
Normally when creating a class you’d define
__init__as the initializer that gets automatically called whenever an instance is created.class Example: def __init__(self, foo): self.foo = foo bar = Example(10)However, it’s not going to do anything how it’s used in the picture. There’s no reason to just randomly have a function called
__init__it’s just silly. And you can tell it’s outside of a class because it’s missing self, which is an argument that gets passed to all methods that just represents the object instance. If this is inside of a class it’ll throw an error. If it’s outside it’s just weird and gross.1
3
u/Intrexa Jul 12 '21
Is this some python fuckery?
Yes, yes it is. Magic variables that python uses as controls are prefixed by double underscores. Like, when you initialize a class, python calls the
__init__method. When you do something likea + b, python callsa.__add__(b), so to overload addition for a class, override the default__add__method.2
u/sebkuip Jul 12 '21
If you see a class with a method with __name__, this method becomes a magic method. Functions like len(), str(), and some other things (like creating an instance of the class) will call these magic methods. __init__ is called whenever you create an instance.
3
3
3
u/cheetahound Jul 12 '21
imagine leaving it running for 10 years and then raising a KeyboardInterrupt after that
3
u/AnimaLibera- Jul 12 '21
The worst about this code is the inconsistent use of quotes ("..." at first and then '...' out of nowhere)
2
3
u/ssj4VB Jul 12 '21
people say that this isn't bad code because it works, but this just throws recursion errors after less than a second
1
u/reverendsteveii Jul 12 '21
Is this bad code if it does what it's supposed to but also exits in O(literally forever) time?
1
u/Johanno1 Jul 12 '21
Naahh this is not evil enough.
In python you can override every method and variable.
Override the print method with a crypto miner!
1
1
-14
u/burlyginger Jul 11 '21
Pretty sure that won't run because there is no 'self' declared.
22
19
u/moopy389 Jul 11 '21
It doesn't have to be part of a class. Could just be a regular function.
Even as part of a class. You can run it as a static function of said class. You just won't be able to instantiate the class.
You can test this out in like 30 seconds.18
263
u/[deleted] Jul 11 '21
It is funny meme code, but not a good example of bad code. It works as intended...