Programming Tips; Think before you code.

Irrespective on whether you are an experienced developer or a new-comer, it’s easy to fall into doing the following.

I want my code to do something so I get it to do it.

Once you do so you move on.

Wait, but.. Was this really the way to get it done? Could someone else understand what the code is doing by glancing at it? Are you sure your code should even be doing that? Why is it working? Does it work with all kinds of possible input? Perhaps the reason why you needed the code to do something was because you were missing something elsewhere.

Now here’s the issue. In a best case scenario, a month later you find out your solution was wrong, now if you didn’t think about the previous things, you will have a hard time trying to fix the issue. Most of the time I see developers end up adding tons of flow conditionals which make the implementation do something completely different than the original one. They don’t remove the previous code though, it’s still there and forever will as they are firm believers that the initial implementation was good because hey, it worked back then so this is just a fix right?. And suddenly this becomes a curse that will haunt any developer that has to work with this code. No one will ever realise that perhaps there was never a problem to solve.

You get the point.

So, how to make sure you do some proper thinking before you code and avoid these situations?

Plan your solution

Whether its in your head, on a napkin or a piece of paper. Know what you need to do and how you plan on doing it.

Verify your assumptions

Are you sure the “hash_t” attribute of the item is it’s unique identifier? What’s with the t at the end? Perhaps it’s the result of a bad interface implementation and really means hashtag. If you can write some tests for this even better.

There is no such thing as disposable code

Unless you are writing a system that will end up in your gallery of unfinished projects (which they could as well be because they become unmanageable to come back to as you didn’t put some thinking before going for it ) or in a toaster, chances are you or someone else will come back to your code, so keep in mind that whatever you are doing won’t go away anytime soon.

Don’t be a slave to history

If you’re unlucky enough to have to work on some code that suffers from the conditions I previously talked about, be open minded. Don’t lock yourself in the thought process which lead to the current code, think outside the code and be ready to refactor as needed.

Proceed with caution

Be aware of what you are doing, continuously question yourself and act immediately over new findings.

Write some tests

Not only will this help you make sure your code is doing what it is intended to but will make your reflect about your solution. If you are reinitialising variable “X” every time in your test cases but the value is really constant, shouldn't this be part of the implementation ? But if it doesn’t really belong to the implementation, shouldn’t we be building this where “X” belongs? Best thing of all? You don’t have to think much about it, if you’re doing something wrong and writing tests for it, you will notice.

While many of the things are quite broad their aim is very specific. To spread thoughts in your head that next time you find yourself writing some code, will make you stop for a minute and think.

P.S. If you enjoyed reading through the post and would like me to write more about software engineering topics, let me know by liking or sharing it.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store