- read

5 Quick Tips And Techniques To Write Better Code

Matt Bentley 36

5 Quick Tips And Techniques To Write Better Code

Matt Bentley
Level Up Coding
Published in
7 min read5 hours ago

--

Some simple tips and techniques to help you write cleaner code today.

Credit: Unsplash

If you are reading this article, the chances are that you write code. Maybe even quite a lot of it... Hopefully writing that code is something that you enjoy and care about. As much as you may (or may not) love coding, as Developers we actually spend about 10x as much of our time reading code compared to writing it.

Reading other people’s code, and unfortunately even our own code, can sometimes be a miserable experience. Incomprehensible, messy, smelly code. We have all seen it. We have all written it…

Without a bit of care, our codebases can quickly become a mess. The cost of owning a mess is high! High cost to our companies. High cost to our mental wellbeing. Rotting code turns our projects into Haunted Graveyards, where even our most experienced Engineers dare not venture. Luckily we can quickly improve our code with a few simple techniques:

  1. Abstract Conditional (if) statements into descriptive methods
  2. Replace Comments with declarative code
  3. Write cleaner functions
  4. Eliminate switch statements using Polymorphism
  5. Start at the end — Think about your destination code and then work out how to get there

I will explain these 5 tips with real code examples. The examples use JavaScript since most people are familiar with it, however they could be applied to any language just as easily. The code for all examples can be found on my GitHub here.

Before we get going, a few words on Clean Code and techniques to help get there.

Clean Code

The ultimate goal here is Clean Code. I see lots of Junior Developers who think that writing Clean Code is a skill that we either just have, or develop over time. In reality, even the most experienced Developers don’t simply churn out clean elegant code all day at first attempt.

Writing good quality code is a process, which generally involves a lot of refactoring. In fact most of the techniques we will go through are refactoring techniques.

Red, Green, Refactor