To write better code, there are several key elements to consider:
The code must be working. It doesn’t matter if it’s a prototype, proof of concept, or even a little buggy. The most important thing is that it solves the problem or meets the expectations you had at the beginning. Without a working code, you have nothing to prove. Therefore, making sure your code works is crucial.
The code must be working correctly. Even if your code works, it’s not enough if it’s not done correctly. Your code needs to function accurately and meet all acceptance criteria. These criteria are usually based on user or customer use cases. Meeting all these criteria must be validated. Ultimately, the goal of any piece of software is for it to work correctly.
The code should be simple. Good code should be easy to read, like a story, not like a puzzle. Complex code is error-prone, so you should do your best to reduce complexity. You may need to refactor some lines of code or solve the problem with less code. The importance of simplicity in code is to ensure it is easy to maintain, debug, and extend. There are several principles that can help you code with simplicity in mind, such as the “Don’t Repeat Yourself” (DRY) principle and the “Keep It Simple Stupid” (KISS) principle.
The code should be optimized. If you plan properly before you code, you shouldn’t have to do much optimization. However, it’s important to measure and benchmark your code performance as early as possible, ideally during the design phase. After running some tests, you may find that you’re not satisfied with the current code performance, and that’s when you can start optimizing, for example by changing existing algorithms or methods. Another way to optimize your code is to refactor or restructure the flow when you need to fix a bug. Some common examples of code optimization include changing algorithms or methods for better performance, refactoring due to changes in design requirements or bug fixes, cleaning up code, and fixing code linting errors. You should also write code documentation wherever necessary.