First of all, I should admit, I have been spoiled! I have been spoiled by the IDE features of Visual Studio in C#. And now these days, I am getting to rectify my spoilt habits of coding. I am having to code in C++ these days. Intellisense is not even 10% as good in C++ as it is in C#. This post documents some of the things I have re-learnt over the past few weeks.
1. Write every line of code only after understanding fully the implications of what you are doing. There is no extra intelligent IDE working behind the scenes to redden the areas where you have slipped up. And this becomes all the more important if you have to recompile because of a semicolon or a incorrectly typed variable or a missing header file.
2. Never assume anything after seeing the name of the function. Do not use any function unless you have gone through its code and seen how it is implemented and how it is working. This is also for learning purposes. Usually you learn a lot after seeing the best practices.
3. Uncover your hidden assumptions. Train your mind not to make any assumption what-so-ever. An ancillary point is that you should check for errors after every single line of code. The checking errors part is so hard to do.
4. Always follow the principles of information hiding. Make only as much visible as is important. For instance, if you have a loop like this :
int i;
for(i = 0; i < 100; i++) { }
then this will be much better:
for(int i = 0; i < 100; i++) { }
Since in this i is only visible to the loop. You should follow this principle not only at the loop level, but also at the bigger levels like classes, header files, dlls and basically at every place you are coding.
5. And before I forget, always deallocate the memory you allocate. This becomes really hard and easy to forget if you allocate memory in one function and use it in another. Also, never try to work with null pointers.
This post was meant more for self-documentation and might include a second part as I get to know more and more stuff about coding.
well written post. Coding effectively and efficiently: A thing which all of us are trying hard to learn these days. Even I'm thinking to come out of the shell and write a blog post on the coding skills and standards I have acquired in these 8 weeks. Maybe I'll write one once I'm back to Delhi. Looking forward for the second part of this post...
ReplyDelete