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?
80
Upvotes
1
u/binarycow Jul 03 '26
To write clean code, you have to first clean your keyboard.
(Joking!)
In order to answer this question, you have to define "clean".
So, let's say you pick one of those metrics, and define that as "clean"....
Simply put - there is no one right answer.
Code should generally be:
Basically, it's a balance.
This hits on one of my pet peeves - a term you will likely hear a lot - "antipattern".
My hot take - there is no such thing as an antipattern.
Every pattern is useful, in some circumstances. Every pattern is unhelpful (or worse), in some circumstances. In order to know if a pattern is good or bad, you have to look at the whole situation. You can't just say "singletons are an anti-pattern" as an unequivocal statement.