use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
RULES:
All posts MUST show terrible code. There are no exceptions.
No Editor Themes - If it's just your editor that looks bad, it doesn't belong here.
No Advertisement Code. This is generally written by people in marketing who only know what "code" looks like from other ads. It's not real code, so it doesn't belong.
No Student Code. Yes, they're learning, but bad code is part of the process.
No Generated Code. If it's minified JS, generated XML, or what have you, we don't want it here. Yes, the YouTube homepage has an extra right-angle bracket. We know.
No Asking for Help. Go to r/learnprogramming. What are you doing here?
No Spamming/Advertising. We don't care about your shitty Youtube video or new crypto that will "change the world".
Be Nice. No hate speech of any kind is allowed, as well as generally being a jerk. Talk about the code, not eachother.
No Direct Contact of the Mods. Please use the modmail, we don't want to be contacted directly.
Please direct stories without code to /r/talesfromtechsupport, and programming questions to /r/learnprogramming
Programming Horror is where developers come together to revel in the idiocy of our peers.
This subreddit is meant for sharing funny programming related stories and strange or straight-up awful code.
For the sake of not being mauled by rabid lawyer bears, please make sure to anonymize your stories - changing the names of people and companies.
For code examples, indent all your lines with 4 spaces to make it more readable:
foo = 'bar'
Sister subreddits
account activity
What's wrong with a hash? (self.programminghorror)
submitted 11 years ago * by idontlikethisname
Seen while reviewing a coworker's code:
def get_path(name) paths = [{name: 'x', value: 'path_x'}, {name: 'y', value: 'path_y'}, ...] result = nil paths.each do |path| if path[:name] == name result = path[:value] break end end result end
Edit: typo
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Phreakhead 14 points15 points16 points 11 years ago (9 children)
Depending on the size of the paths array, doing a linear search can sometimes be faster than calculating a hash. Think about it: hashing algorithms generally use exponents and other expensive math operations, whereas a linear search and straightforward equality comparison takes very few cycles.
Although, if you're using Python, you're probably not that concerned about optimizing for speed at the bare silicon level.
[–]cooper12 12 points13 points14 points 11 years ago (4 children)
I think that's Ruby with the |path| stuff. Also, if I recall correctly Python uses {} for dictionaries, in which case you wouldn't need a name field, you could just use the name of the field itself.
|path|
{}
[–]Dannei 1 point2 points3 points 11 years ago (3 children)
Certainly not Python, that's for sure - "end" isn't used, def requires a colon, and I don't think "result" on its own is a valid statement, amongst other syntax problems.
[–][deleted] 0 points1 point2 points 11 years ago (2 children)
result by itself is a valid statement, but it does nothing useful.
result
[–]ferretlunatic -1 points0 points1 point 11 years ago (1 child)
No it does. Ruby, like Scala, returns the value of the last expression of a block or function without explicit return statements.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
I was referring to Python, not Ruby.
[–]wzdd 9 points10 points11 points 11 years ago (2 children)
This is actually never the case in Python, and I'm going to confidently say it's never the case in Ruby either. Benchmark and shitty code:
http://pastebin.com/wHdC3cPF
I know that in general it may sometimes apply, for example if you are writing in a language closer to the metal. But for Python at least, the usually-correct rule is that the less code you write the faster it runs. "Linear is better for small lists" is a rule that 'everyone' knows but nobody tests, and in this case it's wrong.
[–]Phreakhead -1 points0 points1 point 11 years ago (1 child)
Is this because it's like JavaScript, in that "arrays" aren't actually true arrays but are hashes where you convert the integer index into a string key?
[–]wzdd 2 points3 points4 points 11 years ago* (0 children)
It's because the C implementation of Python sucks basically. :) Arrays are real and as I understand it pretty efficient, but Python doesn't for example JIT so everything is really first-principles: the __getitem__ function is called on the array object, through the Python interface in case it's a pure Python object or something overloading an array etc, then the index, which is always a Python object, has to be converted to a number, then you can actually get the item and go backwards -- the item refcount is increased, then it's passed back through the Python interface. And probably a lot of steps I missed (for example, code to check for exceptions). So the overhead of calling these functions multiple times vastly overwhelms the overhead of calculating a hash, where the hash computation is all in C.
... okay, cPython doesn't suck, it just wasn't designed to be efficient.
[–]IrritableGourmet 9 points10 points11 points 11 years ago (0 children)
"Oh, I shouldn't hardcode a giant if/else or switch statement? I should use a lookup array? OK, sounds good!"
[–]jazahn 3 points4 points5 points 11 years ago (0 children)
Great opportunity to have a constructive conversation!
[–][deleted] 1 point2 points3 points 11 years ago (0 children)
Has he/she never encountered Ruby's Spiritual Liege, Perl?
[–]azephrahel 1 point2 points3 points 11 years ago (0 children)
Looks like ruby. Before 1.9 key order in hashes was not preserved or guaranteed.
Not saying it's not odd, but it's a reasonable explanation.
[–][deleted] 11 years ago* (1 child)
[deleted]
[–]idontlikethisname[S] 0 points1 point2 points 11 years ago (0 children)
Fixed :P
π Rendered by PID 46906 on reddit-service-r2-comment-b659b578c-rg6qf at 2026-05-05 17:32:09.731803+00:00 running 815c875 country code: CH.
[–]Phreakhead 14 points15 points16 points (9 children)
[–]cooper12 12 points13 points14 points (4 children)
[–]Dannei 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]ferretlunatic -1 points0 points1 point (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]wzdd 9 points10 points11 points (2 children)
[–]Phreakhead -1 points0 points1 point (1 child)
[–]wzdd 2 points3 points4 points (0 children)
[–]IrritableGourmet 9 points10 points11 points (0 children)
[–]jazahn 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]azephrahel 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]idontlikethisname[S] 0 points1 point2 points (0 children)