Is 'IF', the next generation GOTO ?! Ganesh sethuraman (2001-02-08) Introduction Using 'IF' effectively in object oriented programming is an art. You haven't felt this? Looks ridiculous ? Trivial ? Many of us must have passed by this stage without actually stamping on it. Nevertheless we need to stream line this concept and enforce it with strong (but simple) theoretical background. Let us strive to change this art to a engineering discipline, this article is in an attempt to do that. Why is it bad?
Where to Use?
Let us define a general philosophy of where a 'IF' to be used and where it should not be. 'IF' is to be used only in the places where the behavior of system under consideration depends on a external entity. What we mean by an external entity here could be a Data Base, user interaction or for that matter any other system. For e.g. consider a case where one wants to set his application's look and feel to Motif or Windows or any other. Here our system should have a mechanism to known what the user(external entity) has set. So Bingo ! use our 'IF' here. Typically a ' if (GIUType.equals("Motif")) ' kind of check has to be done. Use it only once
It is not enough to know where a 'IF' to be used, we should ensure that it is used ONLY
where we need and no where else. Sometimes we make mistakes by inadvertently using a
boolean instance variable for this. We set the boolean variable once and check this in
many methods of that class. Let us take the same example, The user wants a Motif look and
feel(say), so instance variable is set to "Motif". Now for creation of each GUI Component
we need to check this variable. So there will be 'IF' condition to check before creation
of buttons, checkboxes, window ... At the end we land up putting this 'IF' check in so
many places that it is enough for us to get confused.
Way Out
Mere awareness of this potential hazard of using 'IF' cond during development is definitely
going to help.
Conclusion
There should be some language support to streamline the use of 'IF'. It is natural to make good programming practices as a part of the language construct itself, thus making good programming mandatory and thereby not allowing the programmers to commit errors. This is not first time this happened in the past, it has happened with global variables, with GOTO and may be soon with 'IF' as well. |