Writing robust programs

Some points to keep in mind while programming…

  1. 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.
  2. 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. ๐Ÿ™
  3. Split up methods as much as possible, ideal length of a method will be 30/40 beyond this there is something wrong.
  4. 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.
  5. 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!!
  6. 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!! ๐Ÿ˜›
  7. Initialize variables where they are declared. Don’t be lazy in doing this. Go nuts about following this principle.
  8. 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.
  9. 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!
  10. 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.
  11. 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

One thought on “Writing robust programs

  1. I have a request for clarification about this quote:

    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.

    I think you are hinting at “dependency inversion” ? But the example (to me) seems wrong: I do not understand why “sum” would be more reusable if the two numbers are passed in instead of being made member variables?

Appreciate your comments...