8 ms·
How to Write the Worst Possible Python Code (Humor)
- deleted 1y ago[deleted]
- photios 1y agoDisappointed that my favorite pythonic pattern of "here's a dict with unknown contents, do the right thing with it" is not listed.
- sunrunner 1y agoHey now, at least the dictionary has keys that _could_ hint at the contents (or be completely misleading). What about the tuple with just positions? image.size ndarray.shape Are the image sizes (width, height) or (height, width)? Trick question of course, it's (height, width, channels) for numpy. numpy is fairly well known though and sort of gets away with it, but when your never-seen-before internal company starts doing this, well...
- vidyava 1y agoSerious question, but what is the best practice for keeping n-dimensional arrays organized/labeled? Pandas? Xarray? Converting everything to netcdf before using it?
- msgodel 1y agoIt's not just Python. You see this a lot in Javascript and Groovy too. Really I think what makes languages with manual memory management and limited built in types like C nice is it forces people to really think about the types and interfaces they're writing instead of just hacking unmaintainable code at 20 lines an hour.
- alfons_foobar 1y agoWhat do you mean, "unknown contents"? Just trace back the dict to it's origin, through all the places it can be modified, and you know what is (or could be) inside... /s
- naveed125 1y agohaha, i should add this.
- ck45 1y agoSeen (and fixed): A function returns some “struct” (as a dict), with a couple of fields, mostly stringly typed, and one field for accumulating error messages called “errors”. The dict was created as a defaultdict(list) (probably) to make it easier to append messages.
- RadiozRadioz 1y ago> abandoned packages with no recent commits So, stable packages?
- hsbauauvhabzb 1y agoFlip a coin, you might get lucky, and that’s how all great applications are built.
- seanhunter 1y agoI once was in the position of becoming pr approver[1] for a team of outsourced python programmers who were under some pretty extreme deadline pressure. Anyway one day a PR comes in and I can’t help but notice it was doing a string eval. Weird. You almost never need to do string eval in python, and whenever there is something where you think you need eval there is a better and safer way to achieve the same result. Also, I was bending my brain but I couldn’t really figure out what this eval was for until I wrote out some scratch code myself to figure it out. Turns out this 5 lines or so was constructing a string to do dict lookup and then evalling that. So say you have a dict d = {‘foo’: ‘bar’} and you have a variable i=foo and want to look up d[i], instead of just doing that it was doing something like eval(‘d[‘+i+’]’) Just no. So I rejected the change and they came back with “but we’ve always done it that way”. I grep the codebase and yes. There were about 200+ uses of eval, all of which were constructing a string to look something up in a dictionary and then evaling the result. Some person who clearly didn’t program in python had found this twisted way to look things up in a dictionary and then this team had dutifully copied and pasted it throughout the codebase. [1] ie I wasn’t there from the start of the project
- kryptocannon 1y ago> So I rejected the change and they came back with “but we’ve always done it that way”. I grep the codebase and yes. There were about 200+ uses of eval That's code review's worst! Happened to me many times.
- graemep 1y agoI came across something quite similar. Using eval to convert a string to a dict. The string potentially included user input.
- seanhunter 1y agoI wonder whether my dudes cut and pasted from the same cursed stack overflow snippet as your dudes had. The strings here included user input too. Worse still, the situation was the company was offering a b2b service and the string didn’t just come from user input by an employee of the company they came from arbitrary customers of the customers of the company.
- deleted 1y ago[deleted]
- sim7c00 1y agooh but theres so much more worse xD... but i guess that goes in general for code. fun read tho and with reverse psychology u can maybe learn something as a novice snake wrangler xD
- naveed125 1y agothanks :)
- in12parsecs 1y agoI am teaching python to a group of beginner programmers. This will be shared!
- baobun 1y agoCaveat #13
- Zeppelin1492 1y agoAlternative for rule 1 - give descriptive names to your variables, but then just reuse them throughout the function for all kinds values and purposes. The longer the function the better. Example somethingService = ... somethingService = somethingService.Do() Really fun to debug.
- xg15 1y ago> Rule #13: AI-Assisted Coding is for the Weak Me reading this article: yup, yup, yup, yup, wait a minute! This part feels a bit snuck in, in a "leading and pacing" kind of way: All the other points are long-established no brainers, but this one is still controversially discussed and I'd say - in the general form it's presented here - wrong. The author is still right that it's wrong to categorically dismiss AI tools when coding. But you'd have to apply a lot more caution to this point than to the others. I hope sneaking this in wasn't the real motivation of the article.
- baobun 1y agoClassic listicle trick: All the obvious ones and one random controversial hot take to get the engagement going.
- hsbauauvhabzb 1y agoJokes on you, the article is ai written
- naveed125 1y agobusted
- ck45 1y agoDouble pro tip for naming identifiers: Overwrite built ins; dir, list, len, file, those are all beautiful identifiers and debugging the resulting bugs is twice the fun
- antisol 1y agoWell hey there chatgpt! I recognise your writing style anywhere — it's like a bad metaphor where you end up saying "what?".