4 ms·
Hungarian notation is not a workaround, but a medium for expressing semantics about something that cannot be done with the type system. It is literally by defin
by gfodor 13y ago
Hungarian notation is not a workaround, but a medium for expressing semantics about something that cannot be done with the type system. It is literally by definition this. If you think about it, this is the point of naming variables in the first place. No matter how advanced your type system is, there will be semantic information that it does not or cannot encode. Hungarian serves to systematize the definition and use of this semantic information. Hungarian is intended to maximally leverage the semantic bits of information (in a literal sense) we can put into names to the fullest extent possible. You're already making this tradeoff when you name something, it's unavoidable, and Hungarian is an attempt to find a local optima that falls out of specific assumptions around the purpose of names.
For example, in a simple discussion board app, you may have a string of text in memory that has the following attributes:
- It is null terminated
- It is ASCII encoded
- It contains properly formatted HTML
- It has been sanitized against script injection attacks
- It was received by an end user, not rendered from a template
- It is one in a specific series of strings being processed by a overarching process
- It contains content that is intended to be injected onto a page
- It has been matched to contains swear words that need to be censored
- It is longer than a certain number of characters and hence needs to be stored in a text field, not a varchar
- It potentially contains copyright infringing text
I could go on and on. The point is not to get hung up on specific examples, but to note that there is a ton of semantic information about a piece of data, any of which may be a) relevant to the user and b) irrelevant to the goal or capabilities of the compiler's type system. In many of the cases above, one has a hard time imagining what exactly a type system would even do with this information, since it's fuzzy and not really something that should be used to strongly enforce contracts.
This is where the system of Hungarian comes in: it forces you to build a taxonomy of this semantic information so you have a way to leverage it and reduce the amount of entropy a system can have in variable names.
The bottom line is you are going to name a variable something. So what do you name it? If you think about it logically, it makes sense to name it in a way that the most semantic information that is the most relevant is conveyed to the user. English is poor at being efficient and packing in dense information, but it is comfortable and ramp-up time is near zero. Hungarian optimizes this differently, and trades ramp-up time and comfort for efficiency, precision, and density. It is really the vim of variable naming conventions, and like vim, it is not for everyone.
- quanticle 13y agoI could go on and on. The point is not to get hung up on specific examples, but to note that there is a ton of semantic information about a piece of data, any of which may be a) relevant to the user and b) irrelevant to the goal or capabilities of the compiler's type system. But doesn't that just mean you have a bad type system? I mean, the entire point of a type system is that it automatically checks these sorts of constraints for you. Otherwise, why have types at all? Why not just use chunks of bits?
- gfodor 13y agoYou can flip it around and ask why have names at all? What is the point of a name? Why not just have randomString() choose your names? The name is an important dimension that is provided by a programming language to describe some piece of information. Hungarian is a way of leveraging this dimension, which is separate from the type system, that is optimized for efficiency at the cost of intuitivity. Also, many of the examples I gave above have little relevance to enforcing constraints on the use of data, which is the job of the type system, but are still relevant to the programmer in helping them understand and properly work on the code.
- noblethrasher 13y agoAmong the things that names/identifiers do: (1) They act as indexes or discriminators. (2) They describe. (3) They prescribe. (1) will be useful for at least as long as we program in a textual medium. (2) is also useful, but type systems make it ancillary. (3) is dangerous since it's merely an ought statement that can easily get divorced from reality; decent type systems obviate the need for that role.
- gfodor 13y agoYou're completely ignoring the role and importance of notation. Type systems do not take on the role of efficiently conveying information to a reader visually through symbology, which is part of the role of notation. Types in a type system have names, after all, and the choice of those names is also one of notation. Good notation comes largely in part from good names and has little to do with type systems. Hungarian is an exercise in notation, hence the name.