{"id":16906,"date":"2017-01-12T15:31:19","date_gmt":"2017-01-12T20:31:19","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16906"},"modified":"2019-02-14T10:43:04","modified_gmt":"2019-02-14T15:43:04","slug":"static-typing-vs-testing","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/01\/12\/static-typing-vs-testing\/","title":{"rendered":"Static Typing vs. Testing"},"content":{"rendered":"<p><a href=\"http:\/\/blog.cleancoder.com\/uncle-bob\/2017\/01\/11\/TheDarkPath.html\">Robert C. Martin<\/a> (<a href=\"https:\/\/www.reddit.com\/r\/swift\/comments\/5nhy1n\/uncle_bob_on_swift_and_kotlin\/\">Reddit<\/a>):<\/p>\n<blockquote cite=\"http:\/\/blog.cleancoder.com\/uncle-bob\/2017\/01\/11\/TheDarkPath.html\"><p>My problem is that both [Swift and Kotlin] have doubled down on strong static typing.  Both seem to be intent on closing <em>every single type hole<\/em> in their parent languages.<\/p><p>[&#8230;]<\/p><p>But before you run out of fingers and toes, you have created languages that contain dozens of keywords, hundreds of constraints, a tortuous syntax, and a reference manual that reads like a law book.  Indeed, to become an expert in these languages, you must become a <em>language lawyer<\/em> (a term that was invented during the C++ era.)<\/p><p>[&#8230;]<\/p><p>Why are these languages adopting all these features?  Because programmers <em>are not testing<\/em> their code.  And because programmers are not testing their code, we now have languages that <em>force<\/em> us to put the word <code>open<\/code> in front of every class we want to derive from.  We now have languages that <em>force<\/em> us to adorn every function, all the way up the calling tree, with <code>try!<\/code>.   We now have languages that are so constraining, and so over-specified, that you have to design the whole system up front before you can code any of it.<\/p>\n<p>[&#8230;]<\/p>\n<p>All these constraints, that these languages are imposing, presume that the programmer has perfect knowledge of the system; <em>before the system is written<\/em>.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/drewmccormack\/status\/819286390836002816\">Drew McCormack<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/drewmccormack\/status\/819286390836002816\"><p>Not in total agreement with the post, but quite a lot of agreement.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/jckarter\/status\/819284087764455424\">Joe Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/819284087764455424\"><p>A type system is only as static as the code it describes. If you can change code, you can change types! The machine can even help you.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/chris.eidhof.nl\/post\/types-vs-tdd\/\">Chris Eidhof<\/a>:<\/p>\n<blockquote cite=\"http:\/\/chris.eidhof.nl\/post\/types-vs-tdd\/\"><p> I don&rsquo;t think it&rsquo;s an either\/or situation at all: we can have a solid type system and write &ldquo;manual&rdquo; tests.<\/p>\n<p>A type checker actually does testing for you. It&rsquo;s not a replacement for TDD, but it allows you to completely get rid of a whole bunch of tests.<\/p>\n<p>[&#8230;]<\/p>\n<p>In Swift, once you change <code>foo<\/code> to return an <code>Int?<\/code>, the compiler will now show an error for each time you call <code>foo<\/code>. This makes it <em>easy<\/em> to make that change, because until you have reviewed every single call to <code>foo<\/code>, the program simply won&rsquo;t compile. I think of the compile errors as a todo-list, not as a <a href=\"https:\/\/twitter.com\/unclebobmartin\/status\/819262224686546945\">speed bump<\/a>.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/merowing_\/status\/819624692705361920\">Krzysztof Zab&#322;ocki<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/merowing_\/status\/819624692705361920\"><p>I&rsquo;m pro testing but I think this matches my thoughts exactly, almost anything you can do to get errors earlier in dev process is worth it.<\/p><\/blockquote>\n\n<p>I like both testing and types. The problem is that sometimes types get in the way. It would be nice to be able to do some work in a more dynamic style and then nail things down before shipping. That is the promise of type inference, but it&rsquo;s not the reality of developing in Swift today.<\/p>\n\n<p>Update (2017-01-12): <a href=\"https:\/\/twitter.com\/isaiah\/status\/819681109361459202\">Isaiah Carew<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/isaiah\/status\/819681109361459202\"><p>i wish i could have both. static typing is really useful sometimes. really a burden other times.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/owensd\/status\/819702037210275840\">David Owens II<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/owensd\/status\/819702037210275840\"><p>My main problem with Swift has always been this: far too pedantic about types and complete lack of a dynamic runtime.<\/p><\/blockquote>\n\n<p>Update (2017-01-13): <a href=\"http:\/\/www.markbernstein.org\/Jan17\/StaticTypingvsTesting.html\">Mark Bernstein<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.markbernstein.org\/Jan17\/StaticTypingvsTesting.html\"><p>Reading this debate, I was startled to realize one thing I&rsquo;d overlooked: I almost never make type errors. I make plenty of errors &#8211; for me, programming is always about correcting errors &#8211; but type errors are once in a blue moon affairs.<\/p>\n<p>I wonder if strong typing is a solution to a problem we don&rsquo;t really have, or whether I&rsquo;m just Doing It Wrong.<\/p><\/blockquote>\n<p>I concur. I think Swift&rsquo;s optionals solve a real problem. The stronger typing, e.g. for collections, can make calling APIs more concise and help with auto-completion, but I don&rsquo;t see this preventing many errors for me.<\/p>\n\n<p>Update (2017-01-14): <a href=\"http:\/\/blog.cleancoder.com\/uncle-bob\/2017\/01\/13\/TypesAndTests.html\">Robert C. Martin<\/a>:<\/p>\n<blockquote cite=\"http:\/\/blog.cleancoder.com\/uncle-bob\/2017\/01\/13\/TypesAndTests.html\"><p>To be clear, and at the risk of being repetitive, that blog was not an indictment of static typing. I rather enjoy static typing. I&rsquo;ve spent the last 30 years working in statically typed languages and have gotten rather used to them.<\/p><p>My intent, with that blog, was to complain about how far the pendulum has swung. I consider the static typing of Swift and Kotlin to have swung too far in the statically type-checked direction. They have, IMHO, passed the point where the tradeoff between expressiveness and constraint is profitable.<\/p><p>[&#8230;]<\/p><p>You might therefore say that the type system is a kind of &ldquo;test&rdquo; that fails for all inappropriate invocations of <code>f<\/code>.  I might concede that point except for one thing &#8211; <em>the way <code>f<\/code> is called has nothing to do with the required behavior of the system<\/em>.  Rather it is a test of an arbitrary constraint imposed by the programmer.  A constraint that was likely <em>over<\/em> specified from the point of view of the system requirements.<\/p>\n<p>[&#8230;]<\/p>\n<p>So what the type system is checking is not the external behavior of the program. It is checking only the internal consistency of the program text.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Robert C. Martin (Reddit): My problem is that both [Swift and Kotlin] have doubled down on strong static typing. Both seem to be intent on closing every single type hole in their parent languages.[&#8230;]But before you run out of fingers and toes, you have created languages that contain dozens of keywords, hundreds of constraints, a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-02-14T15:43:06Z","apple_news_api_id":"4278e45c-cb57-42d9-b441-b0fc3449c109","apple_news_api_modified_at":"2019-02-14T15:43:07Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AQnjkXMtXQtm0QbD8NEnBCQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,71,901,268],"class_list":["post-16906","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=16906"}],"version-history":[{"count":7,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16906\/revisions"}],"predecessor-version":[{"id":24298,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16906\/revisions\/24298"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}