4 ms·
(GP here) > Isn't the fact that it's just sugar a huge benefit? My main gripe is that I cannot remember what is allowed and not allowed between multiple versi
by hirvi74 11d ago
(GP here)
> Isn't the fact that it's just sugar a huge benefit?
My main gripe is that I cannot remember what is allowed and not allowed between multiple versions of the same language. On a daily basis I hop between apps versioned in .NET Framework 4.8 all the way to .NET 10. I have to constant remember, are nullable types allowed here? What about 'new(); vs. new Object();', new collection syntax, new switch syntax, new extensions syntax, etc..
Plus, I just find it obnoxious that the same thing can be written so many different ways. I can think of 7 ways to assign a new empty List<T>.
List<T> foo = new List<T>();
var foo = new List<T>();
List<T> foo = new();
List<T> foo = new List<T> { };
var foo = new List<T> { };
List<T> foo = [];
var foo = (List<T>)[];
There are probably more that I am forgetting. What irks me most is Java is older than C#, and from what I can remember, it is not nearly this ridiculous in terms of syntactical sugar. So, what is the true benefit behind all this sugar? It hardly saves any keystrokes in the age of autocomplete in IDEs.
I am inclined to believe most of the sugar is an attempt to make the language appeal to a newer generations of programmers. But I would argue features are more attractive than syntactical sugar. I believe Rust is truly impressive language. In my opinion, its syntax is uglier than sin, but that does not seem to deter many from using Rust.
- troupo 11d agoSome of those ways come from just plain object initialisers. Which are amazing and sorely needed in Java. That's why you get `new List<T> { };` Because it could be `new ComplexObject { <fileds and properties> }`. Same for `new`. Some come from type inference which Java also has. That's why you can have `List<T> foo = new List<T>();` and `var foo = new List<T>();` It's not really "7 ways to assign a new empty List<T>". It's "7 ways to create an object", and Java several of them, too.
- samus 10d ago> Some of those ways come from just plain object initialisers. Which are amazing and sorely needed in Java. They really aren't, and they are IMHO an antipattern since they break encapsulation. One might argue that encapsulation doesn't matter with mere data classes, but Java will cater to that use case by introducing withers.
- troupo 10d ago> They really aren't, and they are IMHO an antipattern since they break encapsulation. They don't. Java had to come up with the extremely verbose builder pattern for the exact same thing. And withers are basically the same tedious manual builder pattern, just with a different name. For withers C# just has the with keyword: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/with-expression https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
- peterashford 9d agoYeah, this is the C++ kitchen sink approach and its the reason I don't favour rushing features. Never a second chance to get it right the first time