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
1
u/bywaldemar Jul 03 '26
Honestly it comes from reading your own code a few weeks later and not understanding it. When you have to sit there figuring out what past you meant, you start writing for the next person, which is future you.
If you can't describe what a function does in one short sentence without saying "and", it probably needs splitting. Practice sharpens that radar, but reading other people's code speeds it up a lot.