| > I think that you misunderstand the purpose (and utility) of type systems, but I'm not sure what to suggest to you so that you can understand. I enjoy writing code in typed languages. I am in full agreement that they have purpose and utility. > No, type coercions are bad engineering. The opposite is true. Your job as a software engineer is to coerce types. I don't mean calling `float64(unsafeString)`. That's certainly type coercion but, as you said, its brittle and probably not the best design. Consider a database. It stores its data on disk as something (ignore implementation). It loads that data into memory where it is coerced to a type (probably in the C-language). Its filtered, grouped, whatever else. Serialized to something and sent over the wire. Your Python server receives that something and coerces it to a Python type. That Python type is serialized to something and sent over the wire. Your Javascript front-end loads the data and represents it as a Javascript type. Nothing about this process is bad or ugly or brittle. It would be infinitely worse to maintain the something type across all these different boundaries. Imagine pickling types whenever we want to save something to disk or share it over the network. Imagine being forced to write application code in the same language as your database! > Type coercions at the interface level are a hack that is not acceptable for anything beyond hobbyist work. Representing a value in the database as an enum and as its string name in the interchange format is not hobbyist work. I'm certain if you have worked professionally you've used enums and not marshaled their underlying value. > If the server is changed, the client only needs to change if the interface between the two also changes. You are advocating for type coercion. The server and client store internal representations and must marshal an interface type to communicate. That's a form of type coercion (I should say "casting" since its explicit but I've mistakenly used coercion so far so we'll roll with it). --- I think you're too tunnel visioned on the types argument. Types are great. I love them. But coupling your database to your server to your client just to share types between the three is obviously not a good idea. There are greater concerns at play. Extend the argument all the way. Separate your client from your server. |
> I enjoy writing code in typed languages. I am in full agreement that they have purpose and utility.
My apologies, that was a bit uncharitable of me.
And, you're right, I didn't quite understand the meaning of "coercion" - I misinterpreted it to mean "implicit coercion". So, my intention was to say that "implicit type coercion is bad engineering" - you're correct in that in general, it's not bad, and necessary.
However, you still don't understand my point, which is that adding types don't introduce any more coupling than what actually had to exist in order to make the program run in the first place.
Let's ignore implicit type coercions, because they are bad engineering, and not relevant to the following point. In a language without implicit type coercions, programs must be well-typed to run correctly. In dynamic languages, even though the code might not contain type annotations, the above principle must still hold. If you pass a string to a function expecting an integer, then your code will break, and all that dynamic typing does is push the error from compile-time to whenever in run-time that you hit that code path.
This is equally true for a client-server interface. If your client and server do not agree on their interface, which includes types (alongside packet header, and field length and order), they will break. If you do not explicitly state what those types are in some formal spec, the types still have to line up in the program! The types are still there, even if you don't explicitly state or check them.
> It would be infinitely worse to maintain the something type across all these different boundaries.
This isn't true; you already maintain types between the boundaries formed between functions in a single program, there are far more of those than inter-program boundaries, and after you set up the tooling for checking network communication schemas, it's only a little more work than maintaining "normal" types between functions.
> Imagine pickling types whenever we want to save something to disk or share it over the network.
I don't understand - "pickling types" is redundant because the pickle format already includes types automatically. Unless you mean building a schema for the pickle and machine-checking that the loading code and the saving code match? In which case, if you're in an engineering context, I absolutely advocate for this, because a mismatch means a runtime error at best, and data loss or corruption at worst. The argument for doing so in a client-server setup is even stronger, however, because data saving and loading code are colocated with each other (making errors easier to manually spot) far more often than sending and receiving code in a client-server codebase.
> Imagine being forced to write application code in the same language as your database!
There's absolutely nothing about type schemas that forces you to use a single language - most schemas are language-independent, like JSON Schema[1] (which is a schema (including types) for JSON data that is in JSON, so you already have a parser for it, and you write it once and then check it everywhere) and ASN-1.
> coupling your database to your server to your client just to share types between the three is obviously not a good idea
The point is that there's no more coupling than what exists in the first place. Programs must be well-typed to run correctly (if there's implicit type coercion, which is bad practice, the program must still be well-typed - you just now automatically insert type-level logic for the coercions) - that means the types are there, even if you don't write them, so the coupling is also there, even if you don't want it.
If your database sends a string to your server when it was expecting an integer, then either your program will crash, or there's implicit coercion - bad practice, but then either the program is well-typed, in which case it's actually technically expecting either a string or an integer (with runtime detection - also bad practice), or the program is not well-typed, in which case you'll get a runtime error.
[1] https://json-schema.org/