News for hackers. Stuff that matters.
Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by SkyPuncher on Tuesday February 19, 2019 @10:44AM (#19199686)
Static typing is fantastic in a closed system. It's predictable and reliable.

My problem is nearly every type system breaks down when you try communicate with something outside of the closed system (e.g. API calls, 3rd party integration, etc).

An API returns an unexpected data type, Missing a field that's supposed to be required, Starts returning a number as a string because they changed their ID system, etc.

It always seems to end up in types where every field is optional - defeating the purpose of having types in the first place.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by phaedrus on Tuesday February 19, 2019 @10:58AM (#19199810)
The problem here is not static typing or its interface to the outside world. The problem is most mainstream static typed languages have shit handling for Sum Types. With proper support and proper syntax static typing would actually help you ensure you handle both possibilities of your "option" type. (Any language which defaults to any reference type may be null, at any time, is the opposite of properly handling option types.)

Let me turn this around: if you're saying possibly-missing fields inevitably propagate throughout the types in your codebase, it sounds like you're saying malformed input propagates arbitrarily deeply into your core logic. Wouldn't you rather catch that at the boundary?

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by SkyPuncher on Tuesday February 19, 2019 @11:16AM (#19199977)
> if you're saying possibly-missing fields inevitably propagate throughout the types in your codebase, it sounds like you're saying malformed input propagates arbitrarily deeply into your core logic. Wouldn't you rather catch that at the boundary?

Possibly. If it's a key part of the application, like Credit Card processing or auth - absolutely. If it's simply auxiliary or supplemental information - probably not. The thing for me is the type system shouldn't decide whether or not a certain piece of information is critical to the system. That typically falls to deeper logic in the system. If the type system isn't deciding, then it's pretty much meaningless (as everything would be optional).

For example, I'm using a Stripe JS integration to allow customers to manage credit cards in app. Stripe messes up or changes something without me paying attention. Instead of returning a complete object, Stripe starts returning only the credit card ID.

This isn't a big deal if we simply need to know that a record exists. We can still tell the user has a card and we have an ID to attempt a transaction against server side. We might not be able to display details about the card, but users could still submit a transaction (likely against their last used card).

However, it is a big deal in a credit card management interface. Without supplementary data, all of the cards would look exactly the same and the UI might break if expected fields aren't present.

----

I don't want my type system deciding what the use case is or how important one case is compared to the other. The actual implementation needs to decide if it has the information it needs to do its job.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by klmr on Tuesday February 19, 2019 @11:19AM (#19200005)
> The thing for me is the type system shouldn't decide whether or not a certain piece of information is critical to the system.

No, that is in fact the stated purpose of the type system. If you don’t want to make such a decision statically, you mark a field appropriately (e.g. via an Option type).

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by SkyPuncher on Tuesday February 19, 2019 @11:43AM (#19200254)
I understand that's the stated purpose. I'm demonstrating how I feel types can pretty much become useless when integrating with an outside system.

If everything is optional, then you might as well not use a type system in the first place.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by klmr on Tuesday February 19, 2019 @12:22PM (#19200724)
Again, there’s virtually never a reason to make everything optional/variant. It’s a conscious design choice and should be used judiciously (otherwise the API becomes difficult to use, and the same is true in a purely dynamic system).
Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by pacala on Tuesday February 19, 2019 @11:30AM (#19200119)

    data Stripe1_0 = {
      field1: type1
      field2: type2
    }

    data Stripe2_0 = {
      cc_id: str
    }

    data Stripe = Stripe1_0 | Stripe2_0
Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by pacala on Tuesday February 19, 2019 @11:26AM (#19200069)
Static type systems are just a tool. The type signature describes what _your_ module is capable of handling. If other modules pass incorrect data, your module will fail, whether statically or dynamically typed, albeit harder to debug in the dynamically typed case. If every field ends up being truly optional, it's going to be hard to change your module, because of the sheer weight of the combinatorial explosion of field subsets that may or maynot be set when third party modules interact with your module.

Most of the time optional fields:

* Encode that your module doesn't care about such-and-such fields, so the parser / type checker should just ignore them.

* Are used to encode variants of the same data that slightly changed in another module [id changed from number to string], so you should use an union type.

In practice, designing a statically typed ecosystem for wire data is hard [0]. Grpc has basically gave up on validating field presence altogether [1]. These failures are directly caused by the immaturity of the ecosystem [deep decode batches of unrelated messages in middleware, WTF?]. It would be a mistake to throw the baby with the bathwater and blame the mathematically sound idea that your module can only process data of a certain shape, encoded by a suitable type signature.

[0] https://capnproto.org/faq.html#how-do-i-make-a-field-require...

[1] https://github.com/protocolbuffers/protobuf/issues/2497#issu...

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by idrae on Tuesday February 19, 2019 @10:53AM (#19199756)
I agree with you that static typing can create more work on the boundries of a system.

However, the problems you describe also happen in dynamic languages and they have just as much potential to cause problems. The biggest issue with dynamic languages is that you don't get immediate errors when something changes. The system will just keep doing its thing, until it doesn't and you discover that something has changed and has already been propagated to other parts of the system.

I don't think dynamic languages are always better in these cases. They basically let you defer the error/change handling, but with the potential of much bigger data integrity/quality problems when things eventualy do go wrong because of changes in an integration.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by klmr on Tuesday February 19, 2019 @10:53AM (#19199761)
The API problems you cite aren’t prevented in dynamically typed systems. If anything they are worse, because they could lead to silent failures, whereas a statically typed API statically checks and guarantees the contract. Contrary to what you imply, a statically typed API can’t return an unexpected type. That’s an advantage.

> It always seems to end up in types where every field is optional

“Always”? Not by a long shot. These situations where this is the case can be isolated and dealt with explicitly. You may object to the explicitness but — again, for reasons of robustness — proponents of static typing prefer this.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by pmontra on Tuesday February 19, 2019 @11:04AM (#19199868)
> whereas a statically typed API statically checks and guarantees the contract

I didn't work much with static typed languages lately, so I'm really puzzled by your comment. If I expect an int in the age field of a JSON response and the server suddenly sends me a string, maybe with value "NaN", how do you check for that at compile time? I expect the program to either crash or manage the error at run time (try/catch, die-and-restart a-la-Erlang, etc)

Maybe JSON doesn't count as "statically typed API", but how many of them do common Internet services expose to the public? And how to we make sure that the server or any in between proxy doesn't misbehave and violates the contract?

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by klmr on Tuesday February 19, 2019 @11:17AM (#19199984)
There are different kinds of APIs. You seem to be talking about inherently dynamic ones, such as your JSON blob example. Of course you cannot statically guarantee their contents, which is why statically typed JSON layers provide ways of asserting a schema. Using such schema checkers is best practice even in dynamically typed languages.

Either way, you need to handle such failures, both in statically and in dynamically type-checked languages. The only difference is whether the handling happens explicitly throughout your code or automagically by an API that gives you back a well-behaved object. And both of these ways are possible in both static and dynamic languages (but static languages tend to go for the latter, whereas dynamic languages tend to make you do the former).

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by steveklabnik on Tuesday February 19, 2019 @11:13AM (#19199949)
The way this works is, when you say “please turn this JSON string into a data structure”, it’s a fallible operation. So, if that JSON string does not have what you expect, the serialization operation will fail. You can “check at compile time” by the system ensuring that you handle the success and failure cases. Additionally, it means that if you have an instance of that data structure, it is always valid; it is checked (at runtime) upon creation.

Does that make sense? I can show you some code if you’d like.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by rhacker on Tuesday February 19, 2019 @11:00AM (#19199821)
There's nothing stopping a dynamically typed system from using something like grpc or graphql that can check for missing fields. Or something like json schema to check for missing fields in a lot of those scenarios. A type system doesn't necessarily catch missing fields, but a communication framework of some kind, does.
Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by klmr on Tuesday February 19, 2019 @11:20AM (#19200019)
I entirely agree, see my orthogonal comment reply.
Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by mjburgess on Tuesday February 19, 2019 @10:53AM (#19199754)
Option types do not defy the purpose of typing. Optionality as a type constraint is a reason for static typing.
Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by lewisf on Tuesday February 19, 2019 @10:56AM (#19199787)
This is likely going to lead to an error in both statically and dynamically typed languages.

In statically typed language, at least when troubleshooting it's an easy guess that the error is highly likely to be at the external boundary of your application code since the rest of it type checked.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by carlmr on Tuesday February 19, 2019 @11:29AM (#19200102)
If the API you rely on breaks, you have to change your code. This is exactly how catastrophes happen.

It's more of an issue that you have unreliable 3rd party vendors that you buy from/use.

Re:Why I Prefer Dynamic Typing Over Static Typing (2017) by SkyPuncher on Tuesday February 19, 2019 @11:44AM (#19200267)
> It's more of an issue that you have unreliable 3rd party vendors that you buy from/use.

It happens very, very rarely - but the problem is when it happens it can happen suddenly and be very, very hard to address properly.