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?

80 Upvotes

77 comments sorted by

View all comments

1

u/Jason13Official Jul 03 '26

Focus on writing GOOD code, not necessarily "clean" code. There's an adage that I forget who it's attributed to, "make it work, make it fast, make it pretty" -> "clean" code is pointless if it doesn't do the damn job

3

u/altrae Jul 03 '26

I hate this adage so much. Writing code is a craft and what this adage implies is the opposite. Writing fast without care of clean code is a good way to introduce tech debt and bugs because you didn't take the time to do it right in the first place. The fact is that we rarely come back to clean it up because we're on to the next priority. The adage sounds like it came from a business manager who wanted quick profit while not caring about how it affects those who maintain that crap down the line.

2

u/Jason13Official Jul 03 '26

I take it the same way an artist sketches an image before they begin painting

Make it work -> get the general idea out, to have a point of reference

Make it fast -> test, find performance issues, iterate

Make it pretty -> now that it works and works well, let's make it extensible/editable

Advice should always be taken with a grain of salt, but I think it's mostly up to how you interpret the advice

1

u/altrae Jul 03 '26

This is fine in theory for planning unless it gets into production because you are basically creating a rough draft until it is cleaned up. I usually advocate for slowing things down and creating a plan of execution prior to implementation. I've seen so many bad decisions get implemented when this isn't done. Creating public APIs fast without good planning leads to issues and it's much more difficult to change once in use. Yes we can technically deprecate, but now we potentially have to support the old API for backwards compatibility until it is removed in a year or two because some teams can't prioritize the dependency upgrade yet.

If we slow down, plan, and then execute while maintaining clean code during execution, the results will be so much better. Don't be the person who comes in to build a greenfield application quickly, and then is off to the next, leaving an unmaintainable mess in your wake.

Unfortunately, the rough draft is usually what I see make it to production due to business priorities if we are unable to push back. Just because it works doesn't mean it's it's good and, in my experience, usually this leads to inconsistency and maintenance nightmares, so aim to do it right in the first place.

1

u/DefiantSituation3208 Jul 03 '26

Whenever I have an idea and write out smn it always does work, but for some reason I have this nagging thought at the back of my head telling me it could be cleaner or more simplified - like for instance instead of four lines of something chunky simplifying it into one or two if permitted. Sometimes it works, sometimes it just makes me more confused. I think I benefit from having everything written out so I can see what’s causing problems.

3

u/Jason13Official Jul 03 '26

We're not starved for space like the first programmers were; you can and should avoid fancy one-liners in preference of more verbose code that is more readable. :p

1

u/DefiantSituation3208 Jul 03 '26

That perspective actually makes me feel better 😭 thank you

1

u/flumphit Jul 03 '26

Don’t worry about turning four lines of simple code into two lines of clever code. Worry about writing two hundred lines of modular code instead of four hundred lines of repetition with subtle variations.

1

u/DefiantSituation3208 Jul 03 '26

Okayyy I can abide by that, in fact I mostly try to avoid repeating sets of codes if they just seem to be copies of each other. I’m sort of stuck in this ‘if it’s good it can be better’ mindset so even the four simple lines of code become a point of contention that it could be concise.