Write The Code You Need Today

July 23, 2020

Need: require (something) because it is essential or very important rather than just desirable.

Often times as software developers, we tend to implement features or code we don't currently need. We convince ourselves with a slew of reasons why we should write that code. These reasons may include:

  1. Aha! I will definitely need this piece of logic down the road,
  2. I have to account for all scenarios and edge cases,
  3. Let me just write it now so I won't have to write it again(that's a laugh 😂)
  4. The code don't feel robust enough to me, etc

Though the above reasons(including the ones you are thinking about) are valid and strong, I am here to tell you not to write code you don't need right now. Let me start by unveiling some questions you should ask yourself when implementing any piece of code...

Do I need this? 🤔

It's simple, but yet powerful. This question(if answered sincerely) will keep you focus on writing the least amount of code that is capable of catering for your current implementation. It's like following the TDD development process. In TDD, we write a test and make sure it fails then we right just the right amount of code to make the test pass!

Here is my point: do not write code that you sincerely know you don't need right now. Remember the KISS(Keep it Simple Stupid) principle and let your code be as lean as possible.

Refuse the urge to be too complex. I see this so often with new developers. You just want to show off all the cool new APIs or patterns you have been practicing. The best code is the simple code.

How much do I really know? 🧐

Here is another important question you should ask when implementing a piece of code. Most of the time we think we know a lot of stuff and we only become aware we don't know as much as we think we know when we start digging deep into the implementation we are working on(I am guilty of this myself).

I believe everyone is smarter tomorrow than he/she is today. This is because as we grow we become more sophisticated and knowledgeable on our craft. Have you ever seen a piece of code you wrote some weeks or months back and you go "eww! who wrote that piece of junk"? This is because you have grown and it's completely normal. So my point is - if there is code you can postpone not to write today then by all means leave it to the future wiser and more knowledgeable version of yourself(The future You will thank you for this 😉).

What is the cost of writing this code?

Lastly asking what the cost is for writing a piece of code now as opposed to writing it later is important to ascertain if your implementation is needful or just a mere desire. Don't get me wrong, it is perfectly okay to want to fortify your code. However, do not write desirable code, write needful code - code that is required to satisfy the requirements at hand presently not a future unforeseen requirement you came up with while analyzing the project. Your job as a code author is never done, there is always room for improvement.

Here is a simple rule of thumb - if the cost of writing the code now is greater than writing it sometime in the future, then by all means postpone!

I also want to point out that postponing does not equal procrastinating. In this context, postponing mean putting off code that you won't need today and writing them when you truly need them. I like you to be frugal with your code. This hold tremendous benefits for you as a software developer. Some of these are:

  1. You will meet deadlines faster.
  2. You will write simple and more precise code.
  3. You will be able to prioritize implementations better.
  4. You will put off code you are not totally ready to tackle yet to your future more ready self.

Wrapping up

I hope this concept don't fool you by its simplicity, try applying it and you will begin reaping the above benefits(and more). Write what you need! write what is important! write what is needful! Don't get carried away! Write what is required. In essence - Write the code you need today

As always, you can tweet at me on Twitter your thoughts on this article.

Code expressively 🎨,
Kelvin