upvote
This was always the same thing that Unit tests were trying to bring, defend against your own errors and try to get the most quality out of the tools as possible. A lot of language progress has been exactly this over the years, from removing goto and introduction of for/if/while to constraining types and everything since. As an industry we could do with more of this mindset in general, just use everything we can to try and catch bugs earlier when they are cheaper to find and fix and avoid them happening at all.
reply
I have always had a love/hate relationship with unit tests. I love them for all the obvious reasons. I hate them for all the times I read a test suite where 80% of the tests could have been eliminated by just improving the design of the system under test so that error condition wasn’t possible in the first place.
reply
Null should not exist, and Optionals are an anoying workaround/hack to it. Old languages like Tcl eliminated it, (every object has an initial value, it is never null).

It creates an interesting problem, as sometimes you want to know the absence of value. Usually done with the empty "" string.

The other way to handle is that every Object is initiated (pointing) to an starter initial value. (prototype based languages).

reply
> Optionals are an annoying workaround/hack to it

Sometimes you do want to represent the concept of “this whole datatype OR nothing”. How do you suggest this is modelled? I think optionality is pretty fundamental. What’s wrong about classical null is that it ascribes optionality to all types regardless of whether they need it.

Being forced to represent optionality with a “special” default is terrible design IMO - see the recent SSH RCE on Musl, largely due to the poor choice of indicating “crypt failed”. Default values can be quite dangerous. Sometimes the domain you are modelling supports such a value, but having “default value” be the default (and not a choice) is a similar evil to default null.

reply
1. Have declared nullability (e.g. `String? foo` / `val foo: String?`)

2. Use null coalescing (e.g. `foo?.bar?.baz`)

Languages such as Kotlin and C# support this style o programming.

The thing that I don't like about Optional types (in e.g. Scala or Java) is that they are too verbose. For example, instead of:

    return foo?.bar?.baz ?? 0
you have something like:

    return foo.flatMap(_.bar).flatMap(_.baz).getOrElse(0);
This is even harder to read if you have more complicated logic, requiring implicit lambdas and other machinery.
reply
Both variants are just different syntax for the same thing.
reply
I don't see how initial values solves for much, or how an Optional constitutes a workaround; if anything, I see initial/null values being a hack to get around Optional, which is an explicit declaration of what possibilities exist.

Initial values, like null, is an attempt to smuggle the "unset" case into the type without declaring it. And like null, the defensively written logic should prefix every single usage with a check for validity.

Optional is just a natural subset of the generalization that multiple possible states exist; in this case, just the two, and sum types to properly handle bigger sets.

Initial/null never admit more than 2 possible states, and also believe everything is always in one of those 2 states.

reply
"Always has a value" just creates an in-band signaling problem, where the "absense of value" overlaps with the valid values.
reply
yes i love rust because i acknowledge my own limitations as a programmer. compare that to c/zig devs handwaving away vulnerabilities as "skill issues", any serious engineering discipline eliminates the possibility of human error wherever possible.

fwiw john carmack agrees with me

reply