After a couple of philosophy articles, I am back to my usual self writing about technology and related rants. This time, I will write about a heated discussion that happened between me and my fellow colleague. And that was on 2 points. So let the rant begin. Now before I begin, let me write that these days I am doing a lot of coding in C. So assume C in the below discussions.
1. Why use 0 and 1 instead of an enum?
This probably is a very simple question to answer. If using binary values in C, one can always define enums corresponding to all 0 and 1 values that are being used but is it worth it? Let me explain what happens on the compiler level. The compiler will simply replace the enums so defined by the actual values (especially for binary) when used on the LHS. But if it is used in assigning variables (on RHS), it adds two more instruction in the assembly code to fetch the enum value from the memory location holding the enum definition and assigning it to LHS. Now on the big resourceful systems we have today, it wouldn’t matter but in the embedded world that I live with, it does matter a lot. All process instructions and memory usage should be reduced as much as possible. And as long as variable value usage is properly documented it does not matter. eg: iCount is giving as much information as integer_Count_For_A_Loop. I mean how far should we go for making code presumably readable? An optimizing compiler would definitely omit those 2 instructions and make sure the assignments will be done in the compiled code itself but then, aren’t we depending too much on things we probably don’t know will happen or not? My final say on such an issue is to make sure that the code is commented and try to reduce unnecessary layers of code instead of writing 0 or 1. As an aside, ever wondered by bool is not defined in C? 😉
2. Why club multiple case statements?
Again, I would like to re-use every part of code that I write. So if there is a case where the re-use is happening even for a couple of C statements, I would try to re-use it. Clubbing multiple cases or having an intentional case fall through is a programming discipline and should not be despised. If the statements in multiple cases are repeated why not have an intentional fall through instead of repeating stuff? Of course you have to add an if somewhere in the code to get some variable properly done but hey, than it is code re-use.
So it was a clash of disciplines and I don’t have any opinion on which one is better. As long as the code is readable, maintainable, bug-free, extensible and scalable plus consumes less cpu cycles and memory and is performance oriented, it is okay to follow whatever you choose to follow. So as someone said, your programming discipline to you as long as it does not cause code wars! 😉
Rant out…