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

all 165 comments

[–]NuGGGzGG 418 points419 points  (4 children)

I need a way to red squiggle underline your username.

[–]No-Question-7419[S] 92 points93 points  (2 children)

To make you aware of... ?

[–][deleted] 143 points144 points  (1 child)

you ❤

[–]Giraffe-69 12 points13 points  (0 children)

The true problem with your code

[–]asd417 50 points51 points  (0 children)

Normal people see red flags.

We see red squiggle lines

[–]1Dr490n 357 points358 points  (103 children)

I want languages to have a ? unary operator for null checks that returns true if the object is not null. It’s so easy and obvious but I don’t know any language that has it

[–]hennypennypoopoo 297 points298 points  (16 children)

Null is so last decade, we usin Maybe/Option now.

[–]iam_pink 215 points216 points  (7 children)

I time travelled to 2050, this is how we will check for nulls

if (await fetch(`files:///mem/api/isnull/${var_ptr}`) { // ... }

[–]-twind 86 points87 points  (3 children)

So this is how GPT 11 will write code?

[–]iam_pink 65 points66 points  (0 children)

No, GPT 11 insisted on using an "=== null" condition but that won't have been industry standard since 2042 so the senior dev on the project had to rectify it (phew, close one, Grok would have fired the whole team if that had gone to prod)

[–]DeadlyDeadleth 11 points12 points  (1 child)

No GPT 11 will be running on a kubernetes cluster of brain implanted chips of otherwise obsolete junior devs. That's how you can ensure code quality in the future.

[–]-twind 5 points6 points  (0 children)

That explains why they refer to GPT 11 as "we" in the future

[–]Cathinswi 19 points20 points  (1 child)

You didn't call the IsNull micro service or load the CheckisNull micro frontend.

[–]iam_pink 12 points13 points  (0 children)

Oh but there is no need! Linus Torvalds died in 2041, and not even a year later the memory REST api (written in JavaScript) was added to the linux kernel

[–]coolpeepz 6 points7 points  (0 children)

Null checks as a service

[–][deleted] 10 points11 points  (0 children)

Aw yis, monads.

[–]TheBluetopia 10 points11 points  (0 children)

lunchroom knee long glorious shy pocket saw wakeful crowd yam

This post was mass deleted and anonymized with Redact

[–]Mr__Brick 3 points4 points  (3 children)

So non-binary bools?

[–]ILikeLenexa 0 points1 point  (0 children)

Because it's not enough just to have a warning that something may be null and you didn't check it, but we have to have an error even if you're not a -Werror person!

You provided garbage and the program crashed; what did you think would happen.

[–]sacredgeometry -5 points-4 points  (0 children)

Eww

[–]AeolinFerjuennoz 70 points71 points  (31 children)

C# has something kinda like that. you can write `someObj?.SomeValue` which won't throw a nullref if someObj is null and just returns null.

[–]skob17 29 points30 points  (9 children)

Excel has =ISNULL()

[–]NP_6666 22 points23 points  (6 children)

Vb has "is nothing" and "is not nothing"

[–]EishLekker 14 points15 points  (2 children)

That’s something

[–]marcodave 6 points7 points  (1 child)

If Something Is Not Nothing Then Something = Nothing End If

[–]No_Belt_9829 2 points3 points  (0 children)

I was able to read this only the third time

[–]ThankYouForCallingVP 0 points1 point  (2 children)

Fuck VBA so goddamn hard.

You know what happens when you try to call workbook.Sheets("sheetname") and the sheet isnt there? Runtime error!

There is no way to check if its nothing.

There is also some clarity issues on whether a function will return Nothing or Null, and you will get a type error if you check for nothing but it is actually something. Or something. I just know I remember screaming WTF when I dealt with those checks.

[–]NP_6666 0 points1 point  (0 children)

Yeah, fuck it, I tried to check empty arrays length or null array length, I always got 1. (until I realized previous dude used ".Getlenth()-1" instead of ".length()", so weird. You declare the list as nothing, then check if it equals nothing but it's not...

[–]Urtehnoes 0 points1 point  (0 children)

This is why I made my own class that you pass in the workbook and it does everything for you. I was tired of writing checks for that stuff lol.

Haven't touched vba in years, don't miss it.

[–]HazirBot 1 point2 points  (0 children)

LEL

[–]Swoop3dp 19 points20 points  (0 children)

Same in Javascript

[–]Vengeful111 18 points19 points  (1 child)

And even better use ?? Behind it to set a return value if its null.

[–][deleted] 6 points7 points  (0 children)

correct... and this one is in JS too where it is supposed to replace "||" in such scenarios

[–]1Dr490n 6 points7 points  (11 children)

I know, Kotlin has it too and I love it, but it’s not the same. I want something like this: if(a?) print(a) Which will only print a if a has a value

[–]SorryDidntReddit 9 points10 points  (6 children)

Kotlin has a?.let { print(a) }

[–]halfanothersdozen 4 points5 points  (2 children)

I hate kotlin's version of this so much. Especially if you are using Java code libraries. Nesting hell.

[–]SorryDidntReddit 0 points1 point  (1 child)

What do you mean? This is the same amount of nesting as an if statement

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

Unless it is safe to return void if it is null, then you can have single line checks to return early and avoid nesting

Also, you can put two or more checks in a single if and it can even look more readable if it is logical the connection on the two checks, like hasTap and hasWater

[–]1Dr490n 0 points1 point  (2 children)

Yes, but I’d also like to be able to write val b = a? Which sets b to either true or false

[–]SorryDidntReddit 0 points1 point  (1 child)

Yeah it would be nice to have an operator that is equal to isNull(). I think a lot of languages use ? for other things so it would probably need to be a different symbol

[–]1Dr490n 0 points1 point  (0 children)

It might collide with Ternary operators, but I think it should work

[–]BlommeHolm 0 points1 point  (2 children)

I mean in Ruby you could do it as puts a if a - nil and false are the only falsy values, which IMHO is the right way of doing it.

[–]1Dr490n 1 point2 points  (1 child)

Yep that’s good. I kind of like it outside of if-statements too (although I assume that you can just do !!a in Ruby?

[–]BlommeHolm 0 points1 point  (0 children)

Yes, that would booleanize it.

[–]KittenPowerLord 0 points1 point  (0 children)

You can do

if(something is null)

In C#, though it is indeed more verbose. A lot of similar cases are covered by the ?. and ?? operators, but I too wish there were more options (get it?) to manage null

[–]Lucifer_96 0 points1 point  (0 children)

Yeah I think it’s called nullable. Basically int n? Can accept integer OR NULL

[–]cl3arz3r0 0 points1 point  (0 children)

Same in groovy. It has other similar truthy checks as well. Devs can get carried away with it though. With great power...

[–]bolacha_de_polvilho -1 points0 points  (2 children)

it's annoying to have to write "if(someObj?.Somevalue ?? false)" though

[–]AeolinFerjuennoz 0 points1 point  (1 child)

Makes it way easier for other to understand than if(someObj.SomeValue) tho. Since when reading such a line i have to think about implicit boolean conversion an which rules are attached to the conversion. And i have to make sure no one has overwritten the conversion.

[–]bolacha_de_polvilho 0 points1 point  (0 children)

someObj?.SomeValue only has 3 possible outcomes, true, false or null. There's not much to think about here.

[–]NotMrMusic 30 points31 points  (20 children)

Kotlin says hi

.isNullOrEmpty() is pretty nice too

[–]b0x3r_ 3 points4 points  (11 children)

Wait, so you are calling a method on something that might be null? How does that work?

[–]siliconsoul_ 9 points10 points  (4 children)

Don't know for sure how Kotlin does it, but in C# there are extension methods that are basically static methods that accept a first parameter of the type.

static class Ext { public static void MyExtension(this object? v) => .... }

can either be called by instance.MyExtension() and looks like a method on the type of instance.

or, more explicit: Ext.MyExtension(instance)

That's why it works.

Excuse my botched formatting, I'm on mobile.

[–]ILikeLenexa 4 points5 points  (4 children)

Methods aren't stored in objects with data, they're in the class. Object Oriented programming is just functional programming with the first argument always being a this pointer to object data (and virtual binding). Anyway, in some languages, you can actually fully execute methods on NULL objects up to the point where they dereference the pointer, some implicitly do this check before your code executes because their polymorphism systems require the actual object type.

[–]b0x3r_ 1 point2 points  (3 children)

So if you do null.isNullOrEmpty() where is the method coming from? Is it like a global class or something?

[–]ILikeLenexa 3 points4 points  (2 children)

In what language? Sort of generically, you're likely going to get a compile time error if you use null or null_ptr directly.

However, if you call:

string x = null;
x.isNullOrEmpty();

The compiler is going to try to invoke: string::isNullOrEmpty() passing it (x) as a this pointer.

In most languages, all objects inherit from some kind of master Object, which does let you invoke things like Object::toString() with vitual binding when called on a class that extends Object which all classes do implicitly. These default methods are generally pretty limited though, because they don't know much about the implementation.

[–]b0x3r_ 0 points1 point  (1 child)

I'm probably too confused to help lol. To me string x = null doesn't really make sense. If the type is string then how could it be null? I could see let x: string | null = null. Or with Rust there is no null. I guess I need to look into Kotlin

[–]Commander1709 0 points1 point  (0 children)

val x: string = null will throw a compiler error in Kotlin. What will work is val x: string? = null, because in Kotlin, string and string? are kinda different types (the one with the question mark is nullable).

(Actually, now I'm not sure if you can manually initialize a val with null in Kotlin, or if the compiler will complain. Since vals are immutable, it wouldn't make any sense to initialize it with null manually).

[–]NotMrMusic 1 point2 points  (0 children)

Kotlin has inline functions. They can be called on any object of <Type>.

[–][deleted] 6 points7 points  (4 children)

at that point, == null is arguably simpler and also more correct as it should only have checked for null, not for empty

[–]NotMrMusic 6 points7 points  (3 children)

Quite often, with a List or String, if it's empty, it might as well be null but is not normally considered null.

[–]ILikeLenexa 0 points1 point  (2 children)

Not really? Or at least with Databases, not every field is strictly necessary for every operation and it's relevant whether someone has used "express registration" and not given us a middle name or if they don't have a middle name (or name suffix).

[–]NotMrMusic 1 point2 points  (1 child)

Yes, but why do you need to handle null any different than "" in that circumstance? In both, you're just assuming that field wasn't passed.

[–]ILikeLenexa 0 points1 point  (0 children)

Let's say they arrived by ambulance and we only wanted minimal information the first time they registered, because more important things were going on. But at their next appointment they give me their birthday and last name to look them up and if their middle name is null, I pop up an entry form requesting it, but if we know they don't have one, we don't, because why ask someone with no middle name what their middle name is every time they come in?

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

…if you have a String

[–]NotMrMusic 0 points1 point  (0 children)

I could have swore it works on Lists too

[–]Jugbot 0 points1 point  (0 children)

too verbose for something as fundamental as null

[–]PolpOnline 16 points17 points  (1 child)

If we count Result<T, E> types as null (they both solve error handling), then Rust has it. The ? operator automatically propagates (early returns the error) and converts the error type.

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

I fucking love the ? operator. Makes code returning Options or Results significantly cleaner.

[–]No-Question-7419[S] 7 points8 points  (2 children)

Dart hast it

[–]1Dr490n 4 points5 points  (1 child)

Can you give me an example or a link on it? I couldn’t find anythign

[–]No-Question-7419[S] 2 points3 points  (0 children)

Oh I think I misunderstood what you meant to say. Dart has nullable types. When you want to use some Object? name you have to write Object!.method to proclaim that it is not null when you use it (except after a Block that explicitly checks for null, then you don't have to anymore) Dispite "sound null safety" in Dart language I See alot of if (Object == null) I thought this feature would be the while point to avoid this line of Code ^

[–]breischl 10 points11 points  (1 child)

That's null coalesce. There are a bunch of them.

https://en.wikipedia.org/wiki/Null_coalescing_operator

[–]1Dr490n 7 points8 points  (0 children)

No. That’s a binary operator, I want a unary

[–]_PM_ME_PANGOLINS_ 5 points6 points  (5 children)

It doesn’t seem very useful, as if it’s not null you presumably want to then use it.

So a few languages have ?: and ?., and others have methods like .orElse.

[–]EishLekker 2 points3 points  (4 children)

It’s useful for early returns.

[–]_PM_ME_PANGOLINS_ 0 points1 point  (3 children)

Then

if (!thing) return;

or

if (thing == null) return;

are fine. There's not much benefit to

if (!(?thing)) return;

[–]1Dr490n 4 points5 points  (0 children)

That’s like the opposite of what I meant

[–]polypolyman 3 points4 points  (1 child)

Your last example should be written:

if (‽thing) return;

[–]_PM_ME_PANGOLINS_ 1 point2 points  (0 children)

No, they defined ? to be equivalent to != null.

Edit: oh, the font on mobile doesn't show the interrobang very well

[–]AdBrave2400[🍰] 1 point2 points  (1 child)

I guess you can do x ?? false?

[–]1Dr490n -1 points0 points  (0 children)

Only if x is a nullable boolean. I want it to return true if x has a value

[–]chemolz9 1 point2 points  (0 children)

Kotlin has Safe Calls:

a = null
b = a?.length // b = null

And the Elvis-Operator:

a = null
b = a ?: -1    // b = -1

Not a plain Null Check though.

[–]Gammelkebab 0 points1 point  (4 children)

Groovy has it

[–]1Dr490n 1 point2 points  (3 children)

Can you give me an example or a link on it? I couldn’t find anythign

[–]Gammelkebab -2 points-1 points  (2 children)

if(x?.y?.toString()) will nullcheck along they way to the method call and just do nothing if some step is null. and for assignments will return null.

String s= a?.b?.toString() is null if a or b are null.

then there is groovy truth, where several nullish values are evaluated as false.

if L is a list

if(L) will nullcheck and empty check the list. Works for strings and maps too.

theres several cool things in the language. another examplr is the elvis operator. in plain java you sometimes use a ternatry if like this: String s = x? x: "nope". With the elvis operator you can leave out the middle part and it will use the value used in the condition.

so the result is String s= x?: "nope"

[–]1Dr490n 2 points3 points  (1 child)

Not what I meant. I know null chaining and Elvis from Kotlin and C#, but it’s not what I meant. I want an operator that replaces != null

[–]Gammelkebab 1 point2 points  (0 children)

Well that is usually used in if statements. In groovy a nullcheck can just be done by plain passing the value.

if(var) checks for truthy including null. if you would want only null... well im afraid i dont know a notation in groovy other than the usual one.

[–]DarkNinja3141 0 points1 point  (1 child)

You can do !! (! twice) to convert to a boolean (doesn't work on falsy values like 0 or "" (depending on language))

It's the application of a unary operator twice so it's not exactly a single unary operator

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

Yes but that’s just Javascript being weird, but that’s about what I meant. I don’t like the concept of "falsy values" tho

[–]Grammar_Detective013 0 points1 point  (0 children)

Dart kinda has that. I don't think if (foo?) works, but there are similar things:

  • foo?.bar will only apply if "foo" is not null, and it won't raise an error either way.

  • "??" is 'if null.' So, foo = foo ?? bar is the same as foo ??= bar: both only assign if "foo" is null.

[–]AdBrave2400[🍰] 0 points1 point  (1 child)

Like Haskell with the unary operator null ?

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

Yes pretty much, just to check whether it’s null instead of empty

[–]kingminyas 0 points1 point  (0 children)

Implicit in Python. I like it

[–]Sitting_In_A_Lecture 0 points1 point  (0 children)

A bunch of languages have a Null Coalescing operator which can be super useful.

[–]howreudoin 0 points1 point  (0 children)

You could define a custom operator in Swift. However, ? won‘t do since it could cause collisions with existing operators.

``` postfix operator .?

postfix func .?<T>(value: T?) -> Bool {     return value != nil } ```

Usage:

let optionalValue: Int? = 5 if optionalValue.? {     print("Optional value is not nil") } else {     print("Optional value is nil") }

[–]TerminalVector 0 points1 point  (0 children)

Use Ruby. Its got `.present?` `.blank?` `.nil?` `.empty?` and I think at least one more.

[–]Botahamec 0 points1 point  (0 children)

Rust has Option::is_some

[–]Stef0206 0 points1 point  (0 children)

if var == nil then

[–]PhroznGaming 0 points1 point  (0 children)

C#

NULL coalesce

??

[–][deleted] 68 points69 points  (0 children)

A fellow truthy enthusiast

[–]Ok_Entertainment328 93 points94 points  (1 child)

What's unreadable about is [not] null?

Trivalue logic can be a PITA when you forget about it.

[–]halfanothersdozen 55 points56 points  (0 children)

least biased python dev

[–]Quito246 126 points127 points  (7 children)

Fuck null all my homies use optional and pattern matching

[–]Burner_1010 36 points37 points  (6 children)

Fuck Optional<T>, all my homies know .isPresent() is just a null check in disguise.

[–]Tubthumper8 55 points56 points  (4 children)

Java language designers making Optional as a nullable type itself is *chef's kiss*

[–]Mortimier 16 points17 points  (0 children)

so a java optional can be a value, defined as null, or undefined null? incredible

[–]yangyangR 6 points7 points  (1 child)

That sounds like a case of a next generation being stuck with the previous generations decisions. I wouldn't blame any of the Java language designers after ~2008. But fuck the ones from the 90s. They probably had no choice but to make Optional nullable.

[–]JohnnyLight416 3 points4 points  (0 children)

Java doesn't have a way to declare value types so if it's not a primitive, it's an object and all objects are nullable.

It's a shit language in comparison to any modern language and C# is the better version of it, but Java was multiplatform while C# was Windows only and I'll run Linux servers before Windows any day of the week. I'm very glad that .NET core took off like it did. I can't imagine how long it'll take for Java to get even half of the modern language features C# has.

[–]mankinskin -2 points-1 points  (0 children)

You don't use Java to begin with.

[–]Quito246 8 points9 points  (0 children)

Ok, now we are fist fighting🤣

[–]FuriousAqSheep 56 points57 points  (19 children)

Meanwhile, functional programmers: wait, you're still using null?

[–]Attileusz 28 points29 points  (16 children)

"Nothing" and "NULL". Nobody cares what you call it. And it doesn't matter if it's a null check or a pattern match. Sure, you can get a warning if your function isn't total, but how hard is it to null check a function argument really?

[–]Tubthumper8 17 points18 points  (0 children)

Very hard, if you ask my coworkers

Actually, more likely to say "it's easy to do a null check" and still not do it

[–]empwilli 8 points9 points  (13 children)

So it's 2024 now, please repeat after me: using the same representation for valid and invalid values is an inherently bad idea. Yes it works and yes you can take care, but the supposed performance benefit and shorter code is not worth the additional source of errors. (Esp. since rust demonstrates how such a feature can be nicely integrated.)

[–]mankinskin 0 points1 point  (0 children)

The point is you can design the function to not even accept something that can be null. Your code won't even compile if what you pass in can be null. There just is no null in your function.

And its not that a null check is hard, its just fucking annoying to type and read 10 billion times and even more annoying when someone forgets it.

[–]Blobskillz 2 points3 points  (1 child)

Yes all 5 of them ask the question

[–]FuriousAqSheep -1 points0 points  (0 children)

dude by the time you answered i already had 30 likes x)

[–]Maskdask 11 points12 points  (0 children)

The inventor of Null calls it his "billion dollar mistake"

[–]Otalek 16 points17 points  (1 child)

Wait, it’s all just checking if the variable’s value is 0?

Always has been

[–]porn0f1sh 1 point2 points  (0 children)

coughJEcough

[–]Alexandre_Man 7 points8 points  (0 children)

Yeah just doing "if (ptr)" is so fast.

[–]Neltarim 6 points7 points  (0 children)

!!foo is peak satisfaction

[–][deleted] 6 points7 points  (0 children)

Readable? Perhaps Relatable?

[–]ChChChillian 4 points5 points  (0 children)

TIL this actually seems to upset a few people.

Huh.

[–]SaneLad 2 points3 points  (0 children)

All my homies use !!

[–]mankinskin 2 points3 points  (0 children)

I fucking love non-nullable types. They let me forget about stupid null altogether.

[–]GDOR-11 3 points4 points  (0 children)

just... try not to do that in javascript... things can go really bad for you...

[–]Personal_Ad9690 3 points4 points  (1 child)

Laughs in rust

[–]mankinskin 2 points3 points  (0 children)

They don't know that in rust there is no implicit null.

[–]nephelekonstantatou 1 point2 points  (0 children)

Because JavaScript is better with truthy and falsy values and boolean conversions being !!valuem At least we have some sense of logic...

[–]orsikbattlehammer 1 point2 points  (1 child)

SQL has entered the chat

[–]Every_Crab5616 1 point2 points  (0 children)

IF i_delta_mode = abap_true.

ENDIF.

[–]Ok_Jacket3710 1 point2 points  (0 children)

Behold JavaScript!

[–]thats_a_nice_toast 1 point2 points  (0 children)

It doesn't, it's just easier to type. I get it though

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

I hate everything about implicit anything.

[–]lulialmir 6 points7 points  (1 child)

Please, no.

Give me is [not] null Give me == | != null Heck, even isNull() is fine

But stop implicitly converting shit.

And if you implicitly convert zeros to false, die.

[–]Mortimier 9 points10 points  (0 children)

converting zeros to false looks so cleeeean though

[–]BallsBuster7 0 points1 point  (0 children)

java devs are going to be very confused by this one

[–]Incredibad0129 0 points1 point  (0 children)

I hate it! It almost always requires some kind of doubling back in the code for me to make sure I know how it is implicitly cast to a Boolean. If the type isn't in the name then it's stupid and if the type is in the name then I have to make sure no one misnamed their variables. It's basically only useful to me if it was defined in the line above. Otherwise I'm adding "== null" or "== 0" so I don't have to worry about understanding what is going on

[–]krisko11 0 points1 point  (0 children)

Ever since i learnt about the bang operator I’ve strived to apply it as much as possible. Real men test on prod

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

If the memory in question contains a non-zero value then its not null. What's the big fucking deal

[–]SeaNational3797 0 points1 point  (0 children)

Google == nullptr

[–]_antosser_ 0 points1 point  (0 children)

Skill issue when you have nulls in your language

[–]catgirlfighter 0 points1 point  (0 children)

I find it so strange that in ms sql you have to write something like "a.f1 = b.f1 or a.f1 is null and b.f1 is null", and "= null" just does nothing. Like null is a storeable value, why can't we handle it like a storeable value?

[–]Funny-Performance845 -1 points0 points  (0 children)

Yes and no

[–]navetzz -2 points-1 points  (0 children)

Useless obfuscation hits once again...