10 ms·
This is describing what I've known as "Typed Hungarian notation", and have seen a few times before, though I can't seem to find now. The original intent of Hun
by Izkata 7y ago
This is describing what I've known as "Typed Hungarian notation", and have seen a few times before, though I can't seem to find now.
The original intent of Hungarian notation was to encode metadata into the variable name - for example, "validData = validate(rawData); showValidData(validData)". The idea was that the prefixes would make it just look wrong to the programmer if someone accidentally used mismatched names, such as "showValidData(rawData)", indicating a likely bug.
This notation mutated into the far more simplistic and arguably less useful "prefix with the datatype", such as iFoo indicating Foo is an integer. This mutation became known as Systems Hungarian, while the original became Apps Hungarian.
The suggestion in this post is taking Apps Hungarian, and instead of relying on variable names, encoding it into the type system itself.
The first time I recall seeing something like this suggested was actually in Java, with examples in that blog post involving class attributes:
public String Name;
public String Address;
And creating classes to represent these values, preventing them from being used the wrong way:
public NameType Name;
public AddressType Address;
...which is why I remember this as "Typed Hungarian" - making the language's type system handle the work that a person would normally have to think about in Apps Hungarian.
- mason55 7y agoAnother good example of this is having separate classes for something like unsafe strings vs. safe strings in a web app. The functions which interact with the outside world accept unsafe strings and emit safe strings to the rest of the application. Then the rest of the application only works with safe strings. Anything that accepts a safe string can make an assumption that it doesn't need to do any validation (or "parsing" in the context of the OP), which lets you centralize validation logic. And since you can't turn an unsafe string into a safe string without sending it through the validator, it prevents unsafe strings from leaking into the rest of the app by accident. This concept can be used for pretty much anything where you are doing data validation or transformation.
- user5994461 7y agoWhat are safe and unsafe strings supposed to mean? All strings seem like normal string to me, a "DELETE * FROM db" is no different from any other string until it's given to a SQL query.
- youerbt 7y agoThen your database API should use "safe strings" only, simple as that.
- kortilla 7y ago“DELETE * from table” is a safe string though for something like file contents or perhaps a comment box on a hacker news site. The term “safe string” is effectively meaningless because it entirely depends on how the internals are going to use it.
- youerbt 7y agoBut of course nobody is talking about universally safe strings. It's just a name to explain the concept. Point being, if my database API uses the different types than my random internet input types, compiler will force me to convert/parse those.
- jodrellblank 7y agoAre you genuinely curious, or are you being a troll? Look at the content of your string, make a decision as to whether you would give it to a SQL engine. If you have not looked, it's presumed unsafe. If you have validated it - parsed it, in the context of this article and this discussion - and decided that you consider it safe, then it is a safe string from that point on. This isn't a philosophical debate about what "safe" means to humans, it's a programming discussion that says if you only want to pass "select * from reports" to your database, check that's what the string contains before you pass it anywhere.
- sedeki 7y agoA string that is supposed to represent a ”name” in a web app context — safe or not? I am referring to potential SQL injections. Surely, no names contain semicolons, but is the business logic-part of your app to determine that names which only contain A-Za-z (or whatever) are safe? It is contextually dependent, meaning in practice as close to the actual SQL query as possible. Or call to file system, where dots are unsafe, and so on. Static typing helps here as described in the article.
- squeaky-clean 7y agoAlso a good way to prevent hashed passwords from being accidentally logged. Class PasswordType(django.db.models.Field): hashed_pw = CharField() def __str__(): # you can even raise an Exception here return '<confidential data>' Not that you should be trying to log this stuff anyways, but unless you're a solo dev you can't prevent other people from creating bugs, but you can mitigate common scenarios.
- agumonkey 7y agoImpossible invalid state typecheckeable
- GuiA 7y agoHere is a blog post applying a similar idea to matrix math: https://www.sebastiansylvan.com/post/matrix_naming_convention/ https://www.sebastiansylvan.com/post/matrix_naming_conventio...
- shmageggy 7y agoI wish there were a type system for numerical computation that could encode things like rank and size of tensors and could statically check them before running. Currently, catching bugs requires running the code which can make for a slow development loop, plus it would make for a much more robust system than documenting these things in comments
- ericcholis 7y agoMy earliest programming experience taught me to use Systems Hungarian with scope. So, something like lcName for local character name.
- jacobsenscott 7y agoOn one of my first jobs all the class names were prefixed with C, but it wasn't carried through to variables or anything. I said we should stop prefixing class names with C and got shouted down. That's where I learned to mostly just do what I want without asking. Once you have 5k lines of something your way it is really too late for anyone to ask you to change it :)
- MuffinFlavored 7y ago> making the language's type system handle the work that a person would normally have to think about in Apps Hungarian. That sounds a lot like "type validation" to me, whereas the title of the article includes "Don't Validate"
- Izkata 7y agoRead all the way down through the "Parsing, not validating, in practice" header. It eventually gets to examples exactly like mine, where "parse" means "transform into a different datatype so it can't be used wrong", while "validate" means "generate a message, but don't preserve that message in the type system in a way that's usable later".
- badfrog 7y ago> That sounds a lot like "type validation" to me, whereas the title of the article includes "Don't Validate" The variable name version does, sure. That's why OP said: > The suggestion in this post is taking Apps Hungarian, and instead of relying on variable names, encoding it into the type system itself. Making it clear that their example is different from what the article suggests.
- radicalbyte 7y agoThe Java example you've provided is known as Domain Modeling, it's well described (and honestly greatly expanded on) in Domain Driven Design by Eric Evans. It's applied heavily in the enterprise world simply because it's so powerful. In general as the domain logic becomes more complex the benefits of doing this increase. Actually, not encountering this style in code-base which is solving a complex problem is a massive warning sign for me. It usually means that concepts are poorly defined and that the logic is scattered randomly all over the code-base. Apps Hungarian is just a logical style: name functions, variables, types so that they're meaningful within your domain. The result of which is code which is very easy to understand for someone who understands the domain. This doesn't mean long names - if you're doing anything math intensive then using the short names which conform to the norms of the field is perfect. For a business process it probably isn't :)
- radicalbyte 7y agoIt's particularly powerful if you can great an algebra of your domain. You can define invariants which are then enforced by the type system. I use this a lot when writing C# code and the whole experience is getting better with every new release. Microsoft keep adding awesome stuff like ADTs which make working with these types a dream.
- james_s_tayler 7y agoDo you have any resources you can recommend which describe how to create an algebra of a domain?
- Quekid5 7y agoI generally like this approach, but it's unfortunately really cumbersome in most mainstream languages like Java (and even Scala 2.x) or C#, but it works beautifully in Haskell because of two features: - non-exported newtypes (also known as opaque types in Scala 3.x); this means your little module is in full control of instantiation of a 'parsed' value from an 'untyped' value. - DerivingVia (not sure if there's an equivalent in Scala 3.x); this makes declaring type class instances equivalent to the 'wrapped' value completely trivial. These two features (and, I guess, the pervasiveness of type classes) in Haskell make this sort of thing as close to zero effort as possible. EDIT: Looking at some sibling posts, I think my C# knowledge may be out of date. Apologies for that.
- endgame 7y agoI think you mean GeneralizedNewtypeDeriving, not DerivingVia. GND lets you pull instances through a newtype, trivially. DerivingVia lets you define your instance in terms of an instance on another newtype.
- runeks 7y agoDerivingVia is a generalization of GeneralizedNewtypeDeriving. The former can do everything the latter can and more. https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#extension-DerivingVia https://downloads.haskell.org/~ghc/latest/docs/html/users_gu...
- Quekid5 7y agoExactly. I intentionally avoid GND because it can be quite wonky, and DerivingVia basically subsumes it. You do still have to have slightly more explicit declarations of instances, but IMO it's worth it for the control.
- lexi-lambda 7y agoWhat you’re describe is in a similar vein to what is described in my blog post, and it’s often absolutely a good idea, but it isn’t quite the same as what the post is about. Something like NameType attaches a semantic label to the data, but it doesn’t actually make any illegal states representable… since there really aren’t any illegal states when it comes to names. (See, for example, https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-... .) In other situations, the approach of using an abstract datatype like this can actually rule out some invalid states, and I allude to that in the penultimate section of the blog post where I talk about using abstract types to “fake” parsers from validation functions. However, even that is still different from the technique focused on in most of the post. To illustrate why, consider an encoding of the NonEmpty type from the blog post in Java using an abstract type: public class NonEmpty<T> { public final ImmutableList<T> list; private NonEmpty(ImmutableList<T> list) { this.list = list; } public static <T> Optional<NonEmpty<T>> fromList(List<T> list) { return list.isEmpty() ? Optional.none() : Optional.of(new NonEmpty<>(ImmutableList.copyOf(list))); } public T head() { return list.get(0); } } In this example, since the constructor is private, a NonEmpty<T> can only be created via the static fromList method. This certainly reduces the surface area for failure, but it doesn’t technically make illegal states unrepresentable, since a mistake in the implementation of the NonEmpty class itself could theoretically lead to its list field containing an empty list. In contrast, the NonEmpty type described in the blog post is “correct by construction”—it genuinely makes the illegal state impossible. A translation of that type into Java syntax would look like this: public class NonEmpty<T> { public final T head; public final ImmutableList<T> tail; public NonEmpty(T head, ImmutableList<T> tail) { this.head = head; this.tail = tail; } public static <T> Optional<NonEmpty<T>> fromList(List<T> list) { return list.isEmpty() ? Optional.none() : Optional.of(new NonEmpty<>(list.get(0), ImmutableList.copyOf(list.subList(1, list.size())))); } } This is a little less compelling than the Haskell version simply because of Java’s pervasive nullability and the fact that List is not an inductive type in Java so you don’t get the exhaustiveness checking, but the basic ideas are still the same. Because NonEmpty<T> is correct by construction, it doesn’t need to be an abstract type—its constructor is public—in order to enforce correctness.
- ronilan 7y agoOT: My favorite movie quote: let lastHungarian = ‘go’ run()
- breck 7y agoI currently use this in my language "Grammar" (https://jtree.treenotation.org/designer/#standard%20grammar https://jtree.treenotation.org/designer/#standard%20grammar). In Grammar you can currently define two things: cellTypes and nodeTypes. Grammar looks at the suffix to determine the type. So if you are building a language with the concept of a person node and an age cell, you'd write "personNode" and "ageCell". "ageCell" might extend "intCell". I've found it a pleasure to use.
- zanny 7y agoRust is extremely good at this with newtype syntax and the ability to apply impls and derives to masking types. Your JSON data might be a string, but having a Json type that guarantees its valid Json is way better. Same with stringified Base64, a constrained numeric value for a tunable, etc. Because using from impls on these types lets the compiler figure out almost all invalid type usages at compile time and give you eloquent feedback on it.
- Ericson2314 7y agoYour comment is wrong is wrong. You are talking about "encapsulation tricks", where you don't make illegal states unrepresentable, but just all the construction so you can audit than declare the unrepresented state won't be constructed. Sometimes this is good, but the benefits are a lot lower, and many an overzealous programmer has taking the "boolean blindness" "string blindness" argument too far with copious newtypes. Actually ruling out invalid states by construction, however, is way more valuable, and far more likely to be worth the work. It requires more thought than just newtyping away, but that's a feature, not a bug.
- Izkata 7y agoSimilar to some other replies, you've jumped to the Java example and made assumptions about it without reading the parts immediately before it.
- DonaldFisk 7y agoThis helps the compiler, but only works for languages with type declarations, and is less helpful to the programmer, as to find a variable's kind (e.g. valid data vs. raw data) you need to look at its declaration, which may involve scrolling. The original idea, described in Joel Spolsky's original article on Apps vs. Systems Hungarian (https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/ https://www.joelonsoftware.com/2005/05/11/making-wrong-code-...) is that the programmer can see, in the expression that the variable is used or the function is called, that the code is wrong. I apply a variant of Apps Hungarian in my own code, e.g. (setf input (inputOfEdge (first (edgesOfSocket (outputOfGate (nth (position visibleInput (inputsOfVertex node)) (ingatesOfEnclosure enclosure))))))) (In the system this is lifted from, node was confirmed to be a Vertex a few lines earlier, and Output is a subclass of Socket.) The general idea is language independent: you could write similar code in Java or C.
- ttty 7y agoNo need to scroll. If you use a modern ide you can just hold Ctrl and hover the variable with your mouse.
- DonaldFisk 7y agoThat still wouldn't work for dynamically typed languages.
- squeaky-clean 7y agoMaybe not something like Lisp but languages such as JS, Python, and Ruby have advanced enough tooling available that would easily identify this.
- marcosdumay 7y agoThat's why the claim that it's an advantage of statically typed languages.
- mcphage 7y agoI believe Joel Spolsky discussed this in an article, having a wart on the name to represent if strings have raw inputs which are at risk of injection attacks.
- hnick 7y agoI wonder why Taint Checking (https://en.wikipedia.org/wiki/Taint_checking https://en.wikipedia.org/wiki/Taint_checking) never caught on in more languages.
- mcphage 7y agoI mean, I’ve got a pretty good idea why it never caught on in English...
- hnick 7y agoIt pre-dates the slang term by quite a while, unless it was around back in 1989? I don't recall seeing that online until the last decade or so.
- jolux 7y agoSeems less useful in languages with strong static type systems?
- mcphage 7y agoIt requires a language with dependent types, like Idris.
- deleted 7y ago[deleted]
- mezzode 7y agohttps://github.com/google/mundane/blob/master/DESIGN.md https://github.com/google/mundane/blob/master/DESIGN.md was where I first learned about how useful opaque types can be for ensuring the type system does the heavy lifting
- goto11 7y agoThe original paper by Simonyi actually does describe prefixing with datatype. E.g. b for byte, ch for character, w for word, sz for pointer to zero-terminated string. He specifically states: The basic idea is to name all quantities by their types (...) the concept of "type" in this context is determined by the set of operations that can be applied to a quantity. (..) Note that the above definition of type (which, incidentally, is suggested by languages such as SIMULA and Smalltalk) is a superset of the more common definition, which takes only the quantity's representation into account So he is comparing to early object oriented languages where presumably Hungarian notation is not needed anymore because the type system of the language itself is rich enough to constrain the set of valid operations on the value/object. So the "metadata" encoded in the Hungarian prefix is exactly what would be called type in a modern language. The idea of Hungarian was valid in a language with a insufficiently expressive type system. I think the term "Typed Hungarian Notation" is really weird. Hungarian Notation is a workaround for a limited type system. "Typed Hungarian" is just... typed code.
- matt_kantor 7y ago> I think the term "Typed Hungarian Notation" is really weird. I'm glad I'm not the only one. "Typed Hungarian notation" reminded me of "horseless carriage". :)
- lmm 7y agoYour comment gets the history hilariously backwards. Actually using the type system has been standard practice in ML-family languages since at least the '70s. Simonyi described Hungarian notation as a way of, effectively, emulating a type system in less-powerful languages; describing Hungarian notation as "prefixing with the type" was not a mistake (as Spolsky claims) but an accurate description of how the technique was intended to be used. The "Systems Hungarian" mistake came about because C programmers misunderstood the term "type" to refer to something far more limited than it does. The technique in the article is not "encoding Apps Hungarian into the type system". It's doing the very thing that inspired ("Apps") Hungarian in the first place!
- papln 7y ago"Typed Hungarian" is just "Typed". The whole point of "Hungarian" notation was to workaround scenarios where the OP is impossible. Your comment got an eyeroll from no less than Michael Feathers: https://twitter.com/mfeathers/status/1192771479332642816 https://twitter.com/mfeathers/status/1192771479332642816