How To Write a Clean Code
Hey Everyone!!
#prints hello world
print(“Hello World”)
You might be thinking why I have written this print(“Hello World”), so basically if you don’t know what it means ,this is a python code of line which prints Hello World on the terminals. SO if you see the line it is so simple and you can easily guess it’s functionality and I have also added the comment line that tells it functionality. So you all were wondering that we can easily understand the functionality of print function and it’s use cases so what is the need of adding comments, this is a 1 line code but when we create a software or any application then the code become bigger and for handling these codes we use these things, there are also some new coders who initially started development they usually get confused between the codes so for their feasibility and for your understanding you should keep these things in your mind when you start coding.
So there are two types of programming i.e. Programming and Good Programming you all are a doing programming. Most of you have created your own programs as well, but if I ask you to work on my code and fix this code for me to give the desire output, you would be thinking that ‘yeah I can do this’. But if you open up my code and you see a long functions and they are defined in a single lines and I have also not given the information of that functions in comment line so it would be tough for you to understand. Here comes the importance of the clean code and the cleaner is the code the better coder/programmer you become. Now you all are thinking that I will add a comment and I will become a good programmer but it’s not true yeah the code become easier to handle if you add comments but there are also many things which help your code to look more cleaner more maintainable, so let’s look into it.
What things we should focus to write a clean code
Code is like riding a bike on a road.
1. “If it isn’t tested, it’s broken”
So when you create a code you should test the code, you would be thinking why it is important it is important because if you test the code you most of the time you would get a bug so write lots of tests, especially unit tests, or you’ll regret it.
2. Choose meaningful names
You may heard this that while creating a variable or classes or dictionaries you should choose a meaningful names, why everyone says that they say this because if you are not good in naming things in your code then it will become an unmaintainable nightmare. That means the other developers or the other team member wouldn’t get a idea how your code works and if someone wants to maintain your code then it would be difficult for them to maintain your code
3. Classes and functions should be small
What does “small” mean for functions? No more than 4 lines of code. Yep, you read that correctly, 4 lines. It seems somewhat arbitrary and small and you’ve probably never written code like that which is small and show your desire output. However, 4-line functions force you to think hard and pick a lot of really good names for sub-functions that make your code self-documenting and more maintainable. Additionally, they mean you can’t use nested IF statements that force you to do mental gymnastics to understand all the code paths and that’s how you should think while creating a function.
4. Functions should have no side effects
Side effects are truly evil and make it extremely difficult to create code without bugs like infinite loops, bad variable names etc. It happens when you don’t run your code or test your code that’s why the 1st point plays an important “If it isn’t tested, it’s broken”. Many times it happens that the function run into the infinite loops this is the side effect of a function
5. Comments
You all know what is comments, yeah I am talking about programming comments not the insta comment hahaha I got you. So back to the topic we use comment to explain the functionality of the line or the functions or the variable etc. It helps the coders to understand the code more easily.
In summary, make sure to remember and apply these four “clean code” principles to dramatically improve your Code productivity:
- “If it isn’t tested, it’s broken”
2. Choose meaningful names
3. Classes and functions should be small
4. Functions should have no side effects
5. Comments
I hope I have given you a brief explanation of how to write a clean code and what things you should remember while writing a code you all are a programmers and you should start applying these functionalities in your code to look more beautiful and maintainable.
- Prakhar Singh