Clean Code vs Code Smells

This weeks topic is all about clean code as well as code smells. But what exactly is clean code?

Clean code is a term used to describe computer code that is easy to read, understand, and maintain. Clean code is written in a way that makes it simple, concise, and expressive. It follows a set of conventions, standards, and practices that make it easy to read and follow. (Cocca, 2023)

After reading more about this topic, I was immediately reminded of my internship last summer 2023 as a backend engineer and my lead telling me about the “Zen of Python“. I had never heard of these guiding principles before, but after writing some messy intern code, I realize now that the Zen of Python serves as a simple reminder and roadmap for prioritizing pragmatism.


One of the most important lessons I learned was that “Simple is better than complex“. When I first started coding in a professional setting, I was definitely lost trying to navigate the codebase. It was like being dropped into a foreign country where I didn’t speak the language. I’d try to write these tasks from scratch without even considering that someone else may have already solved the problem I was working on. That was probably my biggest rookie mistake. I’ve come to realize that the function is most likely already written somewhere else in the code. My 50+ lines of code suddenly turned into < 25 , and I realized that I didn’t have to overcomplicate things if I had spent more time searching for what I needed. From that point on, keeping things simple became my number one coding mantra. The more complex the code, the harder it is to make sense of months or even weeks later. Trust me, future you will thank past you for taking the reduce-reuse-recycle approach. Nowadays before I write a single line, I always try to exhaust all search options first. It means my programs stay tidy and I spend my time actually building things instead of tearing my hair out over bugs.

I guess what I’m saying is, if you’re just starting out in coding, don’t be afraid to piggyback off others’ work. Chances are if you’re trying to solve a problem, someone else already has too. Keeping it simple will save you time and stress down the road.

Lastly, in understanding code smells, I strive to avoid writing redundant code. It’s easy to code for long periods of time and not notice overlapping logic sometimes. This highlights the importance of breaks like after coding for an hour, standing up for 15 minutes. Coming back to code the following day provides a new lens as well – it becomes simpler to spot inefficiencies I may have previously glossed over during a marathon coding session. Regular breaks, coupled with reviewing previous work with distance and detachment, can help ferret out flaws that warrant attention. This approach helps maintain both coding quality and personal well-being over the long run. It is crucial to identify flaws now so that our future selves can thank us. As software architect, Joydip Kanilal states, “Code that is particularly smelly can eventually cause profound performance problems and make business-critical applications difficult to maintain.”

Sources:

Cocca, Germán. “How to Write Clean Code – Tips and Best Practices (Full Handbook).” FreeCodeCamp, 15 May 2023, www.freecodecamp.org/news/how-to-write-clean-code/#:~:text=Clean%20code%20is%20a%20term,easy%20to%20read%20and%20follow.

Kanilal, Joydip. “Understanding Code Smells and How Refactoring Can Help.” Tech Target, 11 Mar. 2022, www.techtarget.com/searchsoftwarequality/tip/Understanding-code-smells-and-how-refactoring-can-help#:~:text=Put%20simply%2C%20code%20smells%20are,in%20accordance%20with%20necessary%20standards.

“The Zen of Python.” Python Enhancement Proposals, 19 Aug. 2004, peps.python.org/pep-0020/.