💡
1
c/coding-for-beginners•jamie940jamie940•1mo ago

A Python meetup in Austin last month finally got me to stop overcomplicating my code

I always thought writing clean code meant using every fancy feature I could find, like lambda functions and list comprehensions everywhere. Then a guy named Dan at the Austin Python Users Group showed me his 50 line solution to a problem I had 200 lines for. He just used simple for loops and if statements, nothing clever at all. Has anyone else had to unlearn bad habits from trying to be too smart too early?
3 comments

Log in to join the discussion

Log In
3 Comments
west.richard
Yeah I had the exact same wakeup call a few years back. I was writing these insane one-liners with nested comprehensions and map functions (you know, trying to look smart), then someone refactored my code and I couldn't even follow what it did anymore. The trick that stuck with me is writing code like you're explaining it to someone who's tired and hungry, simple steps that are easy to trace. Now I just use regular loops with meaningful variable names and maybe a helper function if things get long.
2
jason_lewis3
Man, three years ago you wrote a list comprehension you couldn't read? @west.richard that's not exactly a crisis, is it?
9
kimr74
kimr741mo ago
A readable loop beats a clever one-liner every time when you're debugging at 2 AM.
-1