News for hackers. Stuff that matters.
Re:The advantages of static typing, simply stated by susan_hall on Friday September 16, 2016 @09:59AM (#12514006)
My disagreement starts with the beginning of this:

"A large class of errors are caught, earlier in the development process, closer to the location where they are introduced."

I would re-state this as:

"A large class of errors are introduced, which otherwise would not exist."

Consider dealing with JSON in Java. Every element, however deeply nested, needs to be cast, and miscasting leads to endless errors, elsewhere in the code. 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.

Consider the Salesforce clone of Java, Apex, which I have had to work in this month.

The if() statements here are the same one's that I would have to write in Ruby or Python or PHP, but meanwhile I've had to do a bunch of other, useless work:

    public Object deserializeJson(String sandi_data) {
        System.debug(sandi_data);
        Object objResponse = JSON.deserializeUntyped(sandi_data);
        
        if (objResponse instanceof Map<String, Object>) {
            Map<String, Object> mapResponse = (Map<String, Object>)objResponse;                
            List<Object> dataList = (List<Object>)mapResponse.get('data');
            if(dataList == null) {
                String err = 'The Sandi API field for data was null';
                System.debug(err); 
                ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.ERROR, err);
                ApexPages.addmessage(msgErr);
                return null;
            } else if (dataList.isEmpty()) {
                String err = 'The Sandi API field for data was empty';
                System.debug(err); 
                ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.ERROR, err);
                ApexPages.addmessage(msgErr);
                return null;
            } else {
                System.debug('dataList:');
                System.debug(dataList);
                return dataList; 
            }
        }
        return sandi_data;
    }
And then, downstream of this:

            List<Object> dataList = (List<Object>)deserializeJson(sandi_data);

            for(Integer i=0; i < dataList.size(); i++) {                
                Map<String, Object> dataMap = (Map<String, Object>)dataList[i];
                System.debug('dataMap:');
                System.debug(dataMap);
                String response = fetchCompany(dataMap);
                SearchResult__c profile = saveProfileResult(response);
                cr.add(profile);
            }

I'm leaving out the code that is downstream of this function, but it is full of more of the same: guessing at fields, guessing at how they should be cast, using if() to guard against null or empty. Tons of unnecessary bloat. Lots of easy errors to make.

Again, some of the if() statements need to be made in Ruby or Python or PHP, but the rest of it is just pure bloat. Verbose, unneeded and unhelpful.

In a dynamic language I could simply work with a deeply nested data structure of maps and lists, and I'd handle the casting at the very end of the process. In a dynamic language, I could treat everything as a string till the very end, and then cast to integers or dates or floats or strings as needed. In a dynamic language, I could write the code faster, with less errors, and with less code.

Static typing does not live up to its promises.

[ Edit to add ]

We have no control over the API that we draw from. We are drawing from the API of a different company. I wish they didn't use JSON. If they have to use JSON, I wish they at least enforced a consistent schema. But they don't. And that is why static type checking fails: because the real world is chaotic, and when you have to interact with that real world, you are often forced to do so dynamically, because of the mistakes that other companies have made. The real world is dynamic.

The idea that you can know an external API perfectly is a fantasy. The real world is messy. The real world does not always conform to a strict schema.

The notion that An External API Is Reliable is as stupid as the notion The Network Is Reliable:

https://blog.fogcreek.com/eight-fallacies-of-distributed-com...

[[ Further edit to add ]]

the_af wrote:

"Using dynamic typing will just hide the problems under the rug, and they will explode in your face later on. Static typing just made those problems explicit."

What I wrote was:

"In a dynamic language I could simply work with a deeply nested data structure of maps and lists, and I'd handle the casting at the very end of the process"

I'll simplify this: there are 3 times when we can enforce a schema:

1.) when the API call returns with a string

2.) on every line, scattered through dozens of functions

3.) at the end, when I have the data that I want

In my original comment, I advocated for #3. Here are the reasons I don't like the first 2 options:

#1 - the external API is bloated, so writing a schema for the whole thing would be difficult to justify in terms of business. We only need a tiny slice of the data.

#2 - having casting discovery information scattered through dozens of functions makes the code brittle and refactoring difficult.

With Ruby or Python or PHP or any dynamic language I have the option of #3: grab the data, cast everything as a string, grab the tiny sliver of data I actually need, and then enforce the schema on that tiny sliver. This is the data that I can cast to integers, floats, dates, etc -- whatever is actually needed.

In static-type languages such as Java, I'm forced to go with either #1 or #2, and they are both bad options.

About this, from tigershark:

