I have been reading the Code Complete 2nd Edition by Steve McConnell. I read the first edition and thought it had been a while and worth reading again.
I thoroughly recommend the book. However this is not going to be a book review. Instead as I read I thought is this germane to device development. For example, he recommends that you write a function for complicated logic so instead of
if ((a = b) && (b = c))
you are better writing
if (complexLogic())
This is very good advice but is his book valid for device development. I would say maybe.
You have the usual dance between requirements, performance, maintenance, and security. However, there are difference in performance.
- The Compact Framework has an aggressive stance on memory. With .net applications code and data are one and the same. So the Compact Framework will pitch code on a per method basis to free memory unlike the full framework.
- Virtual methods are slower than instance or static method calls. Unlike its big cousin virtual methods are not kept in a pointer table. Rather they are calculated as to where they are and cached. In low memory conditions the cache can be cleared and the program has to recalculate where the method resides.
- In addition, each instruction consumes power. However when the CPU is idle it is truly idle and does not consume power. Thus the amount of code impacts not only performance but power consumption. A badly written appliction can drain the batteries very rapidly.
If you did follow Steven's advice, your program could potentially end up performing worse. True it might be higher quality code but slow is still slow.
What are some key take aways:
- Use virtual methods sparingly
- Don't have methods just to have more readable code - more code in a method is better
- Keep the number of classes small
- Don't generalize your program unless it is the only way to solve a problem
- Program defensively however use #if debug liberally to have programmatic defenses be removed if not needed in release builds
That brings me to the last items, test, test and test. Having unit tests is invaluable for making sure that changes made to the code does not break existing code. And when you go to using the Remote Performance Monitor to tune performance it becomes more important.
The very last item is use your head. Try things out but do not assume what works on the desktop or server will work on a device. Try it but then test and see if it does work better. The best methodology is using your brain and looking at your unique situation. As Steven says "Design is heuristic. Dogmatic adherence to any single methodology hurts creativitiy and hurts your programs."
Comments