The advantages of static typing, simply stated
My disagreement starts with the beginning of this:<p>&quot;A large class of errors are caught, earlier in the development process, closer to the location where they are introduced.&quot;<p>I would re-state this as:<p>&quot;A large class of errors are introduced, which otherwise would not exist.&quot;<p>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&#x27;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.<p>Consider the Salesforce clone of Java, Apex, which I have had to work in this month.<p>The if() statements here are the same one&#x27;s that I would have to write in Ruby or Python or PHP, but meanwhile I&#x27;ve had to do a bunch of other, useless work:<p><pre><code>    public Object deserializeJson(String sandi_data) {
        System.debug(sandi_data);
        Object objResponse = JSON.deserializeUntyped(sandi_data);
        
        if (objResponse instanceof Map&lt;String, Object&gt;) {
            Map&lt;String, Object&gt; mapResponse = (Map&lt;String, Object&gt;)objResponse;                
            List&lt;Object&gt; dataList = (List&lt;Object&gt;)mapResponse.get(&#x27;data&#x27;);
            if(dataList == null) {
                String err = &#x27;The Sandi API field for data was null&#x27;;
                System.debug(err); 
                ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.ERROR, err);
                ApexPages.addmessage(msgErr);
                return null;
            } else if (dataList.isEmpty()) {
                String err = &#x27;The Sandi API field for data was empty&#x27;;
                System.debug(err); 
                ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.ERROR, err);
                ApexPages.addmessage(msgErr);
                return null;
            } else {
                System.debug(&#x27;dataList:&#x27;);
                System.debug(dataList);
                return dataList; 
            }
        }
        return sandi_data;
    }
</code></pre>
And then, downstream of this:<p><pre><code>            List&lt;Object&gt; dataList = (List&lt;Object&gt;)deserializeJson(sandi_data);

            for(Integer i=0; i &lt; dataList.size(); i++) {                
                Map&lt;String, Object&gt; dataMap = (Map&lt;String, Object&gt;)dataList[i];
                System.debug(&#x27;dataMap:&#x27;);
                System.debug(dataMap);
                String response = fetchCompany(dataMap);
                SearchResult__c profile = saveProfileResult(response);
                cr.add(profile);
            }

</code></pre>
I&#x27;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.<p>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.<p>In a dynamic language I could simply work with a deeply nested data structure of maps and lists, and I&#x27;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.<p>Static typing does not live up to its promises.<p>[ Edit to add ]<p>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&#x27;t use JSON. If they have to use JSON, I wish they at least enforced a consistent schema. But they don&#x27;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.<p>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.<p>The notion that An External API Is Reliable is as stupid as the notion The Network Is Reliable:<p><a href="https:&#x2F;&#x2F;blog.fogcreek.com&#x2F;eight-fallacies-of-distributed-computing-tech-talk&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.fogcreek.com&#x2F;eight-fallacies-of-distributed-com...</a><p>[[ Further edit to add ]]<p>the_af wrote:<p>&quot;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.&quot;<p>What I wrote was:<p>&quot;In a dynamic language I could simply work with a deeply nested data structure of maps and lists, and I&#x27;d handle the casting at the very end of the process&quot;<p>I&#x27;ll simplify this: there are 3 times when we can enforce a schema:<p>1.) when the API call returns with a string<p>2.) on every line, scattered through dozens of functions<p>3.) at the end, when I have the data that I want<p>In my original comment, I advocated for #3. Here are the reasons I don&#x27;t like the first 2 options:<p>#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.<p>#2 - having casting discovery information scattered through dozens of functions makes the code brittle and refactoring difficult.<p>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.<p>In static-type languages such as Java, I&#x27;m forced to go with either #1 or #2, and they are both bad options.<p>About this, from tigershark:<p>&quot;it is only the usage of an awful JSON library in a not so nice language&quot;<p>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.<p>As to &quot;not so nice language&quot;, 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&#x27;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!