your next function should look something like this
Like any other art, writing code has got it significance. every one writes code and irony is kids of age 6 writes code and now he is my competition. Kid of age equal to my experience becoming a competition, Hmm… so writing the code alone may not work anymore. It is all about how you write and show the maturity in the code. So lemme share my knowledge on how you can improve the art of writing and make the next developer love the way you have coded.
Compare the code you write with your body.
yes, just compare the code you write with the parts of human body and watch what magic it does. But how do I compare the code to a human body? Wait I will brief you through.
In a human body each part has its own significance, it has nervous system, digestive system and many, in tern each of the part has its own functionality. like wise segregate the code into functionalities. Each functionality is to be limited to its function itself, no overdoing or doing less. Like nervous system has not got any interference with the digestive system(I am not a biology student). Let me get in further detail.
we can take an example on how to write code while comparing it with the digestive system. So we eat food and it gets digested. Though it is not as simple as said and it involves many other operations, our code should also be written as such. And, there are people who make the digestive system looks like this.
Eat() {
digest();
}
or
Eat() {
100 X lines in between
digest();
100 X lines in between
}
I see many people doing this. In fact I was one among them once. you only feel pain your ass once you started working on someone else code and they have done the same thing as you have done to your previous developer. So let us not do this or at least improve on this. jokes apart, how to write a proper code of the same functionality.
we start from the eat method first.
if we just mention eat, what do you expect to eat? grass? so specify what food it(human) can in take. This is where you should add a argument to it. Eat(food). That is your very step, now you have communicated that you eat some thing and the something is the food you pass it to it. okay but what food it can accept? yeah add an interface to it so that you can pass the food you can eat and it is up to your choice on adding the optional / required keys in the interface. so by now your code should look like this.
eat(food: IndianStyle) { }
now, the food you can eat is ready served hot and delicious. so you take your food and pass it to this method. From now don't mess up the thing before the food gets digested. So let us dive in to the digestion.
once you have eaten the food, i.e. you have called the method with the food argument, now it is up to you how much details you are going to add in the code, for example you can add chew method, for proper chewing the food, followed by adding sufficient water inside the stomach to make the food ready for the digestion. To show how it looks here is the code
eat(food: IndianStyle) {
chewedFood = getChewedFood(food);
readyForDigestion = getFoodReadyForDigestion(chewedFood);
}
every detail you add in the code is to be handled in such a way that it looks like flow from one process to the other, do not put every individual process in the same eat method. like the police cannot do all the tasks from catching the suspect to putting him behind the bars.
we are in the final stage of the method eat, so we have the food which is ready for the digestion and we return the ready for digestion food that can be sent to the intestines for further processing.
so we need to add a return type to the code stating, hey my job is done and here is the food ready for further digestion and here is how it looks take it. At this point of time we are almost done with the eat method here is how it looks.
eat(food: IndianStyle): readyForDigestion {
chewedFood = getChewedFood(food);
readyForDigestion = getFoodReadyForDigestion(chewedFood);return
return readyForDigestion
}
Conclusion:
To conclude of what is exactly to be done, write methods/ functions that speak to you, if you read the code we just discussed, the eat method says hey give me the food of type Indian style and I will take care of the processing and in return I will give you the food which can be given to the intestinal department for the absorption of the energy. Nothing more or nothing less, incase if you provide me with the Chinese food I am not going to accept it. Thank you. This also makes the other systems know, okay ! eat method doesn't care about anything just give the food it accepts and it returns me the food which is ready for digestion.
There are many advantages if you write code this way, you can have a proper error handling and many more and this whole thing will be covered in the next blog.
Thanks for reading
happy code coding my friend.. :D