Writing robust programs
Some points to keep in mind while programming…
- Write code in a reusable manner. Think that the code I am writing is meant to be reused. Make the method as independent as possible. Independence doesn’t mean you have to make every method independent, it’s just impossible, but instead there are some methods which can be made independent for e.g. if you have a method called “Sum” which adds two numbers, don’t make these two numbers member variables, pass these numbers as parameters to this function. This means “Sum” can be reused by everyone.
- Think that you are writing code that’s meant to be maintained by someone else probably some newbie, so make it as simple as possible. I’ve been a newbie so I know what I am saying, it’s hard to maintain and understand a 4000 line file without even a single green line. 😦
- Split up methods as much as possible, ideal length of a method will be 30/40 beyond this there is something wrong.
- Split up methods logically, I mean not just functions, but by using linebreaks to split logical blocks in a function, looks neat. Makes reading easier on our eyes too. Also when taking printouts we can write additional comments/notes in these spaces.
- Provide ample comments. Follow this religiously. If you have complex statements, mention what that statement does by using comments because everybody doesn’t have time to understand what crap that part of code is doing. Newbies are hapless with such crap code. Hopeless creatures are people who code like this!!
- Don’t bring unnecessary levels of inheritance to force OO programming. OO should follow naturally. I’ve seen people making code complex just because they think they have to throw their weight around by telling everybody they are OO guys!! 😛
- Initialize variables where they are declared. Don’t be lazy in doing this. Go nuts about following this principle.
- If a variable is not meant to be changed declare it as a constant. It makes debugging programs very easy. Personally when reading through code if I see a variable declared as constant I can just pop that variable from my “mind stack” knowing that it won’t change unless const_cast (or c style, which is rare) is used.
- Don’t rewrite TCP/IP to do network programming. It’s already done for you, use it, throw your ego out, hang it upside down, set it to NULL, do whatever just get rid of it. I myself had this problem as a newbie, I believed everybody outside where dumb heads and I am the only one with something up in my tower. This is called as “ego”, get rid of it! It’s dangerous, you will lose plenty!
- Be active in technical forums, or stay in touch with people who have established themselves in this field. Read good books on programming concepts. For e.g. books like “Code complete”. It’s not hard to start programming, but the toughest part is to do it the right way.
- Don’t leave optimization to the compiler! Don’t forget that compiler is just a program which has it’s limitations. It’s just weird that people write crap code and then ask compiler to optimize it for them. 😐 Also keep in mind that everything that we write is not the optimal way of doing things. Do some research on the stuff that you program to find out what’s the best practice. Do not sacrifice readability for optimization. Some optimal methods that I do follow are as follows…
- Pass heavy objects by reference
- Don’t return heavy objects by value, instead make the heavy object come in as a reference parameter. (I don’t leave this to the compiler, I’ll do it myself 😛 )
- Lazy declaration etc