Here are given some basic steps which every programmer should follow to code efficiently
1. Create functions
Never write all the code in one function only. Make functions performing short tasks so as to increase readability. Writing code in different functions also makes us easy to write code.
Example:
2. Meaningful variable names
Variable names should be meaningful with the context.
Example:
3. Proper indentation
Do not use brackets for one-line conditions in if-else, for, or while loops. Make proper indentation throughout the code.
Example:
4. Use of ternary operator
Use ternary operator instead of if-else wherever possible.
Example:
5. Eliminate unessential operations
Processing of the same data, again and again, can be avoided by saving the required value in variables.
Example:
6. Avoid declaring unnecessary variables
We should not make variables that are not required.
Example:
7. Use comments
Use comments wherever necessary but don't comment for each line.
Example:
8. Eliminate unnecessary else block
Example:
9. Avoid global variables
Do not always use global variables when not required instead pass such values to functions. Make variable global only in the case when it is required.
10. Use print statements for debugging
Use print statements where you feel your code is not running and check whether that is printing or not. If it's not printing you get the reason that it doesn't reach there anytime and hence the problem is somewhere else and you can look at it
11. Think of time complexity
Always think of the time complexity of a code it takes to run and think whether this is the best approach or we can improve on time. If you can reduce the time complexity of the problem update code to it.
12. Keep practicing
At last, just keep practicing by doing more and more problems. Just keep coding, you will definitely learn many things through experience that no one will ever teach you.
The end
Thanks for reading.
If you liked this make sure to give a follow on hashnode.