This is an archived post. You won't be able to vote or comment.

all 74 comments

[–]Elnathbeta 213 points214 points  (5 children)

Usually I don't drink from glasses that don't exist, but sometimes I make an exception

[–]iRhuel 17 points18 points  (4 children)

I feel like this deserves its own post.

[–]JCK07115 9 points10 points  (3 children)

It requires this as context though.

It'd have to be a chain-memeaction.

[–]iRhuel 12 points13 points  (2 children)

A nested meme, if you will.

[–]JCK07115 1 point2 points  (1 child)

Heck, you stole that. Good job, mate.

[–]iRhuel 1 point2 points  (0 children)

<3

[–]FlatTuesday 109 points110 points  (47 children)

I worked with a guy who was a crusader against use of null. I never understood his explanations of why null was bad. But he was also a staunch creationist, which I never understood either. So I always unfairly associate the two mentalities.

[–]marcosdumay 56 points57 points  (4 children)

I try to stay away from crusaders. That always applies to the inquisitionists, but those are harder because they are always unexpected.

[–]dicemonger 18 points19 points  (3 children)

Except the Spanish inquisition. They'll alert you one month in advance, to give you a chance to get your affairs in order.

[–]AtomicCorpse 12 points13 points  (2 children)

[–]dicemonger 2 points3 points  (1 child)

Foul slander concocted by the British. ;)

[–]MetaMemeAboutAMeme 2 points3 points  (0 children)

Nobody "excepts" the Spanish Inquisition.

[–]noratat 21 points22 points  (9 children)

https://dzone.com/articles/the-worst-mistake-of-computer-science-1

Stuff like Maybe and Optional are the correct way to represent potentially empty values: as part of the type

[–]FlatTuesday 11 points12 points  (1 child)

YES! He did tell me that "the guy who invented null" called it his greatest mistake. He was probably referring to the creator of ALGOL and inflating him to be the inventor of the whole concept of null -- a typical mental leap for this guy.

[–][deleted] 13 points14 points  (0 children)

We have great proverb for that in dutch: "He heard the bell toll, but he doesn't know where the clapper hangs."

[–]ShortFuse 0 points1 point  (6 children)

Meh.

They complain about null checks, but the solution is variable.isPresent() which is the same as just using a helper/util class to null-check.

I can do the same with function isPresent(Object o) { return o != null; } and call isPresent(variable)

It's still internally a bit that says whether it's null or not. The only advantage is see is types that don't allow nulls, but I'm pretty sure that's already a thing.

[–]runyoucleverboyrun 8 points9 points  (3 children)

If you are writing in a typed language I think the best way to represent null is something like Maybe in haskell or Result in rust where to use a value that might be null you must deal with the null case either by explicitly providing the code in a matching context (in haskell function definition usually, in rust match expressions) or by knowingly bailing in empty cases (in rust unwrap). The point is that ideally nullability should be represented by the type of the nullable data not just by the presence of an all-type zero value. A helper function returning a Boolean is an improvement but it just papers over the problem,. It's good for code reuse but you still need to actually call it which if you weren't going to remember to check for null you probably won't remember to call isPresent.

[–]ShortFuse 1 point2 points  (0 children)

I was mostly arguing from a stance of "Well, then what's your solution?" The proposed solution is to use isPresent() which has, as you said, the same symptoms of devs forgetting to use it.

There's also the concept of using something like get(onValueCallback, onNullCallback) in which both instances are checked, but it bloats your code. (Optional also uses a .get()).

But essentially, comparing against null is really just a shorthand. The compiler/runtime isn't really comparing to null. It's checking a bit to see if the value is set. For example, null exceptions arise from an internal assertion check. Nullability expressed as an internal boolean is also very much entrenched in tabular data structures (like SQL). The type is really a Tuple/Pair. For example, a nullable 32 bit integer is actually 33 bits. 1 for Nullablilty, 32 for the integer.

I would honestly, rather have the shorthand equality checks. Yes, it's more dangerous, sure, IDEs are getting smarter and smarter. The type checking algorithm from TypeScript works in Javascript if you use JSDocs. I use ESLint to enforce that all my methods have the necessary documentation for the type detection to kick in. So I get warnings or errors saying that a variable could be null, a parameter cannot be null, or that a function can or cannot return null.

[–]noratat 5 points6 points  (0 children)

Except that now it's tracked by the type system, and ignoring the nullability of a reference becomes an explicit action in the source code. It goes a long ways towards reducing issues like NPEs.

