DEV Community

Amanullah Menjli
Amanullah Menjli

Posted on

How to write better code

1- Write comments but only when necessary:

Comments are important but when something is clear there is no need to describe it in a comment.
⭐️ be fast, clear and meaningful.

image.png

2- Unformatted Code:

When your code is very compact it won't be clear and readable even with syntax highlighting , use white spaces to have a better spacing and make your code easier to understand.
⭐️ in python you can use pep8 as guidelines and best practices to write better code.

image.png

3- Use meaningful name:

When naming variables, functions classes or objects using names like a, b c or variable1 is a bad habit because you won't be able to understand why do you have that variable or what is the role of that function and your code will become a big mess and debugging will become impossible.
⭐️ use easy, memorable, meaningful and clear names and you won't be like him 🤦‍♂️👇

image.png

4- KISS (Keep It Simple Stupit):

The KISS principle intends to write code as simple as possible. Avoid complexity and try to make a code that everyone can understand. 👍

image.png

5- DRY (Don't Repeat Yourself):

these days google and stackoverflow are good places to find solutions for our problems but copy-pasting is a bad habit, sometimes solutions you can find aren't for everyone. First understand your problem then understand given solutions and only get the necessary part for you, if you just use it as it's posted on internet, you will make your code longer, slower and harder to understand and maintain and you will become like him 🤦‍♂️👇

image.png

Top comments (0)