News for hackers. Stuff that matters.
Re:Static typing will not save us from broken software by natec425 on Sunday October 23, 2016 @01:44PM (#12774503)
I've started to feel the same way. I feel like a lot of developers would be better off in a dynamically-typed language because it is such common practice to completely subvert the type system and there are such strong resources available for improving testing practices.

My last job was in C#, and it seemed like so many things were stringly typed, entirely nullable, or used something else to weaken the type system.

In addition to this failure to use the type systems, testing is becoming a much more rich environment. I think popular resources on effective testing are much better and numerous than popular resources on effective typing. There are great books and tools around helping developers test their software more thoroughly and efficiently. Comparing these testing resources with typing resources, the closest popular book I can think of for typing is Design Patterns, and I don't think most popular type systems are built with the program correctness as a top priority.

I think rich type systems like haskell's will play more of a role in the future, and I think current users of these systems do experience a benefit. But if you have only ever done dynamically typed programming or used Java/C#, you are probably going to improve your correctness more over the next year by using a property based testing tool like propr (ruby), hypothesis (python), fscheck (.net), scalacheck (jvm), or jsverify (js), spec (clojure).

Re:Static typing will not save us from broken software by d357r0y3r on Sunday October 23, 2016 @02:09PM (#12774626)
> My last job was in C#, and it seemed like so many things were stringly typed, entirely nullable, or used something else to weaken the type system.

This sounds like C# as written by people that don't really know or like C#. If you're stuffing everything into strings or a dynamic, you missed the point of the type system in a very serious way.

> In addition to this failure to use the type systems, testing is becoming a much more rich environment. I think popular resources on effective testing are much better and numerous than popular resources on effective typing. There are great books and tools around helping developers test their software more thoroughly and efficiently. Comparing these testing resources with typing resources, the closest popular book I can think of for typing is Design Patterns, and I don't think most popular type systems are built with the program correctness as a top priority.

There is some overlap between problems that can solved by type systems and test suites, but there are many problems that can only be caught by one or the other.

It also strikes me as strange that people can't be bothered to use the type system provided by the language, but they can be bothered to maintain and add unit tests for their programs. Correctness enforced by types is fused with the code; if you get the types right, you should be all set. There's no additional maintenance beyond making sure that the code compiles.

Re:Static typing will not save us from broken software by natec425 on Sunday October 23, 2016 @02:31PM (#12774734)
> This sounds like C# as written by people that don't really know or like C#. If you're stuffing everything into strings or a dynamic, you missed the point of the type system in a very serious way.

I agree, but I think it is the reality. Software development is a job, not divine appointment. And assuming it is reality, I think the best short term gain those developers isn't doubling down on type systems.

> It also strikes me as strange that people can't be bothered to use the type system provided by the language, but they can be bothered to maintain and add unit tests for their programs. Correctness enforced by types is fused with the code; if you get the types right, you should be all set. There's no additional maintenance beyond making sure that the code compiles.

I'm totally with you. I remember a friend being so impressed when a hypothesis test caught a bug in an example that I set up. The funny thing was that I made the example from a function over an F# Union. A lot of people seem to get super irritated by compiler errors but seem to be really impressed by clever testing. I think it has to do with the error messages. Property based testing frameworks tend to show you the simplest concrete example of your error. Type checkers tend to show you the most abstract example of your error. Maybe if type checkers started to to incorporate more concrete examples of error, people would generally find them more appealing.

Re:Static typing will not save us from broken software by rntz on Sunday October 23, 2016 @04:18PM (#12775222)
> Property based testing frameworks tend to show you the simplest concrete example of your error. Type checkers tend to show you the most abstract example of your error. Maybe if type checkers started to to incorporate more concrete examples of error, people would generally find them more appealing.

I think this is a really important insight! And in fact, there is some recent research about making type checkers produce counterexamples: http://lambda-the-ultimate.org/node/5355. Ergonomics of type systems is an unfortunately understudied area.

Re:Static typing will not save us from broken software by cle on Sunday October 23, 2016 @02:40PM (#12774774)
There is a real cost to properly modeling your domain in the type system. You have to conform with the constraints of the type system, there is higher cognitive overhead, it can cost more up-front (which can cause missed deadlines), etc.

Also, there are cases where using strings is more appropriate than using enums. Where passing a blob is preferred over guaranteeing the correctness of the blob's structure. Where "null" really is better than an Option type.

And conversely, there are companies who hire crappy developers or don't give their good developers enough time to learn the idioms and best practices of their tools.

Re:Static typing will not save us from broken software by KMag on Sunday October 23, 2016 @08:35PM (#12776127)
"Dynamic Witnesses for Static Type Errors* ("or ill-typed programs usually go wrong)"[0] by Eric Seidel et. al. discusses a prototype for a system that searches for concrete examples of ill-typed inputs that break a program, in order to aid novice programmers with type errors at compile time.

[0]http://eric.seidel.io/pub/nanomaly-icfp16.pdf

Re:Static typing will not save us from broken software by dllthomas on Monday October 24, 2016 @12:47PM (#12780499)
That is really cool! When will it be in GHC? :D
Re:Static typing will not save us from broken software by dllthomas on Monday October 24, 2016 @12:42PM (#12780439)
> there is higher cognitive overhead

In my experience, the opposite is the case. If I can offload some checks to my tooling, I don't have to be constantly performing them mentally. And with type inference at a repl, I can ask about the shape of things that can't possibly run yet (though I don't think I get that with C#).

Re:Static typing will not save us from broken software by wereHamster on Sunday October 23, 2016 @03:47PM (#12775107)
> Where "null" really is better than an Option type.

WAT? Give me an example.

Re:Static typing will not save us from broken software by cle on Sunday October 23, 2016 @04:09PM (#12775188)
performance
Re:Static typing will not save us from broken software by tome on Sunday October 23, 2016 @04:22PM (#12775243)
There's no reason it shouldn't be a typed null though. It doesn't have to be a tagged Option type.
Re:Static typing will not save us from broken software by cle on Sunday October 23, 2016 @04:58PM (#12775395)
I'm talking about practical reasons to use null.

Theoretically it's not necessary, but given real-life constraints, there are most definitely times when null is more appropriate than e.g. Java's Optional.

Re:Static typing will not save us from broken software by tome on Sunday October 23, 2016 @06:07PM (#12775655)
I agree! I'm just saying there's no theoretical reason for null not to be typed (which is quite different from Optional).
Re:Static typing will not save us from broken software by cle on Sunday October 23, 2016 @07:43PM (#12775975)
Then we agree :). From a correctness perspective, nulls are awful.

My point was not specifically aimed at typed nulls and compiler optimization, it was that there will always be practical constraints that force us to compromise static guarantees in order to get things done in a reasonable amount of time. As software engineers, our problems are first practical before they are theoretical.

Re:Static typing will not save us from broken software by premium-concern on Sunday October 23, 2016 @05:19PM (#12775480)
That's an extremely weak argument. Java's Optional is broken and should never be used.
Re:Static typing will not save us from broken software by cle on Sunday October 23, 2016 @07:37PM (#12775951)
If that is true, then it's another example supporting my point.
Re:Static typing will not save us from broken software by premium-concern on Monday October 24, 2016 @10:34AM (#12779116)
No. Java messing things up as usual, doesn't have any broader impact on the usefulness of the concept.

Error handling with null is wrong and broken, regardless of whether the null is "typed" or not.

null might have some very limited other uses, but they will never compete with those structures designed for error handling, because they do something fundamentally different.

Re:Static typing will not save us from broken software by cle on Monday October 24, 2016 @12:01PM (#12779998)
My argument is practical, not theoretical. Platitudes and ideals are disposable when you have to actually build something.
Re:Static typing will not save us from broken software by premium-concern on Monday October 24, 2016 @04:27PM (#12782557)
Yawn.

Java is the platform with NPEs and null checks everywhere. If that's your definition of practical, sure, go ahead.

Re:Static typing will not save us from broken software by dllthomas on Monday October 24, 2016 @03:44AM (#12777232)
Could you expand on this?
Re:Static typing will not save us from broken software by premium-concern on Monday October 24, 2016 @10:40AM (#12779190)
Optional#map is implemented incorrectly.

   someOptional.map(a).map(b) NOT EQUAL TO someOptional.map(a.andThen(b)
Re:Static typing will not save us from broken software by dllthomas on Monday October 24, 2016 @11:02AM (#12779410)
Ah, that's unfortunate.

Because of the order in which side effects occur, or is there something else as well? (I haven't Javaed in anger since before Optional).

Re:Static typing will not save us from broken software by premium-concern on Monday October 24, 2016 @05:04PM (#12782883)
Consider two functions (excuse my Scala):

  scala> val str: Function[String, String] = s => if (s.length > 3) s else null
  str: java.util.function.Function[String,String] = $$Lambda$1369/859456754@2c668c2a

  scala> val num: Function[String, Integer] = s => if (s == null) -1 else s.length
  num: java.util.function.Function[String,Integer] = $$Lambda$1376/1887041776@121cf6f4
With Optional, you receive different results depending on whether you call map twice, or combine the functions first:

  scala> Optional.of("Foo").map[String](str).map[Integer](num)
  res12: java.util.Optional[Integer] = Optional.empty

  scala> Optional.of("Foo").map[Integer](str.andThen(num))
  res15: java.util.Optional[Integer] = Optional[-1]
This is incorrect. (They decided to disallow null as Optional's value for whatever insane reason.)

It also leads to all sort of weird usability issues: The old "`someMap.get(...)` returned null ... now does that mean the entry doesn't exist, or the entry exists and it is null?"

In fact they are currently working on special hacks to Java 10's Generics, because they realized that important collection classes don't work well with specialization, and even their new Optional can't save them. (Not directly related, but a good example how design mistakes combine and cause even more pain down the road.)

Re:Static typing will not save us from broken software by dllthomas on Monday October 24, 2016 @07:26PM (#12783831)
Interesting, thanks!
Re:Static typing will not save us from broken software by DRMacIver on Sunday October 23, 2016 @01:51PM (#12774547)
FWIW, rantly (https://github.com/abargnesi/rantly) rather than propr seems to be the most active and usable Ruby property based testing library.

(I maintain a list here: http://hypothesis.works/articles/quickcheck-in-every-languag...)

Re:Static typing will not save us from broken software by LeonidasXIV on Monday October 24, 2016 @06:23AM (#12777733)
For OCaml I'd argue that the canonical QuickCheck is https://github.com/vincent-hugot/iTeML (aka qtest). It looks like it is a unit test tool (which it also is), but it has merged in QCheck which I have used before.
Re:Static typing will not save us from broken software by natec425 on Sunday October 23, 2016 @03:11PM (#12774926)
Ah. Thank you for the links. Also, thanks for hypothesis.
Re:Static typing will not save us from broken software by FLUX-YOU on Sunday October 23, 2016 @02:28PM (#12774712)
>My last job was in C#, and it seemed like so many things were stringly typed, entirely nullable, or used something else to weaken the type system.

I see this happen when a bad database design was born before the C# code, e.g. varchar NULL types everywhere.

Re:Static typing will not save us from broken software by natec425 on Sunday October 23, 2016 @03:09PM (#12774908)
That does happen, but I think the issue is more with education around using types. A lot of people nowadays spend a few years in python/js/java/etc before they have the chance to spend time in a community that really embraces types as the fundamental design tool. Also, it is hard to see the benefits of good typing if my first statically typed language is Java and I'm constantly told to cast into and out of Object. So, I put a lot of blame on the way people are taught to program. People tend to have a pretty hard time uprooting years of experience.
Re:Static typing will not save us from broken software by nsomaru on Sunday October 23, 2016 @03:36PM (#12775049)
Could you offer a learning path for one with experience in js/python?
Re:Static typing will not save us from broken software by natec425 on Sunday October 23, 2016 @05:32PM (#12775534)
I don't know if i could offer a "path", but here are a few ideas to try out:

1. Start using hypothesis or jsverify. 2. Play around with Elm. 3. Participate in a community that highly values types.

## Properties vs Types

Just to explain a bit, types and properties are about program correctness. So they serve the same purpose for us. Types are essentially properties that your type checker can prove for you. Sometimes it is hard or impossible to prove something, so you might reach for property based tests to "approximate" the proof.

So when I'm thinking about my code correctness: 1. write a type where I can 2. write a property where I want to write a type 3. write examples to keep me grounded

Property based testing frameworks will help you move up that ladder.

## Elm

Of course, it will also be helpful to start actually leveraging a type system. I think Elm is a good option for people who are new to using types this way. It has a powerful, but relatively simple, type system that can help you learn good type design without having to deal with all of the features that Haskell/Scala/Rust/Ocaml/etc would have. Also, it has pretty great compiler error messages. So it should be less intimidating than a lot of nearby options.

## Community

Community might be the most important element. Participating in a community that values types (again, I suggest Elm) will be a great investment. Downloading Elm and a property based testing library could be a recipe to feel overwhelmed. Be sure to have a slack channel/mailing list/irc channel ready to help you when you start to feel overwhelmed.