13 May 2005

Data and syntax errors

Joel has a new rant about Hungarian notation and exceptions. In it, he outlines the principle that you should code in a manner that makes bad code more apparent. He uses an example of passing string values that must have special formatting in some instances and not others (safe string variable, sValue, and unsafe string variable, usValue). As usual, he goes step-by-step to slowly improve a bad situation. While he generally surprises me by going further than I can imagine with techniques to clean up the code, this time he stopped too soon.

He argued that Hungarian notation would alert programmers when they attempt to copy a normal string variable to a location that requires a formatted string variable (usValue = sValue, wrong). But in his frenzy to praise Hungarian notation (offering up yet another retelling of the history of it) and complain about poor operator overloading (hint: it's bad) he failed to provide a real solution to the problem.

Yeah, tagging variables with clues about their appropriate use is nice, but why not just create classes that forbid inappropriate use. Instead of staying in the realm of data error, use your compiler to notify you of errors.

class String {...};
class FormattedString {...};

String value1;
FormattedString value2;

// This won't compile.
value1 = value2;

Tada. Am I missing some other point he was trying to make?

[ posted by sstrader on 13 May 2005 at 2:14:28 PM in Programming ]