3 ms·
>Why I prefer dynamic-typing over static-typing: the speed of adapting to change Funny, that's the same reason I prefer static typing. >In How ignorant am I,
by coldtea 8y ago
>Why I prefer dynamic-typing over static-typing: the speed of adapting to change
Funny, that's the same reason I prefer static typing.
>In How ignorant am I, and how do I formally specify that in my code? I said that I liked to add run-time contracts to a function as I better understand it. When I first write a function, I may not know for sure how I will use it.
Which is irrelevant. You still know that the "customer_name" argument to it will be a string and the "age" will be an integer, no?
>For the programming that I do, I am often creating new architectures. Therefore I need dynamic typing.
Non seguitur.
>Consider dealing with JSON in Java. Every element, however deeply nested, needs to be cast, and miscasting leads to errors.
Another bad argument. First, if you don't cast, and expect an integer like 55 but get a string like John, your program has an error. The difference is that in a dynamic language your program will continue to run doing stupid things instead of crashing in the miscast.
>Given JSON whose structure changes (because you draw from an API which leaves out fields if they don’t have data for that field) your only option is to cast to Object, and then you have to guess your way forward, figuring out what the Object might be.
You can trivially have a JSON parser that returns a list of map of the 6-7 valid JSON types (maps ("objects"), floats/"integers", strings, booleans, null, arrays).
You don't need to deserialize to specific object structure/schema as is the default mode in some parsers, if that's what you want to avoid.
If you dislike the verbosity of getting nested elements in Java, that's a Java thing, not a static typing thing. Heck, it's very easy in Haskell (what with lenses and stuff).
With a language that supports Optionals and try or has a map index operator and list/map literals, it's more like JS or Python than Java.
If you just want to extract a deeply nested element, and not care what it is, but still use it, I'm not sure how that works.
Do you expect the object at e.g. resp["x"]["y"]["z"] to change types randomnly?
>Static typing does not live up to its promises. If it did live up to its promises, we would all use it, and the fact that we don’t all use it suggests how often it fails.
"Proper weight does not live up to its promises. If it did live up to its promises, we would all be on it it, and the fact that many are obese suggests how often it fails"
How about some uses of dynamic typing are for specific, constrained, use cases (e.g. glueing, quick scripts) and it increasingly breaks down with larger codebases and multi-developer projects?
Larger codebases and multi-developer projects either don't use dynamic typing (most huge projects the world relies on, from compilers to browsers, and from OSes, to all kinds of embedded hardware, games, desktop apps, etc, are done in one of C/C++/Java), and others (e.g. large websites) are moving towards some kind of types (e.g. Typescript and Flow).
- pmontra 8y ago> You still know that the "customer_name" argument to it will be a string and the "age" will be an integer, no? I've seen integers sent as strings and others as integers in the same JSON. Datetimes stored as strings in three different formats by the same application in the same column of the same table, etc. The real world is really messy. Writing a new program is definitely quicker with a language like Ruby or Python for me (especially Ruby.) Working with Java is like walking with a cast on a leg. But you know, every project and every developer is different, that's why we are using so many different tools. The OP accounts for that in the "There are many arguments in favor of static-typing" paragraph.
- coldtea 8y ago>I've seen integers sent as strings and others as integers in the same JSON. You still need to know what they are in each case, regardless of dynamic vs static typing (it's a matter of whether the language has weak vs strong typing or overly accommodating coercion). If you get an integer value as string for example, Python won't let you add it to another integer you got as integer. So you'll need to know what they are to treat them accordingly in your processing even in a dynamic language. In such case, you can also read them as a variant type on a static language, that's eg. one of the 7 allowed types in JSON (you don't know whether a JSON primitive might be int or string, but you know it wont be suddenly a Date type or a Foobar instance).
- majewsky 8y ago> I've seen integers sent as strings and others as integers in the same JSON. So what? Just use a veryFlexibleUint64: https://github.com/sapcc/limes/blob/d51ecdbb763318da146b9f3246ffc993c2632947/pkg/plugins/client_ironic.go#L127 https://github.com/sapcc/limes/blob/d51ecdbb763318da146b9f32... ;)