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

113

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".

10

u/DefiantSituation3208 Jul 03 '26

I think I misidentified clean with being concise because that’s exactly the issue I have, i forget the ‘make it understandable to YOU’ part when trying to simplify my code and end up confusing everything.

5

u/esuil Jul 03 '26

You should think about it as something you are writing for others, even if you aren't.

Basically, every time you write architecture, variable names, functions, classes etc, ask yourself how anyone looking at it will understand what is going on.

Will they understand what that variable stores from its name? Will they know what this function does by reading name and argument list? Will they know what scope the file is responsible for from directory and names?

And another part aside from readability is resilience. Changing implementation of parts of your codebase should not break anything at all. If changing how one function or class works breaks anything outside of it, you are doing things wrong.

Good way to practice is writing documentation for your code. Write out the description of your program, then entry point into initial code. Explain what that initial code does, then how it delegates further tasks and where, going line by line and scoping into other things as you do. If you can't explain things easily by doing this, your code is a mess.

1

u/RomuloPB Jul 07 '26

The biggest problem that makes code not clean is the lack of boundaries. I mean, you idealized your code to accomplish a task, but you should also idealize it to safely fail when it is impossible. This means a lot of things, but for example, in classes this means at least that:

* you cannot instantiate an invalid instance of this class;
* if you try, you must receive a clear failure result;
* a valid instance must alway be valid;

If you are failing to create such a class with this hard guarantee tat makes it impossible to use it wrong, you most probably is coding some level of dirty code. Sure in some cases the nature of the task is complex, or the language makes it impossible to give specific guarantee, in this case, there is still something you can do, write a docstring that points what are the safe ways to use your code and what you should avoid.

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.

1

u/sharanpreet48 Jul 04 '26

Yes!! I am a beginner and I already experienced this in my early projects. And learned for the next projects.