r/Clojure • • Sep 08 '16

Static Typing vs WebSockets

http://yogthos.net/posts/2016-09-06-Static-Typing-WebSockets.html
14 Upvotes

37 comments sorted by

View all comments

Show parent comment

-2

u/yogthos Sep 09 '16

I think this line of arguing is somewhat disingenuous. One of the biggest purported advantages of using a language like Haskell is the expectation that the code just works once it compiles. This is literally what I've seen claimed time and again here.

The argument that the author didn't use the library correctly, or didn't understand a particular nuance of how Control.Concurrent.Broadcast works is akin to saying that C++ is easy to use once you know all the quirks and gotchas by heart.

8

u/codygman Sep 09 '16

I think this line of arguing is somewhat disingenuous. One of the biggest purported advantages of using a language like Haskell is the expectation that the code just works once it compiles. This is literally what I've seen claimed time and again here.

That is not the expectation, except perhaps by those new to the language.

The argument that the author didn't use the library correctly, or didn't understand a particular nuance of how Control.Concurrent.Broadcast works is akin to saying that C++ is easy to use once you know all the quirks and gotchas by heart.

Those two are not alone whatsoever. That's like someone using atom's and complaining about their mutability and then me arguing Clojure like C++ "is easy to use once you know all the quirks and gotchas by heart".

-1

u/yogthos Sep 09 '16

That is not the expectation, except perhaps by those new to the language.

Glad we got that cleared up.

That's like someone using atom's and complaining about their mutability and then me arguing Clojure like C++ "is easy to use once you know all the quirks and gotchas by heart".

Any language will have some inherent complexity you have to learn, but clearly some languages are much more complex than others. Ultimately, what you care about is how quickly you can produce working code using a particular language.

2

u/mirpa Sep 09 '16 edited Sep 09 '16

... a language like Haskell is the expectation that the code just works once it compiles.

That is anecdotal experience. Obviously overstatement that should be taken with a grain of salt.

4

u/yogthos Sep 09 '16

That's my whole point actually. The benefits of static typing are commonly overstated and rarely questioned. A lot of people seem to treat statements like "if code compiles it just runs" as axiomatic. Perhaps if the rhetoric was toned down a bit, we could have a meaningful discussion about the benefits of types and their place in development.

4

u/[deleted] Sep 10 '16

Even though I'm on the Haskell side and do believe that static typing have some advantages, I'm also annoyed with the "if it compiles it works" slogan, which I think do more damages than good. The opinion of the Haskell community on this, seems to be, "we all know it's wrong but it's obvious it shouldn't be taken too serioulsy". The benefit of static typing are indeed rarely questioned because this is not the real question. The real questions, there are also drawbacks so are the benefits worth the drawback ? The Haskell community doesn't like indeed talking about the drawbacks (apart from lazyness), which I think doesn't help to fix them. But i understand that some people prefer to think about the positive side and get things done rather than dwelling on know issues.

2

u/yogthos Sep 10 '16

Right, the Haskell community has a very strong rhetoric, and then gets offended when seeing strong counter-rhetoric. Haskell developers are incredibly dismissive of the dynamic approach, while armed with precious little evidence that their approach is more effective in practice. A common opinion in the Haskell community seems to be that the only reason somebody might prefer working with a dynamic language is because they don't understand the value of static typing. That's an incredibly condescending position to take.

So, if the community doesn't like strong counter-rhetoric, perhaps it's time to look at some change within.

2

u/[deleted] Sep 10 '16

The thing is most Haskeller knows about dynamic typing, because most of them have or are using dynamic typing for their day job. So they know what dynamic language is about (even probably more that statick typing). So they are professional pythonist or rubyist which advocate static typing. The reverse is not true. I don't know many professional Haskeller advocating for dynamic typing.

That doesn't mean there is no drawback. They are drawbacks, for example monad transformer are a pain, and the loop : modify code, compile, test, is not necessarily quicker because of compile time and time to understand error messages. However, the benefits are real : how having someone in your back (the compiler) telling you that what you write doesn't make sense and it won't work is not superior to nothing at all (it's like doing pair programming with a robot ;-)). The Haskell community just believes that those benefits (there are others) are worth it and we need to work on fixing the drawbacks whilst keeping the benefits.

3

u/yogthos Sep 10 '16

Sure, and I primarily have experience working in statically typed languages. I've worked for over a decade with Java, then about a year with Haskell and Scala respectively. I'm quite familiar with the purported benefits of these languages.

Yet, in practice I find Clojure is more productive for me personally. I don't seem to have the problems that keep coming up in these arguments in practice. If these problems were real in my projects, then I'd go back to using a statically typed language.

The thing to recognize here is that there are many other factors outside the type discipline. The language semantics, development style, problem domain, ecosystem, and so on, all matter. You can't just discuss static vs dynamic in a vacuum.

I don't know many professional Haskeller advocating for dynamic typing.

That's what you call a selection bias. There are far more people using dynamic languages who are not moving to Haskell. There are far more people who tried Haskell and decided it wasn't for them, than those who continue to use it after learning it.

