Another reason I don't like LINQ
-
I find that adding line breaks makes it a lot easier to read. Remember, you are writing for the next person to touch the code, not the computer. It is missing some Elvis operators, before the Where, the Select, and the ToArray, along with providing a value if the null propagates to the end. Also, you don't need the ToArray() as AddRange takes an IEnumerable<T>.
Columns
.AddRange(
obj.GetType()
.GetGenericArguments()
.FirstOrDefault()?.GetProperties()// need the Elvis operator ?.Where(p => { return p.GetCustomAttributes(true) .OfType() .FirstOrDefault()?.Browsable ?? DefaultBrowsableState; }) // need the Elvis operator ?.Select(p => { return new ColumnHeader() { Name = p.Name, Text = p.GetCustomAttributes(true) .OfType().FirstOrDefault()?.DisplayName ?? p.Name }; }) // don't need ToArray(), but need to provide a non-null value for AddRange() ?? Array.Empty()
);
"Time flies like an arrow. Fruit flies like a banana."
In my defense I didn't write that, nor run it through autoformat yet.
Real programmers use butterflies
-
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
It's not really LINQ's fault, it's the chaining of multiple commands into a single statement and also a little of your own inexperience; given that error message many would know exactly where to look.
-
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
A couple of possibly interesting bits of feedback, assuming that code comes from here: - Adding it to a WinForms app created with .NET Core 3.1 or .NET 5 and turning on nullable reference types finds 17 potential accidental nulls in the code from that SO post. But the Columns.AddRange call itself isn't one of them because WinForms wasn't built with NRT enabled. So the compiler decides it can't say one way or another if passing a null
values
argument toAddRange
is okay. - Resharper catches the potential error whether you're using .NET Core/.NET 5 or .NET Framework. It even suggests a fix. The static analysis it's doing must look atAddRange
and notice that the first thing that method does is throw an exception ifvalues
is null. -
I find that adding line breaks makes it a lot easier to read. Remember, you are writing for the next person to touch the code, not the computer. It is missing some Elvis operators, before the Where, the Select, and the ToArray, along with providing a value if the null propagates to the end. Also, you don't need the ToArray() as AddRange takes an IEnumerable<T>.
Columns
.AddRange(
obj.GetType()
.GetGenericArguments()
.FirstOrDefault()?.GetProperties()// need the Elvis operator ?.Where(p => { return p.GetCustomAttributes(true) .OfType() .FirstOrDefault()?.Browsable ?? DefaultBrowsableState; }) // need the Elvis operator ?.Select(p => { return new ColumnHeader() { Name = p.Name, Text = p.GetCustomAttributes(true) .OfType().FirstOrDefault()?.DisplayName ?? p.Name }; }) // don't need ToArray(), but need to provide a non-null value for AddRange() ?? Array.Empty()
);
"Time flies like an arrow. Fruit flies like a banana."
Matthew Dennis wrote:
I find that adding line breaks makes it a lot easier to read
:thumbsup:
-
A couple of possibly interesting bits of feedback, assuming that code comes from here: - Adding it to a WinForms app created with .NET Core 3.1 or .NET 5 and turning on nullable reference types finds 17 potential accidental nulls in the code from that SO post. But the Columns.AddRange call itself isn't one of them because WinForms wasn't built with NRT enabled. So the compiler decides it can't say one way or another if passing a null
values
argument toAddRange
is okay. - Resharper catches the potential error whether you're using .NET Core/.NET 5 or .NET Framework. It even suggests a fix. The static analysis it's doing must look atAddRange
and notice that the first thing that method does is throw an exception ifvalues
is null.Ryan Peden wrote:
Resharper catches the potential error
Ryan Peden wrote:
It even suggests a fix.
:thumbsup:
-
There's a pretty large gulf between not liking something and hating it. If I don't like it, it just means I'll avoid using it, and sometimes complain about it. If I hated it I'd probably actively seek to destroy it somehow, and that would be a fight with Microsoft I'd rather not invest in, especially since it's one I couldn't win.
Real programmers use butterflies
Every technology has its place, For simple filtering and selection Linq is quite fine. But for this I would have used other techniques. (Admittedly mixed with linq)
Wrong is evil and must be defeated. - Jeff Ello
-
A couple of possibly interesting bits of feedback, assuming that code comes from here: - Adding it to a WinForms app created with .NET Core 3.1 or .NET 5 and turning on nullable reference types finds 17 potential accidental nulls in the code from that SO post. But the Columns.AddRange call itself isn't one of them because WinForms wasn't built with NRT enabled. So the compiler decides it can't say one way or another if passing a null
values
argument toAddRange
is okay. - Resharper catches the potential error whether you're using .NET Core/.NET 5 or .NET Framework. It even suggests a fix. The static analysis it's doing must look atAddRange
and notice that the first thing that method does is throw an exception ifvalues
is null.Sounds like nullable reference types is reason enough in itself to upgrade to .Net 5.
Wrong is evil and must be defeated. - Jeff Ello
-
Yeah I sorted it out. It was being applied to an instance of the wrong class. The code that's using it is ridiculously complicated, and something small was out of place. This error was the end result. I still think it's suitable for the purposes of this rant. :) Such is life sometimes. I'm working with lots of Other People's Code(TM) at the moment. It's not so much that any one of them is particularly bad, so much as gluing together so many different paradigms is well.. as you can expect. But the main complication of it all is making it designable so my client can open it up in visual studio and tweak it, because he likes to be able to. He can code some, but I'd prefer he keep his mitts off what i write. I can deal with him using the designer. It works for both of us because he's afraid of my code anyway, and that way he doesn't have to bug me for little changes, but sometimes the code to make it all go properly is nasty.
Real programmers use butterflies
All this complaining about LINQ and AddRange, a non-LINQ function, was the problem? :laugh: Nothing a good old stack trace couldn't have pointed out by the way :zzz:
Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript
-
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
The main challenge of LINQ that you don't like (I believe) is one that is quite difficult to accept (for me too) and for many of us who originally learned imperative programming. The main challenge is that LINQ is a declarative construct within an imperative programming language. You probably know this already. Declarative languages expect you to tell them what you want (not how they should do it). However, how they actually get you want you want is hidden (black box). So when they fail, it is quite difficult to know where / why they failed. With imperative programming you have written the steps to get the thing to do the thing and you know where the problem is. Squirrel From A Different Dimension This may not help but it'll help you understand that even though the animal you are wrestling with looks very much like a normal squirrel, it is actually a squirrel that lives in another dimension and it behaves quite differently. :laugh:
-
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
p.GetCustomAttributes(true).OfType()
I believe OfType<>() is returning a null.
honey the codewitch wrote:
The LINQ isn't really that bad here.
Actually, it's rather horrific. Not to mention what looks like completely unnecessary and probably wrong FirstOrDefault() usage, the reflection usage which looks like it could be simplified, and other confusing things. And the probably useless ToArray().
honey the codewitch wrote:
even though I didn't write it
Whew! ;)
Latest Articles:
Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions -
The underlying issue is that this garbage is a "one-liner". One-liners are darn near impossible to debug. LinQ invites, and even encourages this crap into the code.
And Linq combined with extension methods makes one-liners even more fun! I write them frequently! :laugh:
Latest Articles:
Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions -
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
Yet another example of the school of thought that replacing
if
,for
, andwhile
by declarative constructs results in code that is simplicity itself to read, robust, and completely error free. In a pig's eye.Software Zen:
delete this;
-
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
Exception that leaves you clueless.. yes, it happens :/ The null reference exception is a big culprit with those.. :/ However, I dare say that (perhaps) Stacktrace could help provide a bit more helpful insight here?!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Exception that leaves you clueless.. yes, it happens :/ The null reference exception is a big culprit with those.. :/ However, I dare say that (perhaps) Stacktrace could help provide a bit more helpful insight here?!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
I mean, yes. eventually. but it (as someone mentioned earlier but I forget who) reminds me of C++ template exceptions. The code and the exception couldn't seem more unrelated on the surface, and code that isn't communicative at face value is problematic, which is basically my point here. I have the same complaint about C++ templates and generic programming despite being in love with GP. I guess for me the power of GP outweighs the incomprehensibility of it but I just don't feel that way with LINQ.
Real programmers use butterflies
-
Here's my error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'... for this mess:
Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
{
return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
}).Select(p =>
{
return new ColumnHeader()
{
Name = p.Name,
Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
};
}).ToArray());The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.
Real programmers use butterflies
is that custom generated? because if you had to write that -"their's your sign"
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
I find that adding line breaks makes it a lot easier to read. Remember, you are writing for the next person to touch the code, not the computer. It is missing some Elvis operators, before the Where, the Select, and the ToArray, along with providing a value if the null propagates to the end. Also, you don't need the ToArray() as AddRange takes an IEnumerable<T>.
Columns
.AddRange(
obj.GetType()
.GetGenericArguments()
.FirstOrDefault()?.GetProperties()// need the Elvis operator ?.Where(p => { return p.GetCustomAttributes(true) .OfType() .FirstOrDefault()?.Browsable ?? DefaultBrowsableState; }) // need the Elvis operator ?.Select(p => { return new ColumnHeader() { Name = p.Name, Text = p.GetCustomAttributes(true) .OfType().FirstOrDefault()?.DisplayName ?? p.Name }; }) // don't need ToArray(), but need to provide a non-null value for AddRange() ?? Array.Empty()
);
"Time flies like an arrow. Fruit flies like a banana."
Matthew Dennis wrote:
It is missing some Elvis operators
It's not. If the
FirstOrDefault
returnsnull
, theGetProperties
and subsequent calls won't execute. If it returns non-null,GetProperties
will never returnnull
:Type.GetProperties Method (System) | Microsoft Docs[^]:
Returns An array of PropertyInfo objects representing all public properties of the current Type. -or- An empty array of type
PropertyInfo
, if the current Type does not have public properties.Similarly,
Where
will never returnnull
. If the input sequence isnull
, it will throw an exception. If the input sequence is empty, or there are no matching elements, it will return an empty sequence. And the same applies toSelect
- it will either throw an exception, or return a non-null sequence.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
A couple of possibly interesting bits of feedback, assuming that code comes from here: - Adding it to a WinForms app created with .NET Core 3.1 or .NET 5 and turning on nullable reference types finds 17 potential accidental nulls in the code from that SO post. But the Columns.AddRange call itself isn't one of them because WinForms wasn't built with NRT enabled. So the compiler decides it can't say one way or another if passing a null
values
argument toAddRange
is okay. - Resharper catches the potential error whether you're using .NET Core/.NET 5 or .NET Framework. It even suggests a fix. The static analysis it's doing must look atAddRange
and notice that the first thing that method does is throw an exception ifvalues
is null.Ryan Peden wrote:
The static analysis it's doing must look at
AddRange
and notice that the first thing that method does is throw an exception ifvalues
is null.I suspect it's more likely that it has "external annotations" for the type in question. External Annotations—ReSharper[^] R# is already slow enough; if it had to do static analysis on every framework method you called, it would be completely unusable. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
p.GetCustomAttributes(true).OfType()
I believe OfType<>() is returning a null.
honey the codewitch wrote:
The LINQ isn't really that bad here.
Actually, it's rather horrific. Not to mention what looks like completely unnecessary and probably wrong FirstOrDefault() usage, the reflection usage which looks like it could be simplified, and other confusing things. And the probably useless ToArray().
honey the codewitch wrote:
even though I didn't write it
Whew! ;)
Latest Articles:
Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread SubscriptionsMarc Clifton wrote:
I believe OfType<>() is returning a null.
Nope. :)
OfType<T>
will throw an exception if the input sequence isnull
. If the input sequence is empty, or doesn't contain any matching elements, it will return an empty sequence. It can never returnnull
. Also, the elements within the returned sequence will never benull
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I mean, yes. eventually. but it (as someone mentioned earlier but I forget who) reminds me of C++ template exceptions. The code and the exception couldn't seem more unrelated on the surface, and code that isn't communicative at face value is problematic, which is basically my point here. I have the same complaint about C++ templates and generic programming despite being in love with GP. I guess for me the power of GP outweighs the incomprehensibility of it but I just don't feel that way with LINQ.
Real programmers use butterflies
Try taking a look at the stack trace from an
async
method (prior to .NET Core 2.1). :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Matthew Dennis wrote:
It is missing some Elvis operators
It's not. If the
FirstOrDefault
returnsnull
, theGetProperties
and subsequent calls won't execute. If it returns non-null,GetProperties
will never returnnull
:Type.GetProperties Method (System) | Microsoft Docs[^]:
Returns An array of PropertyInfo objects representing all public properties of the current Type. -or- An empty array of type
PropertyInfo
, if the current Type does not have public properties.Similarly,
Where
will never returnnull
. If the input sequence isnull
, it will throw an exception. If the input sequence is empty, or there are no matching elements, it will return an empty sequence. And the same applies toSelect
- it will either throw an exception, or return a non-null sequence.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
actually that is incorrect. Only the chain of conditional operators is short circuited so if you have the expression
A()?.Bb()?.C().D()
and A() returns null the B and C will not be executed but there will be an attempt to execute D on a null object. Its sort of like async. You need to go all the way down. [Member access operators and expressions - C# reference | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-)
"Time flies like an arrow. Fruit flies like a banana."