This is also what gets me when people are against the assignment expression operator(:=) because they claim, "well it can be confusing if you don't know what it is"... like yeah all code is confusing if you don't know what it is. It's like people don't want to grow and want to keep writing the same code that would run on Python 2.5
back in the day jetbrains syntax highlighting support for scala wasn’t perfect.
in scala object.prop == (object prop). you can replace dot with space and sometimes syntax highlighting would mark it in squiggly reds which is annoying so i would add dots where it would do that or where it would make sense.
I would use space when it made sense as a dsl like account deposit 300 usd
one week the principle engineer became more and more irritated and wouldn’t admit to it or share his reason. A weekend comes in, he is at the end of this rope and he tells he can’t understand why i put a dot here and not there. i explain the above and he refuses to accept this, I explain again that you can simply drop the dot sometimes and i do it for aesthetics. as far as i know he still doesn’t accept it because they threw away the scala code and replaced it with beam
I had someone who didn't like the safe navigation operator in groovy for that same reason. I just had to roll my eyes and change the code so he'd approve the PR
"Don't use map and filter, they're confusing for others! Use list comprehensions instead!" "Don't use list comprehensions, they're confusing others! Use a for loop instead!"
At what point do I start to feel offended at what others feel I can or can't comprehend?
I think a lot of it comes from not really encountering significant use of things like list comprehension, filter, map, even lambdas, outside of stuff like trying to code golf challenging problems. Makes it all seem a lot more daunting than it really is.
It is so confusing to me what kind of coding job people work where you aren't using those frequently, outside of 'pure front-end' positions that are essentially designers. In web dev (and when I did QA) I feel like I use them somewhere in most solutions.
I think that depends largely on whether you have well structured code. I, for one, do not. I'm currently staring at 50 lines of purely mapping and filtering database results, none of which actually lends to using map or filter because of poor design decisions.
A list comprehension will faster when creating lists. When I was new to python I didn’t like reading them so I tended to use loops instead.
I got better at writing code now they aren’t Intimidating. But I imagine this is like that, if you are new comprehensions aren’t the most intuitive structure to read.
"At times" - yes. But even in very simple cases the difference can be quite large (20% difference when computing a list of squares from 0 to 100 [well 99]).
So it will basically replace every reference to the iteration variable by a load fast and the "load_name, load_method, ..., call_method, pop" sequence with a single list_append. There are of course cases where the difference doesn't end up being that big (e.g. simply modifying the example to instead only consider even values of x) - but in general it'd be stupid not to use it especially considering how it's also far more readable and concise.
I agree with the article if you are just doing a calculation and not saving the results to a list but the test they used to show a loop as faster then a comprehension they did nothing with the resulting value. I totally agree that if you don't need a list a list comprehension is definitely a better way to go. And numpy arrange and doing calculations on the resulting array is better.
But my argument was that a comprehension is faster when you need the values to be in a list. I think a more accurate test is just comparing a generator (range) with the for loop and append vs the comprehension with no calculation. In this base situation the comprehension is ~25% faster. There are situations where there are better tools for the job but when it is building a list from a generator and an np array isn't a point you can start at. I have not seen a situation where a loop is faster.
I think it is more because it is a purpose built structure. If you look at SV-97's comment on the other reply to my comment they provided some byte code generated by the python interpreter for a loop vs a comprehension.
In my head I read the first line as "budget dot dict for budget in budgets" and immediately understand what it's doing. The second one takes a little longer (honestly not more than a second but still)
It's not harder but it's slower to read. "This is a list where each element is a dict conversion of that other list" vs "make an empty list, loop through that other list, append the dict version of the element to that first list"
It also makes you think in different terms, the former is declarative, the latter is procedural.
It’s not so much that it’s harder to read, it’s that it is less efficient code. Python’s list comprehension is a major pro and it’s not all that complicated to understand. Like the top comment says, this is just dumbing the code down for people who stand in the way of progress and learning new things.
u/SueedBeygGood code makes sense. Bad code just works. Yours does neither.May 01 '22edited May 01 '22
I understand being intimidated by new language features you're not familiar with (everyone starts somewhere). But to go out of your way to dumb down the code rather than learn to become comfortable with it is just the wrong way to go about programming.
(reminds me of advice I heard to avoid using "tricky" features like ternary operators & do-loops in case your teammates don't get it. Like, no; if your programming vocabulary is stuck at a freshman-level, it should be on you to improve by expanding it, not me to dumb mine down).
I would personally say that this looks like something wasn't populating as expected, so it was converted to a loop so that when debugging it was easier to see what was being added, and was accidentally commited.
Why else would someone rework some existing code like this?
Same. Sometimes things aren't acting as expected because what is being passed in isn't actually what is promised.
This is especially problem with Python when you have type-hinting, which isnt enforced, so you can end up with completely the wrong data and your IDE implying it is something it is not.
I always review what I commit, but sometimes it is 10pm and I no longer care it works...
I think it has to do with the antialiasing setting of the font, and the fact that probably the picture is matching the actual ppi of your monitor and your computer settings aren't.
But Idk, it could be also that the font is slightly smaller than would normally be in your monitor and it's a happy little coincidence.
Or it could also be that you are on linux using xorg with scaling.
I just think that if that font looks good, there is probably something you can do to make your bad monitor look good, and that probably has to do with some scaling settings or somthing similar.
If I remember right, list comprehension is a generator, so the list is evaluated lazily.
With a for loop, it'll not do it lazily.
Edit: Reading the other comments brought up a second point: Lists are immutable. So a for loop creation will most likely need to create N (or even N+1) lists in the process.
List comprehension should only generate a single List(generator) so it would indeed by slightly faster in addition.
So, in Per--I mean, the language that shall not be named--we have this thing called "map"
map { as_dict } @$budget_dicts
(That code would probably get rejected because "map in void context is discouraged." If you ask why it's discouraged, the best answer is "because Damian said so." Maybe Perl does deserve the hate...)
Quick question, did you talk to him about him refactoring your code to do the same thing? I mean that had to had a conversation at least on the reason why he didn’t even talk to you prior pr that. How you guys deal with that? I usually talk to the person and I try to understand something like that, so next time he/she knows that i will say something if bs merges are in the way and eventually they star trying harder to refactor my code. I mean i would love to get feedback on my code is not a reflection of my intelligence perhaps is my mood at the time lol.
You don't see a key because this isn't creating a dictionary. I don't know exactly what budgets is, but a safe guess is a list of some kind of Budget objects. Then, they're creating a new list of the dict representations of those Budgets.
Looks like Pydantic. Good chance budgets is a list of Budget objects as you suggested, with the objects inheriting from Pydantic.BaseModel which has a as_dict func built in
The only thing I could say is that the second option visually explicitly tells you that it is appending a value to a list. For somebody who is barely familiar with list comprehensions, it is definitely not clear that it is appending.
That's because it's not appending. It's creating a new list with a known length, rather than creating an empty list, and repeatedly extending it by appending, which can lead to repeated memory allocations.
Of all the amazing things programming languages can do, some dumbass syntactic sugar that does a bit of optimization is the strongest construct in the language, that's pretty sad.
I understand that you don't have a lot of life experience yet, and that's okay. I still encourage you to learn them if you want to work with Python once you graduate. The changes highlighted by OP are ostensibly bad code and would be rejected by any team. As for "dumbass syntactic sugar"... well, our opinions will differ.
Hah! Got an honest laugh out of me. Yes, they are considered the pythonic way to write things like this. You can do the same thing with dictionaries if you want!
149
u/ichiruto70 Apr 30 '22
So, did it get approved?
368
u/ItsT1ps Apr 30 '22
The “ )” at the end of their code is the worst part
422
u/imthebear11 Apr 30 '22
This is also what gets me when people are against the assignment expression operator(
:=) because they claim, "well it can be confusing if you don't know what it is"... like yeah all code is confusing if you don't know what it is. It's like people don't want to grow and want to keep writing the same code that would run on Python 2.5127
u/303Redirect Apr 30 '22
"Don't use map and filter, they're confusing for others! Use list comprehensions instead!"
"Don't use list comprehensions, they're confusing others! Use a for loop instead!"
At what point do I start to feel offended at what others feel I can or can't comprehend?
103
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 30 '22
Image Transcription: Code
[Part of a git commit summary on GitHub, showing one line removed and three added. The removed line is:]
[The added lines are:]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
16
u/jbrains May 01 '22
What happened when you asked this person why they submitted this change?
44
u/Trineki Apr 30 '22
I'm not super fluent in python. But they do the same thing right. It just took a simplified one and made it longer and more complicated?
127
u/Putalittlefence Apr 30 '22
I get clearing up code where someone's been a bit comprehension-happy but jeez this makes it so much harder to read
32
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. May 01 '22 edited May 01 '22
I understand being intimidated by new language features you're not familiar with (everyone starts somewhere). But to go out of your way to dumb down the code rather than learn to become comfortable with it is just the wrong way to go about programming.
(reminds me of advice I heard to avoid using "tricky" features like ternary operators & do-loops in case your teammates don't get it. Like, no; if your programming vocabulary is stuck at a freshman-level, it should be on you to improve by expanding it, not me to dumb mine down).
30
u/Deaboy Apr 30 '22
Jeesh. Hard reject. Variables that change value are usually very bad for readability, especially in larger methods.
40
u/HerLegz Apr 30 '22
Literally the most unpythonic nonsense. A total waste of effort to make it more bad.
Paid by the line? Or just paid to waste time making counterproductive changes.
Some brogrammer with more ego than talent I'll wager.
9
u/[deleted] Apr 30 '22
What is that font, how is it so crispy while i am on my bad monitor?
4
u/BlakeBarnes00 May 01 '22
I need to learn list comprehension, been meaning to
4
u/DelusionalPianist May 01 '22
Chances are they wanted to debug it and check which of those elements were not like the other.
6
u/EunuchNinja Apr 30 '22
If you don’t understand code, make a note after you figure it out but don’t just rewrite it.
3
u/McKapucna May 01 '22
what am i looking at ? i am not sure if i can read it, but i wont even try. it makes me unhappy. someone please translate.
3
u/fridge_water_filter May 01 '22
There are cases where it is better to expand a list comprehension for readability, but this is not one of them.
2
u/G4rsid3 May 01 '22
Amazing you get to watch devolution happens in real time over several PRs.
Just approve this and add “One step close to allowing us to refactor this whole code base into JavaScript. LGTM”
2
u/[deleted] May 01 '22
please slap him for me
1
u/Tm1lly May 01 '22
Bleh mutability… also why include the data type in the variable name? It’s redundant
1
u/rynemac357 May 01 '22
I wanna ask is list comprehension faster than normal loops, or time complexity wise both are pretty much same ?
1
u/naptastic May 01 '22
So, in Per--I mean, the language that shall not be named--we have this thing called "map"
map { as_dict } @$budget_dicts(That code would probably get rejected because "map in void context is discouraged." If you ask why it's discouraged, the best answer is "because Damian said so." Maybe Perl does deserve the hate...)
1
u/[deleted] May 01 '22
Man every time I see a post I just say “wish I knew what this is”
1
u/sergiOO7 May 01 '22
Quick question, did you talk to him about him refactoring your code to do the same thing? I mean that had to had a conversation at least on the reason why he didn’t even talk to you prior pr that. How you guys deal with that? I usually talk to the person and I try to understand something like that, so next time he/she knows that i will say something if bs merges are in the way and eventually they star trying harder to refactor my code. I mean i would love to get feedback on my code is not a reflection of my intelligence perhaps is my mood at the time lol.
1
u/VaranTavers Apr 30 '22
It might be optimized away (I don't really know python) but it is also much slower this way.
1
u/_Freedom2020 May 01 '22
Loooool, close the pull request and fire him
-6
u/jan-pona-sina Apr 30 '22
great use case for
map()also0
u/Manny_Sunday May 01 '22
I'm not a python guy, does python not have something like C#'s IEnumerables and LINQ?
In C# you would just do
budgets.ToDictionary(b => b.Id)Or whatever, and is to_dict() a user defined method? I don't see a key defined anywhere
-7
u/katovskiy Apr 30 '22
The only thing I could say is that the second option visually explicitly tells you that it is appending a value to a list. For somebody who is barely familiar with list comprehensions, it is definitely not clear that it is appending.
-14
u/Effective-Attorney33 May 01 '22
I'm sorry but fuck list comprehensions. They are unreadable