Sitemap
Better Programming

Advice for programmers.

The Real Reason it’s Difficult to Write Clean Code

5 min readOct 30, 2019

--

Photo by Sarah Dorweiler on Unsplash

The real reason it’s difficult to write clean code has little to do with technical challenges. Instead, the difficulty comes from trying to maintain a high standard of code quality while dealing with other aspects of our lives.

What is Clean Code?

For those of you that are unfamiliar with clean code, it’s a term that describes the act of making code as readable as possible. To understand its importance, you only have to remember a time when you’ve looked at your own code, or someone else’s, and struggled to understand what it’s doing.

I won’t go into detail on how to write clean code in this article. Instead, look at resources like the book Clean Code: A Handbook of Agile Software Craftsmanship. However, what I will say is that writing clean code comes down to respecting and being thoughtful towards anyone who may have to read the code you produce. This includes yourself.

The Cost of Not Writing Clean Code

Not writing clean code can be disastrous, but it’s not difficult. It’s a lot easier than a lot of other aspects of programming. Instead, the main problem with writing clean code is not being able to do it consistently, regardless of what is happening around us.

Anything in our life can affect our productivity at work and thus our ability to write clean code. Many of these problems like family problems or health issues have no quick fix but there are some issues we do have control over.

Not knowing how to write clean code

The obvious first reason for not writing clean code is simply not knowing how to write clean code. Throughout my education, I was never made aware of the term clean code nor did anyone seem to care whether or not the code I was writing was readable. It often wasn’t.

At university the focus was on learning languages and solving problems. This is mostly good, but in a professional environment a developer needs to do more than solve problems. Software is not written to be thrown away like a student project. Instead developers need to be constantly improving the code, adding new features, and squashing bugs. That’s a lot harder to do if the code is poorly written.

Therefore, if like me you were not taught anything about writing clean code at university then the responsibility to teach yourselves is in your own hands. It’s quite possible that you already write good readable code, but it doesn’t hurt to learn more.

Not having enough time

One of the number one reasons for not producing high-quality, readable code is not having enough time to do so. When you’re feeling the pressure, either internally or you have teammates and managers breathing down your neck to get something done, you are trying to do just that — get something done. How you get it done is usually less important, so long as it works and it’s in on time. The exact reasons for not having enough time can vary but I’d argue the two main reasons are:

  1. Underestimating the time a task will take to complete
  2. Management gave you too much work to do within a specified period

I would argue that for most people a lack of time due to tight deadlines is the likely cause. Without supportive management that can be difficult to change. However, reason one is more in our control.

As a developer, you might let ego get in the way, or you might just not have enough experience to give an accurate time estimate. Either way, you want to show you are capable and this usually means not giving longer times than you’d expect your boss to hear. In reality, all that gives you is more stress and pressure to get things finished and that won’t help you write clean code.

I’d also say that a lack of organisation and efficient planning can lead to poor code quality, but I trust that most programmers don’t leave things to the last minute.

Not prioritising refactoring

Another problem is that it’s easy to push towards implementing a feature in time for a deadline, only to move straight onto the next task without ever going back and spending a bit of time to neaten up the code you’ve just created. Your boss might not thank you for this nor understand why you’re refactoring code instead of adding new features, but you and your fellow developers will appreciate the effort.

Not understanding the problem

Finally, not understanding what you’re doing is a sure-fire way to reduce the readability of your code. It’s not uncommon to see people start to code up the solution to a problem, without actually knowing how to solve the problem.

This can lead to a lot of problems when combined with other pressures like a tight deadline. The result is a frantic scramble to hack together bits of your pre-existing knowledge with whatever you find on the internet. With no clear understanding, simple things like naming variables or methods intuitively and following coding conventions go out the window.

Instead, make sure you have a clear understanding of the problem, a plan to implement a solution, and a calm mind to stop you panicking or getting frustrated when issues arise.

The Real Reason it’s Difficult: We Might Not Always be able to Write Clean Code

The real reason it’s difficult to write clean code is that it isn’t always possible to write clean code. In an ideal world, where deadlines aren’t so tight, we always know what we are doing, and our lives are always going so well — then, maybe.

But in reality, there will always be times when our code isn’t as good as it could be. I believe the best thing we can do is to practice clean code so that when something impedes our ability to write code, the results we produce are still a lot better than when we didn’t have any clue about how to make our code readable.

--

--

Drew Coleman
Drew Coleman

Written by Drew Coleman

Game developer writing articles about: game development and programming.