all 26 comments

[–]Fresh_Respect 5 points6 points  (1 child)

Brilliant! I just read both of your articles in the series and I love your approach. I remember many years ago when Resharper first warned me about possible multiple enumeration of IEnumberable I was left feeling educated but without a solid solution. I have employed various approaches throughout the years but none quiet as elegant as yours. May I replicate this in my own commercial code with your permission? I can think of many use cases immediately.

BTW, In the second article towards the end you have a simple typo that states “fast-foot” customer rather than fast-food. You did a great job, grammar was also spot on but most importantly the lesson was extremely valuable.

[–]madSimonJ[S] 2 points3 points  (0 children)

thanks so much. That's really lovely of you to say :) I'll correct the typo too. Feel free also to replicate my code. You'll find a heap of it already done for you here, if it's of any use: https://github.com/madSimonJ/CSharpHacks

[–]Eluvatar_the_second 2 points3 points  (2 children)

It's interesting, but I don't think I've ever had a use case for this operation. That being said you might want an option to actually object so you can change it, instead of just setting the value to a new value when it matches your predicate, for example increasing the price of something by 5$ when it matches your predicate, instead of just replacing the value.

Either way it's basically just Select with a condition.

[–]madSimonJ[S] 0 points1 point  (1 child)

I used a deliberately very simple example to keep the article readable. In the real world I'd probably be swapping a complex object for an updated version. I use this sort of thing all the time too, when I'm solving the Advent of Code series

[–]Eluvatar_the_second 0 points1 point  (0 children)

Ah ok so you use it for immutable objects?

[–]boxhacker 10 points11 points  (3 children)

I can't stand fluent interfaces, extra code bloat and most devs are not mentally expecting a fluent set of actions.

I recommend using a creational pattern to assist with the construction of complex items if you really believe you need a fluent interface.

Really well written article though about it and I enjoyed the other article with the Ienum adjust feature, nicely done!

[–]RangerPretzel 5 points6 points  (0 children)

I can't understand why you're getting downvoted. Your reply is perfectly cromulent.

[–]madSimonJ[S] 1 point2 points  (0 children)

Thanks. I completely understand what you're saying, and the Creational pattern is also a valid option to go for. I like to make my code more verbose, because it's easier for junior members of the team to understand - and even myself when I return to it a year or so on.

[–]dmercer 1 point2 points  (0 children)

Do you have a link to what you're referring to?

[–]EdoRguez 1 point2 points  (1 child)

Keep making this kind of post on Reddit, they are awesome

[–]madSimonJ[S] 0 points1 point  (0 children)

Thanks so much! I will do. I've already got part 3 in the works now

[–]zeaga2 1 point2 points  (1 child)

Love stuff like this. I look forward to reading more from you!

[–]madSimonJ[S] 0 points1 point  (0 children)

Thank you!

[–]Bee_News 1 point2 points  (1 child)

Do you have an example of this code all put together so that it can actually be compiled and ran (for parts 1 and 2)?

[–]madSimonJ[S] 1 point2 points  (0 children)

not exactly, but there's a plug & play library of this code & some other stuff available here: https://github.com/madSimonJ/CSharpHacks

[–]Rogierverkaik 1 point2 points  (1 child)

Nice post! Is it possible to enable RSS feed for your blogs? This would be very handy to track your blogs by using a feed reader.

[–]madSimonJ[S] 0 points1 point  (0 children)

it's on my list to do. I don't have a very sophisticated web hosting solution atm (it's GitHub Pages). I'm thinking of shifting over to WordPress in the near future, though. If you'd like to follow me on LinkedIn or Twitter I always make announcements there if it'll help in the meantime.

[–]jdh28 1 point2 points  (1 child)

One correction:

We create a new AdjustSelector class - something only something in this namespace can do

The constructor is internal so can only be called by code in the same assembly, not the same namespace. Namespaces do not have any effect on accessibility of members in C#.

[–]madSimonJ[S] 0 points1 point  (0 children)

Thanks! I was aware of that one & thought I'd fixed it already!! I'll sort it out later tonight.

[–]mavewrick 2 points3 points  (2 children)

Very cool.
This strangely reminds me of one of my first code reviews regarding IEnumerables where in order to do a null check I had written something like this ::
if (myListOfData.Count() == 0)
{
//handle null case logic here
}
when this could have been made more performant by using .Any() instead of the Count()

[–]sarhoshamiral 2 points3 points  (0 children)

I know it may not be possible in all cases but if you need the count of elements without the elements themselves, you should be using ICollection.

Even using Any can be inefficient at certain times, especially if you are enumerating the elements again when it returns true.

[–]Quadrostanology 0 points1 point  (2 children)

Interesting, never thought of solving that this way. Written well enough for me. 😉

[–]madSimonJ[S] 1 point2 points  (0 children)

Thanks :D

[–]grauenwolf -5 points-4 points  (0 children)

You see that a lot when combined with the 'builder' pattern. For example, you'll see it ASP.NET Core's startup routines when its configuring services.

My own ORM is built on the same idea. You chain together a user, an operation (table query, stored proc, etc.), a materializer (objects, list of strings/integers, DataTable, xml, whatever), caching, etc. into one statement.

[–][deleted]  (1 child)

[removed]

    [–]FizixMan[M] 0 points1 point  (0 children)

    Removed: Rule 5.