Re:Making Wrong Code Look Wrong (2005)
by jchw on Saturday November 09, 2019 @02:47AM
(#21490245)
Hungarian notation solves one problem: it makes wrong code look wrong.
Type alias can also do that, but they can do so in a way where correctness can be statically enforced.
As an example, let’s say you have a Write function like in the article, but it only takes an HTMLString, which can’t implicitly be casted from String. Then, you can have Encode return an HTMLString, but take a String. Finally, you can use static analysis to only allow whitelisted functions to construct non-literal HTMLStrings (so anyone could do a literal of HTML, but every other usage would require encoding or whitelisting.)
This is a bit rudimentary of an example, but it is widely used in practice.
Type alias can also do that, but they can do so in a way where correctness can be statically enforced.
As an example, let’s say you have a Write function like in the article, but it only takes an HTMLString, which can’t implicitly be casted from String. Then, you can have Encode return an HTMLString, but take a String. Finally, you can use static analysis to only allow whitelisted functions to construct non-literal HTMLStrings (so anyone could do a literal of HTML, but every other usage would require encoding or whitelisting.)
This is a bit rudimentary of an example, but it is widely used in practice.
Here is a safe type used in Closure:
https://google.github.io/closure-library/api/goog.html.SafeH...