How to write better code with one simple realisation
Published: 2024-02-07 20:58:12

Let's start with something obvious.

There are multiple ways to deliver the same feature. Pick a handful of developers to do the same task, you will probably get at least a couple of different implementations. Your personal style and knowledge are deciding how you write code. If there are no clear guidelines on your team, you are running free on an open field. There are shortcuts to take and pitfalls to avoid, and you decide what To do. In every line you write.

We use frameworks and agree on naming conventions so we at least have a set path of how to do things. But, there is still leeway to inject your personal choices into the code.

You choose what a function does and what is the name of it. You choose to follow the single responsibility rule or to break it based on your judgment. 

> Every single key you type in your keyboard while coding, is a decision made. You decide what path to follow, in every line.
 
Once you realise the responsibility you have on your hands, you can slow down and use it to your advantage.

## Think before you type

Plan your code at a high level, Draw a simple diagram in your notepad, on the back of a napkin, or anywhere. Do it before you start typing. Write your tests, make them pass, and then rebuild your code by revisiting all the choices you made. 

Push yourself to have the mindset of a reviewer. Pretend you are reading someone else's code. Rethink the names and everything else you have typed. Do they make sense?

Useful questions to ask
1. Is it doing what it is supposed to do? 
2. Does it say what it does?
3. Can it be reused?

## Avoid unconscious decisions

There is a domino effect, caused by a propagation of decisions. You do one thing and imply something else. Bear in mind, even if you leave things at "default" it also counts as a choice that has a chain reaction.

Let's imagine you create a class. The class has a series of properties, and without a lot of thinking you make them all public. Maybe just because this is what you see in all other classes of the project. The decision you just made is not affecting just the properties though. It means that your class can be used as a Data Structure and not as an Object. All the users of this class "know" all about the class, and its internal structure is exposed. That is not a bad thing, and maybe it serves the feature. But it has to be an intended quality and not by accident.

Yes, maybe a silly example but it gets the message across. You type one thing that affects something else.

Guard your code from being misused and try to understand deeply what you imply by your implementations. Pay attention to the details. Be aware of the decisions you make.  

## Every little helps 

(for UK readers, yes I was inspired by Tesco's slogan)

We have a lot of power in our hands with these modern languages and tools we use. Freedom is endless and the world of 0s and 1s is our to shape. Accept the responsibility and embrace it.

Pay attention to everything!

Cover image by <a href="https://unsplash.com/@pinewatt?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">pine  watt</a> on <a href="https://unsplash.com/photos/birds-eye-view-photo-of-two-people-standing-on-gray-concrete-road-in-front-of-hill-dUbKcgu0zjw?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>