Which is more readable?

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
Pedantic, I know, but which do you think is more readable...and why?

Code:
this.SmartFiles.ForEach(t => ExtractInfoFromFilePayload(t));
            
this.SmartFiles.ForEach<FileSortCell>(ExtractInfoFromFilePayload);

Kind of a silly question, but I figured I would ask the board
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Neither is significantly easier than the other. They also don't appear equivalent, since I can't see what happened to the instance being yielded from the enumerator in the second example.
 

uclabachelor

Senior member
Nov 9, 2009
448
0
71
Pedantic, I know, but which do you think is more readable...and why?

Code:
this.SmartFiles.ForEach(t => ExtractInfoFromFilePayload(t));
            
this.SmartFiles.ForEach<FileSortCell>(ExtractInfoFromFilePayload);

Kind of a silly question, but I figured I would ask the board

I understood the first one. The second one looks like gibberish. So for me, the 1st one is more readable.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
the second is easier to read IMO.

reminds me of using comparators on a list of info
 
Last edited:

Kr@n

Member
Feb 25, 2010
44
0
0
Also, the two options have a kind of different feeling :

- the first would enumerate all items in this.SmartFiles and call ExtractInfoFromFilePayload on them
- whereas the second one will enumerate only the FileSortCell items in this.SmartFiles and call ExtractInfoFromFilePayload on them

This distinction seems purely academic if your SmartFiles is an IEnumerable<FileSortCell>, but feels different. At the least, the first option would not compile if SmartFiles was not an IEnumerable<FileSortCell> (and ExtractInfoFromFilePayload only takes a FileSortCell of course), and I think the second would.

EDIT : removed code tags, because they can't be used inline.
 
Last edited:

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I'd say they're both unreadable. Probably because I lack context of what this is, what the other types are, etc.
 

Oyster

Member
Nov 20, 2008
151
0
0
I can decipher both of them just fine. If I must choose, I'd go with the second, just because it points to the type inline.


I'd say they're both unreadable. Probably because I lack context of what this is, what the other types are, etc.

C# Anonymous Delegates and Lambdas
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
About the same for me. The one I would choose would depend on what each of the objects are and who I expect to use the code.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I can decipher both of them just fine. If I must choose, I'd go with the second, just because it points to the type inline.




C# Anonymous Delegates and Lambdas

I still don't get what's going on in the second one. A lambda would accept the yielded instance as an argument, i.e.,

this.SmartFiles.ForEach<FileSortCell>(t => ExtractInfoFromFilePayload(t));
 

Oyster

Member
Nov 20, 2008
151
0
0
I still don't get what's going on in the second one. A lambda would accept the yielded instance as an argument, i.e.,

this.SmartFiles.ForEach<FileSortCell>(t => ExtractInfoFromFilePayload(t));

Well, one way to think about the second call is to go back in time before lambdas existed. The ForEach<T>() method accepts an Action<T> delegate (whose signature is to accept a single parameter and not return a value).

When I look at the original method call (second call in the original post), I read it as: SmartFiles is an IList containing elements of type FileSortCell. I want to perform some action on every item in the list, so I am going to utilize ExtractInfoFromFilePayload method whose signature matches that of an Action<T> delegate (hence, the call to ForEach inline).

In a way, you can think about the first method call as being archaic and redundant... it is valid because lambdas and LINQ made it valid later on, not because it was meant to be primary functionality. I guess this last point may come across as being confusing... someone else may be able to explain it better.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Well, one way to think about the second call is to go back in time before lambdas existed. The ForEach<T>() method accepts an Action<T> delegate (whose signature is to accept a single parameter and not return a value).

When I look at the original method call (second call in the original post), I read it as: SmartFiles is an IList containing elements of type FileSortCell. I want to perform some action on every item in the list, so I am going to utilize ExtractInfoFromFilePayload method whose signature matches that of an Action<T> delegate (hence, the call to ForEach inline).

In a way, you can think about the first method call as being archaic and redundant... it is valid because lambdas and LINQ made it valid later on, not because it was meant to be primary functionality. I guess this last point may come across as being confusing... someone else may be able to explain it better.

Ah, right, yep. I see what you're saying.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |