Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Is riding a horse a good way of travelling to another country? You probably say a big NO to this question because you are thinking about all the other easier more efficient way you can do it like using your own car, flying, etc. But hundreds of years ago when none of the other options was available, in fact riding a horse was a very good option!
We can not say something is good or bad unless we compare it to its alternatives. The same goes for coding. We don’t have good code and we don’t have bad code, good and bad only comes when we do the comparison!
isActive is a very good variable name to use instead of flag1 but comparing it to isUserActive it’s a bad one! In another example, a code that solves a problem in 5 lines of code while the same can be done in one simple line is bad code and a code that uses ten thousand lines of crazy stuff in it but is the best way you can come up with is a good one!
Why this is important?
As developers, we use to apply certain solutions, principles, architectures, design patterns, frameworks, libraries and all the other fancy stuff over and over again, without thinking that they do not really matter! What matters is the difference they can make to your code. Usually, they are helpful and the version of your code after applying principle X is better than the version without principle X but sometimes it’s not!
So blindly applying principle X that could be anything you ever heard about good coding is a big mistake. A mistake that I’ve personally made a lot. You can only be sure when you do the compression and decide base on your values!
We have 2, 3 or n solutions, they could be in our head, on a piece of paper or already coded, How do we decide which one to use? In my opinion, answer to this question is what determines how well your code going to be! And the thing is there is no right answer to it.
Everybody has different values and compare things differently, One may prefer the code that’s a bit more complex but has better performance, or writes some extra lines of code to keep it consistent with other parts while someone else may do the opposite. I’m not here to tell you what your values should be? Or how you should compare different solutions, I’m just saying that you should be fully aware of your values. So you can use them instantly to compare solutions hundreds of times a day!
For me, Simplicity and Maintainability are two core values that I always follow. So when I write some code I try to come up with as many solutions or alternatives that I can and then choose the one that’s the simplest and makes editing my code in the future easier. Of course, I care about other stuff as well but these two have the highest value for me. So know your values and stick with them!
How do I come up with more alternatives?
So alternatives are good and they are the allow us to write good code. How can we come with more alternatives or more solutions? Here is what I usually do.
- Asking a simple question of Can I do something else instead? Before implementing any feature I always ask this question and the result is surprising! So instead of going through with the first thing that comes to your head think more. Usually, better solutions are waiting there. When I start a new task I wait for around 2–3 mins thinking it thoroughly before putting my fingers on the keyboard.
- Learning! Nothing like learning gives you insight and broaden your mind. I remember a time that a friend of mine asked me to review a piece of code, when I looked at it I instantly realized applying strategy design pattern would make the code much simpler. That was because I knew about design patterns and I practised them already so I was able to come up with the solution. Knowing more and gaining more experience allows you to come up with more alternatives to choose from.
- Teamwork and code review! No matter how good you are, you’re not perfect, none of us is perfect. So being in touch with others and seeing how they solve different problems really gives you some good insight.
It’s not you, it’s your code!
We each grow up in a different country, different culture, worked with different technologies, programming languages, etc. and because of that, we all have a bias toward certain things and the way we code is not an exception.
Sometimes it’s not that easy to abandon what you already love and are familiar with in favour of something else. But the willingness to question your own though and believes and everything you stand for is the key to gain real insights and lets you do things better. Remember your code is not who you are, you are bigger than that. So let it be seen, judged, improved and throwaway if necessary.
Conclusion
- There are no such things as good code or bad code, good or bad is only meaningful when we compare things together.
- To effectively compare your solutions you need to be aware of your values and what you stand for.
- You can make your code better by constantly coming with more alternatives and choosing the best one.
Good code is all about alternatives was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.