|Written by Mike James|
|Friday, 22 February 2013|
Page 1 of 2
Einstein (who would have made a really great programmer) said
"Everything should be made as simple as possible, but not simpler."
This is an important maxim for the programmer as well as the physicist. Take, for example, the desire to make code as short and neat as possible. This is sometimes a good idea, but generally only when it improves readability and understanding. It is most certainly not a good idea when the change in syntax brings about an unexpected change in the semantics.
at the start of a function. To be neat this is usually converted to:
and if the variables need to be initialized, instead of writing:
is much more compact and it works in exactly the same way.
Often gathering up declarations and initializations is something that is left for a later stage in code polishing and it usually doesn't cause any problems.
During some code polishing, the following code was discovered:
Most programmer would have thought that this was compact and clear enough, but our enthusiastic programmer was being paid by the character saved and there were eight bytes that could be eliminated:
The function even passed its unit tests and the whole thing was voted a great improvement, and yet very soon the problems started.
Code that once worked with the function stopped working.
The question is what is wrong with the change?
What semantics have been modified by this seemingly innocent syntactic change?
How have we violated Einstein's dictum and made a simplification too far?
|Last Updated ( Friday, 22 February 2013 )|