To write a better code, you need;
First thing in coding that you need is a working code. It does not matter if your code is just experimental, proof of concept or prototype. It does not matter if the code is ugly or a little buggy. The only thing that matters now is that it works, able to solve the problem or meets the expectation that you thought at the beginning. Without the working code, it does not prove you anything on the you have done. Making the code works first is crucial here.
Working correctly. Correctness of the code.
Working code is not enough if it is done incorrectly. The working code needs to function accurately. It needs to meet all the acceptance criteria. These acceptance criteria usually are written based on the use cases from your users (or customers). The same feature that may be used in different ways. Meeting all of these acceptance criteria must be validated. Correctness is the destination of any piece of software (ultimately the goal of any piece of software is to work).
Simplicity in code
A good code should be read like a story, not like a puzzle.
Complex code is error-prone. If your code complexity is high, you should do your best to reduce or minimize that complexity. You may need to refactor some lines of code. If the problem can be solved with less code, it would be better. The importance of simplicity in code is to ensure the code is easier to be maintained, debugged and extended (e.g. API compatibility). There are few coding principles that are designed to help you code with simplicity mind:
- Writing self-explainable code would be the best documentation.
- Handling code duplication with Rule of 3: Write once, copy twice and refactor after 3.
If you plan properly before you code, there is nothing much you can do here. In term of code performance, you should be measuring or benchmarking your code performance as earlier as possible, probably during the design time. Sometimes, better understanding comes after you learned something. In this case, after running some tests, you may found something that you are not satisfied with the current code performance, then this is where you shall start the optimization works, e.g. changing existing algorithm or method. Another way that you may do the code optimization here is that when there is a bug that required you to change some designs or methods in order to resolve that bug. This is where you may change your code, refactor or restructure the flow.
Here are a few examples on what people mostly do at this stage:
- Change code algorithm/method for better performance.
- Refactor some code due to little change in design requirements or bug fixing.
- Clean up code or fix code linting errors.
- Writing code documentation wherever it’s necessary.