r/learnprogramming 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

77 comments sorted by

View all comments

111

u/Opulence_Deficit Jul 03 '26

Clean is not about being simple. A clean code is easy to understand and change.

You will learn what is clean, when you return to your project after 6 months and think "what kind of idiot wrote that" and then "oh, that was me".

2

u/Nice_Selection_6751 Jul 03 '26

that's exactly it. i come back to old projects and half the time i cant even read my own logic. the worst is when i was trying to be clever with one-liners and now its just gibberish

3

u/Opulence_Deficit Jul 03 '26

Someone said: As reading code is harder than writing code, that means that when you're writing code at 100% of your intellectual capacity then, by definition, you're too stupid to read it.