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?
- Techniques after using Swift for a month posted by sstrader on 26 August 2015 at 11:41:51 PM
- Some thoughts on Ruby after finishing a week (minus one day) of training posted by sstrader on 15 December 2011 at 8:59:30 PM
- Links on the singleton pattern posted by sstrader on 9 December 2011 at 9:19:50 AM
- Phonebot posted by sstrader on 29 October 2011 at 6:37:05 PM
- The labeled break in Java posted by sstrader on 4 September 2011 at 11:27:56 AM