3 ms·
I might agree if that type of if statement only appeared at one point in the code, but there're normally several scattered around which all have to be updated w
by aardvark179 14y ago
I might agree if that type of if statement only appeared at one point in the code, but there're normally several scattered around which all have to be updated when obj3 is introduced. Also introducing a class hierarchy often makes it easier to move responsibilities between objects as the code evolves.
For example I recently did some work on a graphics library which had a lot of ifs to do with line and fill styles. By changing those into an inheritance hierarchy I found a couple of places where clauses had been missed and so fixed some bugs, but more importantly when we needed to add new fill styles which were more complicated to draw it became very easy to turn the fill style objects into fillers which had all the responsibility for filling paths, not simply setting the graphics state.
- raverbashing 14y agoYes, there are situations where changing the structure of the code is better than having an 'if' like the example you gave. But this doesn't mean that it's better to completely eliminate ifs