Coding Practices I have learnt as a developer
Published: 2023-07-04 11:42:38

Like a lot of developers, I ignored the importance of following coding practices when I started out. It seemed "cool" to just do stuff that works. But overtime, I have realised that these practices help improve the quality of code by making it more readable and efficient. Properly structured and documented code is easier to understand, debug, and maintain, even when multiple developers are working on the same project. Additionally, coding practices encourage the use of efficient algorithms and data structures, leading to optimised performance and reduced resource consumption. So here are a few things I have picked up over my time coding.

### Name your variables

The single most important practice is naming your variables properly, or at least in a way that makes sense to your code. Check out the code below:

![wrong variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96e7eogx1bsw2xd28irf.jpg)

The block of code works, but imagine seeing this somewhere in a large codebase and staring at it for very long time. Yes, it would make no sense when you come back. Instead using a readable variable name would solve so much.

![correct variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssvkh83xwhyw0h6zt5eq.jpg)
The above change makes the code so much more readable. The naming convention can be decided, but follow that consistently

### Structure your code

In the above example, we have three different variables which can be thought of properties of one single object, in this case probably invoice. It is highly possible that same structure can be reused later in the code. It is better to make a structure to contain this data.


![struct using dict](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vwx40htzozwr9uydru2.jpg)
Using a dictionary to make an object makes it more readable and efficient than creating same variables over and over. A better way to do this is using Class.

![STRUCT USING CLASS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1ygqaoak5hrn8gtrlc8.jpg)

However, avoid overuse of this as it makes the code bulky. 

### Comment your code

Comments are very important. When dealing with large codebases, they become a necessity to understand the underlying architecture. These become very helpful in refactoring code later on and understanding logic. Adding documentation comments in functions increase readability of code.

![code comments](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdpfy2m1i51njyvitczf.jpg)

### Code Review
Its important to take suggestions and feedback from peers to maintain the quality of code. Code review also helps adding things one might missed during implementation. Using collaboration, code can also be optimised.

### Test your code
This is something that I have recently learnt the importance of. Testing your code against all possible edge cases is important. This ensures the correctness of functionality being implemented against all scenarios. Plus, writing tests means other developers get the opportunity to check their code as per the standard required, so it's a win-win. For an example, check out [this](https://dev.to/ag2byte/series/23412) series. 


What are some practices you use? Let me know in comments.