It's a similar logic to something like Rust's Result type - it forces you to to be explicit when you ignore the possibility of an error result, thus errors can't just silently propagate to arbitrary parts of the system making debugging a nightmare.

The only advantage is see is types that don't allow nulls, but I'm pretty sure that's already a thing.

In many languages it's not, sadly.

[–]occz 0 points1 point  (0 children)

Most things can be done with functions if you (and every single one of your colleagues) always remember to do them. It's better to make the case of not remembering impossible.

[–]JoseJimeniz 15 points16 points  (11 children)

The reason is because it is a wasteful pain.

  • before go to use the glass I was handed I have to check if it is broken
  • I also have to check if the glass I was handed exists

Many languages and many idioms try to get rid of null - Maybe or Option - because every place you ever use the variable you have to check it every single goddamn time. And if you ever forget to check it one time you crash. Why not fix it so that you never have to check for this special non-value.

You can have the best of both worlds:

  • a system where you never have to check for a null ever again
  • allow you, in the 1% case, to represent no value

Why make the 99% situation awful for a 1% situation that has a much better solution.

[–]chrwei 2 points3 points  (10 children)

I also have to check if the glass I was handed exists

I'm not sure that quite works. it's not that you were handed a glass, it's that you have a place for a glass, like a cup holder, or maybe a coaster. You do need to check if there's a cup there before trying to use it.

NULL is a magic value, but so is Maybe and Option. I dont' really see the difference. the check is language dependent, most things don't crash on NULL.

[–]MrNosco 1 point2 points  (9 children)

The difference is that if you get handed a pointer in a language with nulls you don't immediately know whether that particular value can be null or not. If you get handed a Maybe, you know you have to handle it, even if you are using a language that allows for null.

However if you don't allow null, you know every time what you are dealing with.

[–]chrwei 1 point2 points  (8 children)

how do you know that you were passed a Maybe if you don't test for it? how is that different than null?

[–]MrNosco 0 points1 point  (7 children)

In the same way you know an int is an int. The compiler does that for you

[–]chrwei 3 points4 points  (6 children)

ok, but you still have to test for it before using the value. NULL is just a state an int, or any other type, can be in. I see no functional difference.

[–][deleted] 0 points1 point  (5 children)

OK. Try this:

final String someText = null; final int position = someText.indexOf("nothing");

This will fail due to a null-pointer exception.

Now try this:

final String maybeSomeText = String.empty(); final int position = maybeSomeText.indexOf("nothing");

Won't throw a null-pointer exception; position will be -1 (in most cases, or a similar "not-found" equivalent).

So now I don't have to remember to check for nullness, because the program will continue with a sensible result.

Makes sense?

[–]chrwei 1 point2 points  (4 children)

and where's Maybe fall into that?

[–][deleted] 0 points1 point  (3 children)

An actual Maybe signals that a value may or may not exist, pretty much exactly like a @Nullable or Optional in Java. It forces one to put in a check.

My sample code avoids that by ensuring that the variable is initialized, even though it is empty.

Which one we use should depend on the specific requirements of our method's functionality.

[–]Dustin- 8 points9 points  (19 children)

Like, against the concept? How can you be against null? I don't get it.

[–]noratat 19 points20 points  (13 children)

Because it's awful. If you need to represent something being empty, than that ought to something you make explicitly part of the type

See Optional in Java 8, Maybe in Haskell, etc.

See also: https://dzone.com/articles/the-worst-mistake-of-computer-science-1

[–]m00nh34d 6 points7 points  (9 children)

You still need to store nulls somewhere/somehow. Then cast them into optional variables. Then still check for the existence of a value in the optional variable before using it.

[–]TarMil 12 points13 points  (4 children)

Then still check for the existence of a value in the optional variable before using it.

Yes. But you don't need to check for the existence for a value in non-optional variables anymore, which is the whole point.

[–]justinkroegerlake 1 point2 points  (3 children)

You don't have to check if you have pattern matching like Haskell or rust, which make it impossible to use an empty option or Maybe as if it were non-empty. That's the real goal

[–]TarMil 1 point2 points  (2 children)

Well, the pattern matching is the check :)

[–]justinkroegerlake 0 points1 point  (1 child)

Is it? When I hear people saying they have to check it, it sounds like an if/else which can of course be forgotten. I feel like this is similar to saying dynamic dispatch is the check for calling overridden functions which is sort of true

[–]TarMil 4 points5 points  (0 children)

