What's New in C# 14: Null-Conditional Assignments
21–30 of 174 posts
Re: What's New in C# 14: Null-Conditional Assignments
#22More concise? Yes. More readable? I'm less convinced on that one. Some of those edge cases and their effects can get pretty nuanced. I fear this will get overused exactly as the article warns, and I'm going to see bloody questions marks all over codebases. I hope in time the mental overhead to interpret exactly what they're doing will become muscle memory...
Re: What's New in C# 14: Null-Conditional Assignments
#23Re: What's New in C# 14: Null-Conditional Assignments
#24More concise? Yes. More readable? I'm less convinced on that one. Some of those edge cases and their effects can get pretty nuanced. I fear this will get overused exactly as the article warns, and I'm going to see bloody questions marks all over codebases. I hope in time the mental overhead to interpret exactly what they're doing will become muscle memory...
What?.could?.possibly?.go?.wrong?.
What?.could?.possibly?.go?.wrong?
Not so convinced: What?.could?.possibly?.go?.wrong = important_value()
Maybe the design is wrong if the code is asked to store values into an incomplete skeleton, and it's just okay to discard them in that case.Re: What's New in C# 14: Null-Conditional Assignments
#25Re: What's New in C# 14: Null-Conditional Assignments
#26I couldn't imagine what a "Null-Conditional Assignment" would do, and now I see but I don't want this.
Less seriously, I think there's plenty of April Fools opportunity in this space. "Null-Conditional Function Parameters" for example. Suppose we call foo(bar?, baz?) we can now decide that because bar was null, this is actually executing foo(baz) even though that's a completely unrelated overload. Hilarity ensues!
Or what about "Null-Conditional Syntax". If I write ???? inside a namespace block, C# just assumes that when we need stuff which doesn't exist from this namespace it's probably just null anyway, don't stress. Instead of actual work I can just open up a file, paste in ???? and by the time anybody realises none of my new "classes" actually exist or work I've collected my salary anyway.
Re: What's New in C# 14: Null-Conditional Assignments
#27Earlier quoted context omitted.
if (This) { if (is) { if (much) { if (better) { println("I get paid by the brace") } } } }
if (!same) { return; } if (!number) { return; } if (!of_braces) { return; } println("but easier to read")
Re: What's New in C# 14: Null-Conditional Assignments
#28Earlier quoted context omitted.
What?.could?.possibly?.go?.wrong?.
if (This) { if (is) { if (much) { if (better) { println("I get paid by the brace") } } } }
what?.could?.possibly.go?.wrong = important_value()
Maybe we want code like this: if (!what)
what = new typeof(what); // default-construct representative instance
if (!what.could)
what.could = new typeof(what.could);
if (!what.could.possibly.go)
what.could.possibly.go = new typeof(what.could.posssibly.go)
// now assignment can take place and actually retain the stored value
// since we may have allocated what, we have to be sure
// we propagate it out of here.
what.could.possibly.go.wrong = important_value();
and not code which throws away the value (and possibly its calculation).Why would you ever write an assignment, but not expect that it "sticks"? Assignments are pretty important.
What if someone doesn't notice the question marks and proceeds to read the rest of the code thinking that the assignment always takes effect? Is that still readable?
Re: What's New in C# 14: Null-Conditional Assignments
#29Isn't this more confusing? Because it skip the code if the value is null and I don't think it is normal to follow the flow assuming nothing has happened.
> I don't think it is normal to follow the flow assuming nothing has happened. I think it is for situations where the programmer wants to check a child property but the parent object may be null. If the parent is expected to be null sometimes, the syntax lets the programmer express "try to get this value, but if we can't then move on" without the boilerplate of explicitly checking null (which may be a better pattern…
Re: What's New in C# 14: Null-Conditional Assignments
#30More concise? Yes. More readable? I'm less convinced on that one. Some of those edge cases and their effects can get pretty nuanced. I fear this will get overused exactly as the article warns, and I'm going to see bloody questions marks all over codebases. I hope in time the mental overhead to interpret exactly what they're doing will become muscle memory...