Extension methods are an integral part of modern .NET and some of .NET’s best features such as LINQ. Unfortunately, a lot of developers get intimid...
For further actions, you may consider blocking this person and/or reporting abuse
Great article but what do you think about the allocations caused by LINQ?
As a game programmer I've been taught to avoid using LINQ, especially in scripts running in real-time on the main thread in order to avoid allocations and so sudden frame drops caused by the GC kicking in. Surely a game is not comparable to other types of programs and besides not every LINQ method necessarily allocates (I sincerely don't know). Maybe some of the allocations could be even avoided, like those caused by the Func passed by simply caching them, but then it would lose in readability.
It's a tool like many others .NET put you at your disposal but would you still suggest it for environments like games?
I use LINQ by default. However, I did find while profiling a roguelike's core AI routines that LINQ significantly added to the duration of the application. However, this was a core routine in a genetic algorithm that had to run millions of times.
My suggestion would be to go with LINQ until your data tells you to avoid it in key places, then make those optimizations.
(I emphasised the main purpose)
It's a very valid question. The answer is "I need to teach extension methods". If you have full control of all code, you're better off putting it in
BookBuilder
.Nice post.
I’m a fan of the Extension methods. Like in your examples, I combine them with the Builder pattern too. I think that this combination creates a readable Domain Specific Language (DSL) that could be used in the test case scenarios for an application. With such a DSL, we can build our System Under Test (SUT) in a very descriptive way.
I can't wait for your next article.
Cheers.
I've done that before and really like the power and simplicity of that, plus the way it makes tests more maintainable. I really should write about that at some point soon.
Great post Matt. Clearly explained and absolutely useful!
Cool! This article further solidifies my love for the Builder Design Pattern. It's one of the best ones out there, to be honest.
Thanks for the introduction to Extension methods. You packed some great information in there and in a easy to follow and understand way.
Slowly slowly languages incorporate more Smalltalk features and style.. and still fail to be as clean and expressive.
If you're ready to take it unto the next level, check out how you can use extension methods in some more creative ways.