"it is only the usage of an awful JSON library in a not so nice language"

Bad JSON is part of the real world. If your static-type language can not handle bad JSON, then it can not handle the real world. That is my point: static-type checking is too academic, too pure, for the real world.

As to "not so nice language", you are engaging in the No True Scotsman fallacy, which goes like this: no True statically typed language would be this bad! But following the No True Scotsman illogic, the rest of your unstated assumptions amount to: It's only the statically typed languages that most programmers actually use that are this bad! But somewhere there is a statically-typed language of such unbelievable purity, it overcomes all of these problems!

Re:The advantages of static typing, simply stated by tigershark on Friday September 16, 2016 @10:24AM (#12514191)
Your example is non-sensical. It has nothing to do with a comparison between static types vs dynamic types, it is only the usage of an awful JSON library in a not so nice language. In F# you even have Type Providers for JSON that give you auto-completion in the IDE:

    type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
    let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """)
    simple.Age
    simple.Name
and most importantly you don't have nulls.
Re:The advantages of static typing, simply stated by tmhedberg on Friday September 16, 2016 @10:19AM (#12514157)
Haskell's aeson library, which is probably the most popular way to use JSON in that language, doesn't require any of this complexity or typecasting ugliness. It can derive a typechecking parser for a particular JSON format from an ordinary Haskell data type. Your type becomes a JSON schema, and as long as the JSON you're parsing conforms to that schema, the parsing just works with a single function call.

The problem you're pointing out is not a problem with statically typed languages. It's a problem with bad languages.

Re:The advantages of static typing, simply stated by jomamaxx on Friday September 16, 2016 @10:11AM (#12514109)
I think the problem in your example is mostly related to the fact that Java syntax is just horrible for dealing with JSON - not really an issue of typing.

If Java were conceived of today - I think it might look a little different with respect to this.

I'd like to point out that there are many areas wherein fluid typing might help a little bit, especially on the UI.

Have to make a class for 'every little thing' gets cumbersome.

I wish there was a 'lighter typing' opportunity in some cases (schema-ish JSON).

But I agree that in the long run, static typing is really the way to go for the most part.

Re:The advantages of static typing, simply stated by tdb7893 on Friday September 16, 2016 @10:28AM (#12514209)
Java is fine at decoding Json if you know what the fields are going to be. It's not like creating an object with the fields you want is very difficult and then libraries will deserialize it easily. It's also not hard to create a tool that will generate a POJO class from a string of Json. It doesn't really take time so the only problem is that it is yet another class on the classpath
Re:The advantages of static typing, simply stated by jomamaxx on Friday September 16, 2016 @10:30AM (#12514235)
"Java is fine at decoding Json "

It does the job but it's not nearly as nice as javascript or python. It's about double the code in Java and it doesn't read nicely.

I think it depends on what you're doing.

Re:The advantages of static typing, simply stated by tdb7893 on Friday September 16, 2016 @10:58AM (#12514432)
Java definitely makes it harder to work with json but the only problem I run in to is that I need classes (which I just autogenerate) for every json object. Serializing and deserializing json is a one liner once you have classes set up for them to deserialize into. Again, it is definitely harder than in Python and Javascript but I work with Java and json every day and it's generally only a huge difficulty if you need a strange edge case.

EDIT: also, java is bad at dealing with json if the api could return data in multiple different schemas and you don't know which one it is until you parse it but I've only run into this once and it's just bad API design.

Re:The advantages of static typing, simply stated by jomamaxx on Friday September 16, 2016 @11:01AM (#12514466)
Ah, yeah, if you have known-entities ahead of time it's no problem via serialization.

Developing/experimenting/scripting is where it's painful.

Re:The advantages of static typing, simply stated by empath75 on Friday September 16, 2016 @10:17AM (#12514140)
Elm is similarly awful at decoding json, and was what stopped me from trying to use it. I mean, it'll get the job done, eventually, but it's just so much additional work, compared to just json.loads() in python.
Re:The advantages of static typing, simply stated by danneu on Friday September 16, 2016 @11:15AM (#12514561)
I'd say working with JSON in Elm is pleasant and simple, but it does require familiarity with parser combinators.

It's a common challenge for Elm beginners.

Re:The advantages of static typing, simply stated by douche on Friday September 16, 2016 @10:11AM (#12514102)
That Salesforce has a relatively broken variant of a less-than-ideal statically typed language is less a problem with the idea of static typing than with Salesforce's crappy implementation[1].

There are less awful ways to deserialize JSON in other languages. In C# with JSON.Net, you have options ranging from very dynamic-style handling, all the way to fully-typed deserialization into POCOs, for example.

[1] I have no love for Salesforce; it's a mess. Their weird custom dialect of SQL for a REST API query language is also buckets of fun.

Re:The advantages of static typing, simply stated by yamadapc on Friday September 16, 2016 @10:54AM (#12514388)
In Haskell you can generate a parser to a typed structure using library combinators and code generation:

    data DataMessage = DataMessage { company :: Text
                                   }
      deriving (Show)
    $(deriveJSON defaultOptions ''DataMessage)

    data DataList = DataList { _data :: [DataMessage]
                             }
      deriving (Show)
    $(deriveJSON defaultOptions { fieldLabelModifier = drop 1 } ''DataList)

Which will fail properly when the data is malformed and in the real-world is even shorter, because you can set your data-type to protocol naming conventions and share them throughout.

Then there isn't much bloat. You wrote your types and generated your way to parse them.

You could test with:

    main = do
        print (encode (DataMessage "some-company"))
        print (decode "{\"company\":\"some-company\"}" :: Maybe DataMessage)

        print (encode (DataList [DataMessage "c1", DataMessage "c2", DataMessage "c3"]))
        print (decode "{\"data\":[{\"company\":\"c1\"},{\"company\":\"c2\"}]}" :: Maybe DataList)

https://gist.github.com/b30f6f09a737dcc980e052b0f3d2a39e
Re:The advantages of static typing, simply stated by yamadapc on Friday September 16, 2016 @10:57AM (#12514419)
You can also generate the types and parsers from a data sample.
Re:The advantages of static typing, simply stated by the_af on Friday September 16, 2016 @10:26AM (#12514203)
You seem to be focusing just on the (admittedly problematic) problem of parsing outside and possibly invalid input. This is hard, especially if the API is poorly defined.

But this is hard with any typing system. Using dynamic typing will just hide the problems under the rug, and they will explode in your face later on. Static typing just made those problems explicit.

There are many benefits to static typing, beyond the external boundaries of the system, where it definitely lives up to its promises.

Re:The advantages of static typing, simply stated by sidlls on Friday September 16, 2016 @10:04AM (#12514054)
Have you considered the possibility instead that JSON is not an appropriate serialization protocol or storage mechanism for the problem to be solved? Or that it isn't being used effectively/correctly (that is, it isn't being parsed correctly, e.g. with a library or some other mechanism that makes checking underlying types less cumbersome)? I noted in a comment downstream from here that plenty of C and C++ JSON libraries offer very easy parsing (e.g. "json_get_int(element, key)") that fails early. I almost don't even think about JSON twice if I have to use it as a data format in a C++ program I'm working on. It's just another library to link against and use almost effortlessly. Doesn't Java, or this Salesforce implementation of it, have something similar?

Furthermore every dynamic language I've used has had tooling spring up, e.g. in the form of linters, that includes at least some basic checking for type mismatch where possible. I'm not so sure static typing leads to problems. It isn't problem free--no programming language or paradigm is--but I do think the lack of it leads to some problems that are easily avoided without too much extra overhead.

Re:The advantages of static typing, simply stated by empath75 on Friday September 16, 2016 @10:15AM (#12514128)
Have you worked with a public api in the last few years that didn't depend on json?
Re:The advantages of static typing, simply stated by zeveb on Friday September 16, 2016 @10:34AM (#12514264)
'Everybody else is doing it' is insufficient justification to keep on doing it.

As it happens, I agree that JSON is probably a long-term mistake, although it is definitely an improvement over XML, the previous player in that space.

Re:The advantages of static typing, simply stated by asimuvPR on Friday September 16, 2016 @10:54AM (#12514391)
Every technology is relatively wrong. Time points out where and we fix it (process is repeated again forever).
Re:The advantages of static typing, simply stated by jack9 on Friday September 16, 2016 @10:10AM (#12514101)
> your only option is to cast to Object

In Java JSON is horrible for serialization because of Java behaviors (your best option is Object). That doesn't mean JSON is horrible for serialization, in general. Fields do have types (int, string, object, array) making it slightly better than arbitrary serialization.

Re:The advantages of static typing, simply stated by sidlls on Friday September 16, 2016 @10:31AM (#12514239)
I haven't used Java in a long time, so I don't what the library ecosystem for JSON is, but with both C and C++ there are plenty of parsers that make type casting and working with JSON data about as easy as in python. The difference is usually that the type of the underlying data one is interested in working with is explicit and, when the JSON data is malformed is caught earlier (at cast, as opposed to later, when trying to, e.g. add a string to an int in sane languages that break when that happens).
Re:The advantages of static typing, simply stated by sdegutis on Friday September 16, 2016 @10:08AM (#12514086)
Meh. A good JSON library for Java can still make it fail fast and fail early, and using Java's reflection it can be pretty concise too.
Re:The advantages of static typing, simply stated by zzalpha on Friday September 16, 2016 @10:07AM (#12514074)
Yup. The correct solution to this is schemas, not throwing away typing information.
Re:The advantages of static typing, simply stated by susan_hall on Friday September 16, 2016 @10:08AM (#12514083)
We have no control over the API that we draw from. We are drawing from the API of a different company. I wish they didn't use JSON. If they have to use JSON, I wish they at least enforced a consistent schema. But they don't. And that is why static type checking fails: because the real world is chaotic, and when you have to interact with that real world, you are often forced to do so dynamically, because of the mistakes that other companies have made. The real world is dynamic.
Re:The advantages of static typing, simply stated by sidlls on Friday September 16, 2016 @10:42AM (#12514309)
The real world being dynamic has almost literally nothing to do with this issue. The concepts are completely unrelated.

Although I'd agree that I find development with dynamically typed languages a bit more chaotic than static or strongly typed ones. And not necessarily in a fun, good way, either.

Re:The advantages of static typing, simply stated by mamon on Friday September 16, 2016 @10:19AM (#12514160)
Ok, so they created a problem by using JSON without schema.

Now, instead of fixing problem by enforcing some schema you propose to use dynamically typed programming language which won't solve the problem but instead will make it spread thorough the whole codebase.

At least static typing limits the damage to the code that directly deals with parsing JSON.

Re:The advantages of static typing, simply stated by oldmanjay on Friday September 16, 2016 @10:36AM (#12514279)
You're insisting on an assertion that the experience of many of us directly contravenes. It's not the best way to make a point.
Re:The advantages of static typing, simply stated by BasDirks on Friday September 16, 2016 @10:14AM (#12514116)
The way you talk about static vs dynamic suggests that you only have a very vague idea of what these mean.
Re:The advantages of static typing, simply stated by noelwelsh on Friday September 16, 2016 @10:26AM (#12514202)
You don't need to have these issues. If you have a field that is optional, you can model that with the Optional type in Java 8. So I would model your dataList as Option<List<Something>>. If I cared that a list is definitely not empty I would create a type called NonEmptyList to model that. This is very typical is Scala, which is the language I use most often.
Re:The advantages of static typing, simply stated by RussianCow on Friday September 16, 2016 @11:07AM (#12514502)
Every element, however deeply nested, needs to be cast, and miscasting leads to endless errors, elsewhere in the code.

But these aren't needless errors. If something is not of the type that you expect, there should be an error, and static typing makes this more explicit. The verbosity is more to do with that particular implementation, not anything inherent to static type systems.

Re:The advantages of static typing, simply stated by hackits on Friday September 16, 2016 @10:36AM (#12514278)
Sorry to say this but you're talking out of your ####.

All the research from expiercal studies mostly all show that static vs dynamic languages all correlated with a Expertise reversal effect. This is for beginners not experience programmers. That at the start of the studies for the first 8 to 9 months novices have a negative correlation with the speed of development work with a statically typed language. After 8 to 9 months they gain a singificant boost from static type language.

Source to backup my statement and resource if you're interested @ Susan_hall

Functional Geekery Episode 55 – Andreas Stefik https://www.functionalgeekery.com/episode-55-andreas-stefik/...

Secondly another study that was published for Polymorphism in Python from the website (neverworkintheory)

Polymorphism in Python http://neverworkintheory.org/2016/06/13/polymorphism-in-pyth...

Gives the qoute: `Our findings show that the receiver in 97.4% of all call-sites in the average program can be described by a single static type using a conservative nominal type system using single inheritance. If we add parametric polymorphism to the type system, we increase the typeability to 97.9% of all call-sites for the average program.`

I could qoute other resources to backup my statements though if you want to have a skype coversation I'm up for a talk.

Re:The advantages of static typing, simply stated by willtim on Friday September 16, 2016 @10:34AM (#12514269)
Abstract over and guard against external messy systems at the boundaries. Static types help with this just as much as enforcing invariants and consistency, at least with a good language.
Re:The advantages of static typing, simply stated by ashark on Friday September 16, 2016 @10:23AM (#12514178)
There's casting happening all over in the dynamic code in this scenario. It's implicitly handling what would be error conditions or explicit casting in a static language. Whether that's a good thing or not is what's at issue (generally, no, it's not).