Back in 2018, my interest in Robotics and AI grew when I was pursuing my masters. My interest in programming increased. Before this, I did not have much programming experience as my background was purely mechanical. Most times I even avoided coding as I was afraid to see the red lines. Initially, I was coding mostly in python. Which I taught myself as to advance in the fields I found my passion in. I needed to learn to code to get the robots moving! Most of my codes… Let's just say was — “all over the places”. Although it got the job done of what I wanted to achieve, but it was very rigid. It was specific to that problem and that problem only. To change anything I would have to either write it all up or change the entire logic of the program which involved writing more lines of code. But for me, I did not even know that that was a problem as long as the task executed with no errors or bugs I was satisfied. But little did I know bugs were creeping in all over the places.
I met, Benjamin in late summer of 2019. He showed me the importance of practicing clean code while writing any piece of code even if it was for research purpose. He also introduced me to Uncle Bob and his lecture series on clean codes. Just within the first four lectures of the series I realized how shitty my codes were. For instances:
- Inconsistent naming: I used variables such as wR and wL instead of wheelRight and wheelLeft. Some of these variable if I look now I will take a while to see what they actually mean.
- No code structure: Except for the import statements at the top, my file had classes, functions, global variables all in one file and in no order. A class followed by another class. A function, containing more than 50 lines of code. Global variables declared in between functions, inconsistent spacing and so on.
- No unit tests: Did I say, I was happy that my code did not have bugs? I did not have any tests to even confirm that. My code just for one test case which was the problem I was trying to solve that all I can say without any tests. That too I am not sure if it works cause in two years the problem itself might have changed.
These were just a few of many. It has been almost two years since then, looking sometimes back I wonder if I did not get this guidance I would have been oblivious to all the mistakes I was committing! Having clean codes saves a lot of time in maintaining the code base, specially if the code requires to fly a drone or move a robot as there are multiple classes involved. Most importantly tests help remove the bugs and if a new bug arises all you have to do is write the test and pass it. This saves one from going line by line to catch the bug. Also, with tests it is much easier to get familiar with a code base.
Hope this was interesting to anyone who has just got into coding or someone who wants to advance in their coding skills. The road to being a trained Shinobi is hard but if one does it properly it is quite enjoyable. All you need to do is keep your head up in all the failing tests. :p Next time I will write about some tools which we use here in Delta Engine to help with clean codes. A Shinobi is defined by the katana he uses!