Tips for writing code efficiently for beginners

Tips for writing code efficiently for beginners

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: image.png

2. Meaningful variable names

Variable names should be meaningful with the context.
Example: image.png

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: image.png

4. Use of ternary operator

Use ternary operator instead of if-else wherever possible.
Example: image.png

5. Eliminate unessential operations

Processing of the same data, again and again, can be avoided by saving the required value in variables.
Example: image.png

6. Avoid declaring unnecessary variables

We should not make variables that are not required.
Example: image.png

7. Use comments

Use comments wherever necessary but don't comment for each line.
Example: image.png

8. Eliminate unnecessary else block

Example: image.png

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.