DEV Community

Cover image for Using Extension Methods in C# to Build Fluent Code

Using Extension Methods in C# to Build Fluent Code

Matt Eland on January 18, 2020

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...
Collapse
 
silviosavino1 profile image
Silvio Savino

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?

Collapse
 
integerman profile image
Matt Eland

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.

Collapse
 
vasar007 profile image
Vasily Vasilyev

Next our example has us calling WrittenBy. Well, the BookBuilder class doesn’t define that method. In a normal application we’d probably just add the method to BookBuilder, but that wouldn’t let us play with extension methods here, so let’s pretend that the BookBuilder class is defined by some code we don’t control and can’t modify.

(I emphasised the main purpose)

Collapse
 
integerman profile image
Matt Eland

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.

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

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.

Collapse
 
integerman profile image
Matt Eland

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.

Collapse
 
loopdeez profile image
Loopdeez

Great post Matt. Clearly explained and absolutely useful!

Collapse
 
somedood profile image
Basti Ortiz

Cool! This article further solidifies my love for the Builder Design Pattern. It's one of the best ones out there, to be honest.

Collapse
 
roguecode profile image
John Breland

Thanks for the introduction to Extension methods. You packed some great information in there and in a easy to follow and understand way.

Collapse
 
aminmansuri profile image
hidden_dude

Slowly slowly languages incorporate more Smalltalk features and style.. and still fail to be as clean and expressive.

Collapse
 
tyrrrz profile image
Oleksii Holub

If you're ready to take it unto the next level, check out how you can use extension methods in some more creative ways.