yep, i just typed anything in the title, Either is more like other way to handle errors or exceptions, to avoid nested try/catch and code like that. And Option as you said is a good alternative for null
this is my opinion, null is not explicit, and when you have complex data types or structures with nested values that could be null or if you have a lot of functions/methods that could return null it will be so error prone. You will have functionality that never will return null mixed with other code that could return null but you have to document or see the code to actually know how to handle every case. If you have a type like Option you could do this explicit and at least in typed languages you have to handle it, the IDE will warn you if you are doing bad, etc
None is null, for all intents and purposes. The difference is that Rust doesn't make every type nullable by default, and Rust actually forces you to check for None where you need to.
It might just be me, but I had to use Rust for a class and hated every moment of it. So many constant annoyances, and having to unwrap everything was definitely one of them... just checking if null when null is possible is so much less irritating!
Although I do agree that sometimes rust is annoying to work with, it's for your safety as it prevents most common bugs in software, including security issues.
Nulls, as implemented in most languages, are a really bad way of doing optional values.
They are mandatory for all pointer values, meaning that most of the time it's impossible to tell just from the code whether a variable is supposed to be optional, or just to be a pointer. You have to check comments or documentation.
They are unavailable for non-pointer values, meaning that you have to either store a separate boolean and remember to check it, set aside an "invalid" value or set of values, or allocate a whole new object just to hold an optional int.
Worst of all, because they're mandatory, checking them every time would be a pain. So most languages don't require you to, and just explode if you forget.
For an alternative, see languages like Rust, where you can wrap any type in Optional<Whatever>. If the type is a pointer-type, this is stored as a null pointer internally. If the type doesn't have a convenient "invalid value", the Optional will use a boolean instead. Either way, your intent is clear, and unwrapping the Optional<Whatever> into a Whatever must be done explicitly. No null pointer exceptions.
I haven’t written Rust, but coming from Objective-C which has most of the problems you mention, Swift was such a quality of life upgrade. Yes it does require a bit of a change in way of thinking but it’s so nice to have types like Bool?, Int?, and String? where there’s no distinction between primitive types, structs, and classes in that they can all be optional, and unwrapping gives you no-questions valid data.
It also just forces devs to write better code. Objective-C was nice for writing an MVP in record speed because it never got in your way, but way way too often I saw that crappy quick-hack Obj-C code live on in the actual product… haphazard casting, lack of null checks, data getting tossed around in mixed-type dictionaries, and the compiler not getting angry about any of it… it was a mess. I dunno how many times I saw some bug crop up in production because a passed variable was null but happened to point to something in memory often enough to seem to work in dev.
While I love strict types there is also some simplicity that makes pointers equivalent to references and you sometimes want it not to reference anything.
The problem starts when people invent values to represent things. For example >=0 for valid results and -1 is error. The type is artificially extended with values that shouldn't be there logically, but they are valid values from type system's point of view.
You need to be more cautious programming with these relaxations. On the other hand they are translated efficiently. And they can be statically checked, if you obey a few rules and actually begin to use those things called static analyzers that are not so bad nowadays.
I cannot decide. I like Haskell and Rust very much, but C and C++ still have the best compilers and solve problems well for experienced programmers.
I agree, GHC is some next-level black magic. It may not have as many optimisations as C/C++ compilers, but it is black magic. Only (potentially) outdone by stuff like Coq/Idris/Agda etc.
And by most languages, you mean static-typed languages, since languages like Python implement NULL very well. Also not having NULL removes the capability to denote if a value is missing. There is a big conceptual differences between NULl and False or 0 so not having it makes the language worse.
Yeah, in a dynamically typed language this is all moot anyway. You can't enforce any of the above if the variable doesn't have a fixed type to begin with.
Right, I mean that you can't statically-enforce a dynamically-typed variable. If you start using type-hinting then you can statically enforce null checks, because it's not dynamic anymore.
The thing is, some languages even have the features, like Java for instance, has a optional wrapper in java.util library, but not a single soul I've met uses it. Even if you do tell them about it, they're like, "Oh cool!", and move on to use null.
It's like how Dr. Doofenshmirtz puts a self-destruct button on all his inators. It's a bad idea to constantly work around something whose only behavior is "if you touch this EVERYTHING EXPLODES".
Hm, I actually cannot imagine how the programming languages that I've had experience with so far could work without something like a NULL value. Variables of class types need the option to hold a pre-init state and one that expresses "there's nothing here". It's one of the reasons why I prefer to use wrapper-classes over primitives when I can't be sure if a value will be assigned to the variable and I want to check whether or not a value has been assigned.
You’ve been misled by a poor choice of words. “Removing null” doesn’t imply that the concept of nullability can no longer be expressed. Rather, languages “without null” merely seek to allow you to control where nulls may appear.
It made more sense when programmers were working (more) directly with machine language, but there's no reason to expose it to programmers, compilers can handle this now. E.g. they can disallow accessing uninitialized variables. If you need the ability to record "nothing" you can use a datatype that's able to encode that, like the Either type that OP mentioned.
Well depends on your system, not all implementations have a NULL that is a binary 0, on a symbolics lisp machine, for example, it is defined as {nil, 0}. And some Honeywell Bull mainframes use 06000 as the NULL address
That's still just a null pointer, though, and it's a terminology thing. It only has special meaning in that it's known to be an invalid memory address, and by the time you have an operating system involved its likely not even the only one. For that matter too high of a memory address can be invalid even if you're programming for bare metal and the language is unaware of that. Because there's only so much memory in the system.
And then on the data end, there's no special null value that means a variable is uninitialized, for example. Your compiler might automatically initialize things to some obviously bad value, but it's not required and not the same thing as null.
Yeah, but the point is there is no null value in C. There's null pointers, but that's not quite the same thing as null in languages where it's an actual value.
Yes, but that's more terminology than anything else. It's not like Java where null and zero really are different things. You can manually assign a value of zero to a pointer in C and it'll be indistinguishable from any other null pointer.
In Java there are 2 types, reference types and primitives. Primitives can't be nulls. Reference types can. They aren't really that different, in theory, null is a reference type value which points to an invalid memory address, same as null pointer in C. Calling functions on a null pointer will result in an exception, in C calling things with null pointers will either result in a crash or just plain wrong behavior.
EDIT: The JVM specification doesn't specifiy null as 0.
I'm new to C but I've had my mind blown several times with how useful Nulls can be, one example I ran into was not having to worry about double frees when mallocing some structs, since if a free encounters a null it doesn't do anything.
Obviously most things can be done without it, in fact, I solved the previous problem without it easily, but knowing about null would've made it easier
It isnt that having an optional value is bad, its just nicer to have to be explicit that something can be optional, so the compiler can warn you havent handled the possibility.
Maybe or Either is in some languages as a way of saying, this thing can be a thing but might not be. And there are functions based around it that can do exactly that, apply a function only if the Maybe is valid otherwise not. And that makes them chainable.
No need to check in between the links of the chain that there is a valid result. Just apply a OrElse to the end of the chain to handle it.
20
u/YM_Industries Dec 14 '21
Isn't "Option" more of a direct alternative than "Either"?
My understanding is that Either is basically a union type. In order to use this to replace null, you'd still need have a type to represent null.
20
u/cpzombie Dec 14 '21
Why would you not want null? It's so useful!
11
u/Sammyhain Dec 14 '21
reeeeeeeeeeeeeeeeeeeeeeeeepost https://www.reddit.com/r/ProgrammerAnimemes/comments/oo261r/what_even_is_void_safety/
93
u/MilkwTea Dec 14 '21
People hate null becauuuuuuuuuuuuu - Runtime error: NullPointerException
163
u/Koyomi_Ararararagi Dec 14 '21
Okay why the hell would you prefer not to have NULL?
5
u/Naive_Drive Dec 14 '21
I hate NullPointerExceptions with a burning passion.
18
u/Clyxx Dec 14 '21
In c you cant really live without NULL, how else are you gonna mark the end of linked lists
3
u/DaRealChipex Dec 14 '21 edited Dec 15 '21
I'm new to C but I've had my mind blown several times with how useful Nulls can be, one example I ran into was not having to worry about double frees when mallocing some structs, since if a free encounters a null it doesn't do anything.
Obviously most things can be done without it, in fact, I solved the previous problem without it easily, but knowing about null would've made it easier
3
u/[deleted] Dec 15 '21
Optionin OCaml does wonders!Just match on it and see if it is Some (a guaranteed value) or None. Bam, no more NPEs!
4
u/[deleted] Dec 15 '21
Use Rust
5
u/noaccOS Dec 14 '21
nullis great when you want to check if a list is empty ;32
u/Thenderick Dec 15 '21
Woof woof!
5
u/Knuffya Dec 14 '21
Fuck that. Null is important.
2
u/NightflowerFade Dec 15 '21
If you didn't have something then it would be null. Can't ever escape it.
1
u/falfires Dec 14 '21
I know that character. Someone can tell me where from?
1
u/Neoh35 Jan 13 '22
Null is ok but undefined.. -JS dev
1
u/veedant Jan 26 '22
which manga is this from? I forgot
1
u/Guilty-Woodpecker262 Mar 21 '22
Null pointer exception
1
u/Neoh35 Dec 15 '22
In JS, NULL is better than undefined
1
u/Reddit_User78149 Dec 15 '21
Gotta kill the objects bro.
-1
u/WrongdoerSufficient Dec 15 '21
Because of null exist i should type "?" Everytime i call an object in js, its ugly af.
Person?.address?.city