Earlier quoted context omitted.
Why the requirement, because of J++ and how Ext-VOS alongside Cool became .NET? Most companies don't care about this kind of stuff. I work across Java, C#, JS/TS, C++, SQL, and whatever else might be needed, even stuff like Go and C, that I routinely criticise, because there is my opinion, and then there is the job market, and I rather pay my bills.
Reminds of something I read somewhere: "I don't love Java, but I love the house it bought me."
What's New in C# 14: Null-Conditional Assignments
131–140 of 174 posts
Re: What's New in C# 14: Null-Conditional Assignments
#132Let's say in the first instance, you write a proof of concept algorithm using basic concepts like List, foreach, stream writing. Accessible to a beginner, safe code, but it'll churn memory (which is GC'd) and run using scalar CPU instructions.
Depending on your requirements you can then progressively enhance the memory churn, or the processing speed:
for(;;), async, LINQ, T[], ArrayPool, Span, NativeMemory.Alloc, Parallel.For, Vector, Vector256, System.Runtime.Intrinsics.
Eventually getting to a point where it's nearly the same as the best C code you could write, with no memory churn (or stop-the-world GC), and SIMD over all CPU cores for blisteringly fast performance, whilst keeping the all/most of the safety.
I think these new language features have the same virtue - I can opt into them later, and intellisense/analysers will optionally make me aware that they exist.
Re: What's New in C# 14: Null-Conditional Assignments
#133Honestly I like the C# better, and I was initially not super thrilled about the choice to include the period in the TS case, but since there is some overlap between the language design teams I assumed they would be the same.
Re: What's New in C# 14: Null-Conditional Assignments
#134Thanks for the upvotes! While testing and writing about the feature, I suspected it would receive mixed reactions. The `?.` operator behaves similarly on the LHS to the RHS, making the language more consistent, which is always a good thing. In terms of readability, I would say that once you understand how the operator works (which is intuitive because the language already supports it on the RHS), it becomes more read…
For example, if we have something like this:
if (config?.Settings is not null)
{
... Multiple lines that modify other settings.
config.Settings.RetryPolicy = new ExponentialBackoffRetryPolicy();
}
and we introduce another category SpecialSettings, we need to split one code block into two and manually place each line in the correct code block: if (config?.Settings is not null)
{
... Multiple lines that modify other (normal) settings.
}
if (config?.SpecialSettings is not null)
{
... Multiple lines that modify other special settings.
config.SpecialSettings.RetryPolicy = new ExponentialBackoffRetryPolicy();
}
With the new language feature the modification is easy and concise: config.Settings?.RetryPolicy = new ExponentialBackoffRetryPolicy();
becomes: config.SpecialSettings?.RetryPolicy = new ExponentialBackoffRetryPolicy();
and can be made for any other special setting in place, without the need to group them.Furthermore, I find the "Don't Overuse It" section of the article somewhat misleading. All the issues mentioned with regard to
customer?.Orders?.FirstOrDefault()?.OrderNumber = GenerateNewOrderNumber();
would apply to the traditional version as well: if (customer is not null)
{
if (customer.Orders is not null)
{
if (customer.Orders.FirstOrDefault() is not null)
{
customer.Orders.FirstOrDefault().OrderNumber = GenerateNewOrderNumber();
}
}
}
or: if (customer is not null)
{
var orders = customer.Orders;
if (orders is not null)
{
var firstOrder = customer.Orders.FirstOrDefault();
if (firstOrder is not null)
{
firstOrder.OrderNumber = GenerateNewOrderNumber();
}
}
}
If it really were a bug, when customer is null here, etc., then it would of course make sense to guard the code as detailed as described in the article. However, this is not a specific issue of the new language feature. Or to put it more bluntly: customer?.Orders?.FirstOrDefault()?.OrderNumber = GenerateNewOrderNumber();
is no replacement for customer.Orders.First().OrderNumber = GenerateNewOrderNumber();
were we want an exception on null.BTW, with the new version, we can also make the code even clearer by placing each element on its own line:
customer?
.Orders?
.FirstOrDefault()?
.OrderNumber =
GenerateNewOrderNumber();Re: What's New in C# 14: Null-Conditional Assignments
#135Sorry, I would flag this in a code review. It's too easy to skip past this visually. Explicit if statements make it a lot more obvious what's going on. This is too much syntactic sugar.
You've never seen something like this?
MyEvent?.Invoke(sender, arg);Re: What's New in C# 14: Null-Conditional Assignments
#136Earlier quoted context omitted.
I personally can't think of an all-rounder language that is better than C#. It's fast, has great tooling, powerful, extremely productive for working with large code bases and runs 'anywhere'. JS has lost against TS which is basically C# for web (both designed by the same person) and Python is not really something you should build large applications with (execution speed + maintenance issues). What do you believe is t…
Golang is often the default now. As someone who’s new to backend development, I’ve been exploring C# and can’t understand why it’s not the default. I think C# primarily has a marketing problem.
I think they would have done themselves a favor had they just rebranded as `dot`.
`dot build`
`dot run`
`dot publish`
`dot test`
Waaaaay better
Re: What's New in C# 14: Null-Conditional Assignments
#137Strange that they chose to make it `a?["b"] = c;` rather than to use the same syntax as TypeScript (`a?.["b"] = c;`). Honestly I like the C# better, and I was initially not super thrilled about the choice to include the period in the TS case, but since there is some overlap between the language design teams I assumed they would be the same.
Re: What's New in C# 14: Null-Conditional Assignments
#138Earlier quoted context omitted.
> his actually _reduces_ the "mental size" of C# IDK, if you read Settings?.RetryPolicy = new ExponentialBackoffRetryPolicy(); as "there is a now a ExponentialBackoffRetryPolicy" then you could be caught out when there isn't. That one ? char can be ignored .. unless it can't. It's another case where "just because it compiles and runs doesn't mean that it does the thing". This to me is another thing to keep track of.…
Hi there! One of the lang designers here. That's been part and parcel for C# for over 10 years at this point. When we added `?.` originally, it was its nature that it would not execute code that was now unnecessary due to the receiver being null. For example: Settings?.SetRetryPolicy(new ExponentialBackoffRetryPolicy()); This would already not run anything on the RHS of the `?.` if `Settings` was null. So this featur…
But also, reading the code will mean keeping track of (slightly) more possible outcomes.
Re: What's New in C# 14: Null-Conditional Assignments
#139Strange that they chose to make it `a?["b"] = c;` rather than to use the same syntax as TypeScript (`a?.["b"] = c;`). Honestly I like the C# better, and I was initially not super thrilled about the choice to include the period in the TS case, but since there is some overlap between the language design teams I assumed they would be the same.
Re: What's New in C# 14: Null-Conditional Assignments
#140The thing I enjoy most about C# is the depth/levels of progressive enhancement you can do. Let's say in the first instance, you write a proof of concept algorithm using basic concepts like List , foreach, stream writing. Accessible to a beginner, safe code, but it'll churn memory (which is GC'd) and run using scalar CPU instructions. Depending on your requirements you can then progressively enhance the memory churn,…