DEV Community

Abhigyan Gautam
Abhigyan Gautam

Posted on

Coding Practices I have learnt as a developer

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

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
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
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

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

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 series.

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

Top comments (1)

Collapse
 
ap45 profile image
Arpit Parekh

this is good. i will make sure to not to make "coding" mistakes after reading this.