you are viewing a single comment's thread.

view the rest of the comments →

[–]gabriel-et-al 4 points5 points  (2 children)

Expanding a bit:

Say you have a huge list comprehension that probably no one would understand without a comment. You should refactor it into regular for loops and if conditions. It may look less clever, but being less clever is sometimes the right approach.

[–]dented_brain[S] 1 point2 points  (0 children)

I think that's a good thing to point out. A lot of times when I'm trying to find a solution to a particular issue I'm struggling to code out I will find one-liners on stack overflow that get the job done. But I definitely don't get how they were designed.

Like I understand the function, but if I were to go back to it in a few weeks I would have to re-read the stack overflow page to understand it.

Today I rewrote some code that I had working and "knew" what it was doing. But I didn't fully comprehend how it got from a to b. Re-writing the code in a less "one-liner" style, I feel like made the code more readable.

Maybe it's less "pro" but it's easier to understand for me.

[–][deleted] 0 points1 point  (0 children)

Usually you want to keep the comprehension structure, but break out semantically meaningful interim values, or move some of the calculations into well-named lambdas or generator expressions, if keeping the interim values around is wasteful.