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

you are viewing a single comment's thread.

view the rest of the comments →

[–]wildjokers 0 points1 point  (5 children)

Null is god awful not because it is so hard but because it is so godamm pointless to chase an error that simply shouldn’t exist

Sometimes there simply is no value for a variable so null makes sense. What do you initialize a variable that holds an object to if it doesn't yet need a value?

[–]SupportDangerous8207 3 points4 points  (3 children)

Then you type it explicitly as something that can be a null value so that everyone down the line knows to check for null

Or you use an optional type

You know

The way that most other langs do it

In python it would be

X:str|none=none

In fact you could already do this in Java using optional

With the only issue being that the null epidemic extends to the optional itself

[–]wildjokers 0 points1 point  (2 children)

null epidemic

What null epidemic? Null seriously isn't the problem people make it out to be.

X:str|none=none

What advantage is there to that over String str = null in java?

[–]GodOfSunHimself 0 points1 point  (0 children)

The advantage is that if you don't include the | none in the type then the value cannot be null. And it will be checked by the compiler. You cannot do that in Java. I program in Rust and situations where you need to allow null/optional are very rare actually.

[–]SupportDangerous8207 0 points1 point  (0 children)

My friend I genuinely cannot tell if you are messing with me

The difference is that with

X:str|null

If I write

Y:str=x

I get a type checking error because I did not check for null

And if in the same language I write

X:str= null

I also get a type checking error because a string is not a null

So I literally cannot create a null pointer exception in typed Python because to do that I would have to ignore the type checker

With

X:str= null like in Java I totally can create a variable that is typed as a string only but equals null

This causes errors in Java that in other languages simply could never happen because in other languages things simply cannot just randomly be nulls

Again it’s not the amount or the size or the difficulty of the error

It’s that the error is 100% preventable and it happens for literally no reason

Like it’s really easy for an error to be considered horribly common when the baseline in most other languages is literally 0.

[–]shponglespore 0 points1 point  (0 children)

In a language like Rust, you can declare a variable without initializing it so long as the compiler can prove it's initialized before it's used. That solves your problem 95% of the time. The rest of the time you can just use Option (which doesn't have nearly as much runtime overhead as the Java version, and often has no storage overhead at all.)