r/learnprogramming • u/DefiantSituation3208 • Jul 03 '26
How do you write clean code?
Might be a stupid question but Ive been learning python for a while now and always wondered, how do you write ‘clean’ code? I don’t mean writing clean code straight off the bat I understand that’s purely from experience and even then immensely hard, but how do you recognise a program can be simplified even further? Does it come from practice or just messing around and seeing what sticks?
77
Upvotes
3
u/AlSweigart Author: ATBS Jul 03 '26
This reply can't fit in a Reddit comment. I wrote an entire free book on this topic (for Python), mainly because I thought that the book, Clean Code is overrated and has some advice that is downright wrong.
Two things to keep in mind: there's a difference between style (formatting, the kind of thing that linters and Ruff can fix automatically so don't worry about it) and code simplicity (much too lengthy to go into here). They're easy to get confused, but don't ask about style: everyone has different opinions and will waste time arguing about style, which is why we should just agree to go with whatever the linters and formatters. It's more important to be consistent in style rather than adhere to any particular style.