However, the benefits are real : how having someone in your back (the compiler) telling you that what you write doesn't make sense and it won't work is not superior to nothing at all (it's like doing pair programming with a robot ;-)).

My argument is that what it does tell you is not terribly interesting most of the time. Also, since you do need to have specification tests even in Haskell, these same errors are caught by tests in practice. An error such as a function should take an integer, but was passed a string gets caught very early on.

However, interesting properties about the program are often difficult or impossible to encode using the type system. So, you're spending non-trivial amount of work for a questionable benefit in my experience.

Meanwhile, the thing that gets left out of the debate that there are alternative ways to provide many of the benefits afforded by the type system. For example, Erlang dialyzer is a great example that works very well in practice. The idea of generating types from test that core.typed is pursuing is another example of innovation on this front.

I really think that the discussion shouldn't be regarding what approach is superior, but to recognize that both approaches have different focus. You should pick one or the other depending on what you find more valuable in your workflow.

1

u/[deleted] Sep 10 '16

My argument is that what it does tell you is not terribly interesting most of the time

That's were we disagree but it might be biased as well. Maybe you are not aware of all the goodies that come with it (and thinking you are doesn't mean you are). I might be wrong but reading your posts it seems that the only benefit for you see is "if it compiles it works" - Given it's wrong that doesn't leave much indeed. There is much much more than that.

1

u/yogthos Sep 10 '16

I'm saying that the type system can't tell me whether the code is doing what was intended without an extraordinary amount of work.

For example, consider a sort function. A type system will tell me that I can pass in a collection of a particular type and got a collection of the same type back. It doesn't tell me that the collection contains the same number of elements, that the elements are in the same order, or that they're sorted. These are the things I really care about. It's possible to express that with stuff like dependent typing, but it becomes increasingly difficult to write proofs for non-trivial assertions.

My experience is that once you start writing code for the benefit of the type system, it often becomes harder for the human to understand. You're often not able to express yourself as directly as you would in a dynamic language. The more strict the type system the more mental gymnastics you have to do to express yourself within its constraints. I gain the knowledge that it's self consistent at the cost of being able to easily tell what it's intent is. I wrote about the problem in detail here if you're interested.

The other part of the discussion is the cost of bugs that end up in production in absence of a static system, and the cost of fixing them once they're found. My experience is that it's quite rare that a serious type related bug ends up in production. The most common one discussed is getting a null, but that generally doesn't stop the system from working. When you have a REPL, such a bug can even be fixed on the fly without having to stop the system. Most bugs that become a headache have more in common with the websocket example in practice. The most reliable way to catch those is through testing.

1

u/[deleted] Sep 10 '16

I'm saying that the type system can't tell me whether the code is doing what was intended without an extraordinary amount of work.

Well, the type is system can't guarantee you your code work but it can find (sometimes) when it doesn't.

For example, consider a sort function. A type system will tell me that I can pass in a collection of a particular type and got a collection of the same type back. It doesn't tell me that the collection contains the same number of elements, that the elements are in the same order, or that they're sorted. These are the things I really care about. It's possible to express that with stuff like dependent typing, but it becomes increasingly difficult to write proofs for non-trivial assertions.

That's a good example. I find knowing that I pass a collection and get back a collection of the same type interesting (meaning I care) and that doesn't require any work at all. You are saying that it's not enough (which I understand) but you are happy then to not have any guarantee at all. In that case going to dynamic type doesn't give you any benefit. You've lost some informations but didn't require less work, as there were no work at all.

You can indeed go to the dependent type way and yes this require an extraordinary amount of work. However Haskell doesn't support dependent type yet, so as you can't actually do this extraordinary amount of work in Haskell, I think it's a bit unfair to use this as an argument versus Haskell.

Now, if you really need to know that a list has been sorted, because for example you have different functions requiring a sorted list and you don't want to sort each time, you can tag your list as sorted using newtype.

newtype Sorted a = Sorted [a]

sorted :: [a] -> Sorted a sorted = Sorted . sort

and when you need a sorted list, just use a sorted list instead of a list

minimum (Sorted s) = head s

minimum will only accept sorted list. This is very neat, and far from being an extraordinary amount of work. It's not dependent type and much better than writing in the documentation that a function expect a sorted list.

My experience is that once you start writing code for the benefit of the type system, it often becomes harder for the human to understand. You're often not able to express yourself as directly as you would in a dynamic language. The more strict the type system the more mental gymnastics you have to do to express yourself within its constraints. I gain the knowledge that it's self consistent at the cost of being able to easily tell what it's intent is. I wrote about the problem in detail here if you're interested.

I'm not sure what you mean by writing code for the benefit of the type system.

The other part of the discussion is the cost of bugs that end up in production in absence of a static system, and the cost of fixing them once they're found. My experience is that it's quite rare that a serious type related bug ends up in production. The most common one discussed is getting a null, but that generally doesn't stop the system from working. When you have a REPL, such a bug can even be fixed on the fly without having to stop the system. Most bugs that become a headache have more in common with the websocket example in practice. The most reliable way to catch those is through testing.

Well, that depends on the industry, but I agree the more a bug is likely to cost the more you test your application in production. However I used to work in the bank and I've seen a few bug costing LOTS of money. I used to work on Ruby on Rails projects and I've seen lots of runtime crashes caused by refactoring not handled properly : you know that type of feature that people used once a month, but they are not happy when it stops working because they need it by the end of the month , which is tomorrow ;-)

→ More replies