language icon indicating copy to clipboard operation
language copied to clipboard

Are we thinking about an alias for if null and if not null?

Open jodinathan opened this issue 2 years ago • 10 comments

Sorry if already posted, I just couldn't find anything related.

What about a shorthand to if (foo == null) and if (foo != null)?

Maybe some simple stuff like

if (foo?) // same as foo == null

if (foo!) // same as foo != null

jodinathan avatar Jul 06 '22 13:07 jodinathan

Personally, I don't like this change, since it makes it less clear when null-checking are happening compared to the amount of characters actually being saved. There are also several cases with existing syntax where I think this could add confusion like e.g. ternary expressions: if (var? ? something() : somethingElse()).

Also, ! are already a thing so what does it even mean to say: if (var!) if var are a bool?? Does it check for null? Does it force a null-check and extract the value of the bool?

julemand101 avatar Jul 06 '22 13:07 julemand101

Yeah, my original post was to find an alternative to if null and if not null. The example just to foment the idea. Edited the post and title.

Currently checking for null is too verbose to something that happens a lot

jodinathan avatar Jul 06 '22 13:07 jodinathan

I don't think this is valuable at all... It's not like we make null checks all the time, nor like it's a verbose expression, so...

mateusfccp avatar Jul 07 '22 09:07 mateusfccp

I agree, I think "verbose" isn't the right way to describe it. A null check says exactly what it's doing, using syntax we're already familiar with, with no extra words:

if (value == null)

Read: "If value is/equals null". With a shorter syntax, we'd have to "unwrap" it in our heads as we read it.

Levi-Lesches avatar Jul 07 '22 19:07 Levi-Lesches

// this
foo = foo == null ? bar : foo;

// became this
foo ??= bar;

I am assuming that the reasoning for the shorthand is because the prior is verbose to type and read.

IMO I find (value == null) verbose and semantically "incorrect".

For example, if we had something like:

if (?foo) // meaning foo is null

if (?!foo) // meaning foo is not null

We would be validating the nullability independently of the null keyword, this means we could have undefined and other kind of nullability keywords.
This was just a brainstorm example, not that we need an undefined kind of variable etc.

jodinathan avatar Jul 07 '22 19:07 jodinathan

There is the option of making null falsy and others truthy. I'm personally not a fan of that but since you need ideas...

I'm also not a fan of adding 1 char tokens, I think they are obtuse when you don't use a language often and you think to yourself "what is this again ? Oh right". Obviously that's not the case with people reading this repository but it's something to keep in mind.

foo ??= bar;

I don't like that either, in my experience it does not happen often and foo = foo ?? bar; isn't much longer and less cryptic. The only reason I ever use that is because the lint tells me to, but it's bad for readability imo.

cedvdb avatar Jul 08 '22 00:07 cedvdb

// this
foo = foo == null ? bar : foo;

// became this
foo ??= bar;

This example is verbose because it says the word foo three times. Not to mention the ?: syntax is hardly intuitive and readable in the first place. IMO, if you really don't like syntax sugar, replace it with the full thing, it's even shorter and more readable.

if (foo == null) foo = bar;

IMO I find (value == null) verbose and semantically "incorrect".

...why? Again, it's not verbose, it's literally the minimum amount of characters/information needed to express "check if value is equal to null". Making it shorter isn't going to be cutting down on redundancy, it'll be adding special tokens you need to memorize to know it means the same thing. In any case, checking if value == null is definitely not semantically incorrect if you're actually checking that the value is null.

We would be validating the nullability independently of the null keyword

Again, how does this make it more "valid" or "correct"? An object's "nullability" just means whether it is or can be equal to null. Avoiding the word null isn't more correct at all, since it's directly tied to the notion of nullability.

Levi-Lesches avatar Jul 08 '22 21:07 Levi-Lesches

using foo == null gives me a feeling that I am checking the equality of a hardcode constant ie bool isOdd(int n) => n == 1 || n == 3 instead of a someInt.isOdd or n % 2 == 0 etc.

As if the null keyword ever changes, it would break stuff.

So adding a shorthand to nullability also opens room for other possibilities.

jodinathan avatar Jul 08 '22 21:07 jodinathan

I think what you are saying is that it's less English?

final employees = findCompanyEmployees();
final employeesCount = employees.length;

if (employeesCount.isOdd) {
   print('we have an odd number of employees');
}

final firstEmployeeName = employees.first.name;
if (firstEmployeeName != null) {  // this line is not like the others above, it's less "English"
  print(firstEmployeeName);
}

// could be if (x.isNull) 

cedvdb avatar Jul 08 '22 23:07 cedvdb

Maybe some simple stuff like

if (foo?) // same as foo == null

if (foo!) // same as foo != null

Given how heavily overloaded ? and ! are already—nullable types, null-aware method call, conditional operator, null-aware spread, logical not—I think that would just cause more confusion than clarity. == null and != null are already fairly short completely clear in their meaning, so I think it's unlikely we'd come up with something better.

munificent avatar Jul 09 '22 00:07 munificent