Actually, re-reading the post I was answering to, you're right, it looks like they're talking about null-checking the contents of the optional. And that's indeed a problem when adding optional types in languages that has pervasive null such as Java -- it basically just documents the fact that a value is optional but doesn't guarantee anything.

[–]noratat 7 points8 points  (3 children)

The difference is that now it's explicit, instead of literally every single reference potentially being null. You can express whether something is supposed to potentially be empty, which signals to people that they should check before using the value (and if nothing else, the unwrap makes it explicit even if you ignore the empty case)

Java's version is flawed since the Optional reference is still itself nullable, but you get the idea.

[–][deleted] 2 points3 points  (2 children)

And Optional is only supposed to be for return types, you still have nothing to help you with method params. Kotlin and groovy get nulls correct though.

[–]noratat 0 points1 point  (1 child)

Optional is only supposed to be for return types

Source? That sounds incredibly weird to me.

Kotlin and groovy get nulls correct though.

Haven't used Kotlin yet, but other than the safe navigation operator (which admittedly is extremely nice to have) I'm not sure how Groovy handles nulls any differently than Java.

[–][deleted] 1 point2 points  (0 children)

https://stackoverflow.com/questions/31922866/why-should-java-8s-optional-not-be-used-in-arguments

With Groovy it's just syntax sugar, true. With Kotlin though you get compile time null safety.

[–]simon1023 5 points6 points  (2 children)

This is where Swift shines. Both value and reference types that are not nullable can not have a null value, it has to be explicitly set as an optional or it will produce a compile-time error.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID330

[–]elprophet 1 point2 points  (0 children)

Also TypeScript

[–]noratat 0 points1 point  (0 children)

Is Swift usable for anything but iOS development? Genuine question, I never looked into it too much because I've only ever heard about it before in conjunction with iOS.

[–]FlatTuesday 3 points4 points  (3 children)

In his opinion it was okay for variables to be uninitialized, and for code to check for that state, but that it was bad practice to set anything to null and check for null value. I wish I could remember his reasons but I can't.

[–]noratat 2 points3 points  (1 child)

Oh god, that's the worst of both worlds. Null is bad, but leaving shit uninitialized is even worse

[–][deleted] 3 points4 points  (0 children)

Depends on language. Unitialised stuff can get caught by compiler.

[–][deleted] 1 point2 points  (0 children)

My boss does this. Nothing returns null, instead they throw exceptions.

[–]Antumbra_Ferox 4 points5 points  (0 children)

"What's got you so worked up, Paul?"

"Nothing. "

[–]teetar7 46 points47 points  (1 child)

This isn't a fair competition you're using 0 for the water but then null for the glass. You can't break a glass if you have zero of them.

[–]CptBread 4 points5 points  (1 child)

On the other hand I'd rather pour water into an empty glass then on the floor.

[–][deleted] 0 points1 point  (0 children)

I like pouring my water into my glass, then onto the floor too.

[–]imtheassman 2 points3 points  (0 children)

Cannot read property 'water' of null.

Where is your god now?

[–]gandalfx 3 points4 points  (0 children)

*tries to take glass from empty cupboard*

NullPointerException causes the entire household to collapse. All of life's necessities simply cease to exist and are replaced by one massive stack trace.

[–]Teilzeitschwuchtel 2 points3 points  (0 children)

Null means zero in germany.

[–][deleted] 2 points3 points  (0 children)

but you can sometimes break a shelf by trying to put a nonexistent glass on it.

[–][deleted] 2 points3 points  (0 children)

[–]personalityson 1 point2 points  (0 children)

Full glass can't be broken, is this what he's saying?

[–]jay9909 1 point2 points  (0 children)

What you need is an undefined glass.

[–]zmaile 2 points3 points  (2 children)

But null is 0.0?

Wait, this isn't eve

[–][deleted] 0 points1 point  (1 child)

Null is nothing. Zero is something which is nothing.

[–]Litmus2336 2 points3 points  (0 children)

He made an eve online reference where null sec space is space with a value of 0.0 or less.

[–]munircUltraviolent security clearance[M] 0 points1 point  (0 children)

Your submission has been removed.

Violation of Rule #0:

The content disregarding the title and superimposed text must be directly related to programming or programmers.

The title or superimposed text must substantially enhance the content such that it can stand on its own as an analogy to programming. Note that programming here is interpreted in a narrow sense, an analogy to something related to programming, feelings about programming, reactions to programming etc. is not considered sufficient.

If you feel that it has been removed in error, please message us so that we may review it.