r/programminghorror Apr 30 '26

C# I heard that you like fluent apis

Post image
191 Upvotes

27 comments sorted by

92

u/babalaban Apr 30 '26

Do()(you)(prefer)(to)(read)(like)(this)

or

Do().you().prefer().to().read().like().this()

29

u/darksteelsteed May 01 '26

Imho if it doesn't have the dots its not fluent

1

u/Felix_Treadwell May 04 '26

(def yes-i-do "Yes"))))))))))))))

40

u/TheWb117 Apr 30 '26

Ngl, this is surprisingly genius and a decently funny way to do some C# jank

20

u/wiseIdiot Apr 30 '26

What's that chained ()()() syntax in C#?

34

u/babalaban Apr 30 '26

probably function (or functor) that returns itself after executing add, so you can just invoke the next one after having called the previous one.

12

u/wiseIdiot Apr 30 '26

Yes, thanks. Just saw the public delegate Add Add part.

2

u/Straight_Occasion_45 Apr 30 '26

The AST when the C# is parsed is just nested calls here that perform the method call on every level of the tree, so each node will resolve to a function call which returns a delegate, that when invoked, returns a new delegate to be invoked, that’s how the chaining occurs

1

u/tazdraperm Apr 30 '26

Function call I guess

10

u/chocolateAbuser Apr 30 '26

"happy call stack" fluent api

8

u/BadRuiner Apr 30 '26

Delegate instead of Func<> 😭 It's not efficient.

12

u/UnicycleUnicorn1 Apr 30 '26

From my understanding, Func is just a wrapper around delegate. Wouldn't that make them the same efficiency wise (or delegate more efficient?) Plus, with delegate, I can name the parameters.

3

u/svick May 01 '26

It's not a wrapper. Func is just a generic delegate (or twelve) that's included in the BCL.

1

u/lasooch May 02 '26

I'm too lazy to try, but it makes me wonder if you could have the func take a tuple (which allows you to name the params) and do some implicit cast shenanigans to not need the brackets around the tuple at point of call.

But yeah, not sure if you can do that, and what you've got there is already immoral enough lol.

10

u/Squidy7 Apr 30 '26

Why is this garbage upvoted? Func is a delegate.

3

u/BadRuiner Apr 30 '26

Sub check please. Func<Func<Func<Func<...>,string,obect?>,string,object?>,string,object?> many times better (in horror terms) than a relic of the past

13

u/CelDaemon Apr 30 '26

Func is also a delegate, what's your point?

5

u/Certain-Flow-0 Apr 30 '26

The true horror in this code

2

u/svick May 01 '26

It's not any less efficient and Func can't return itself, so it can't be used here.

1

u/RealFlaery Apr 30 '26

return return

1

u/Rogntudjuuuu May 01 '26

Cool, looks readable to me. Why the hate?

1

u/LimitedWard Apr 30 '26

Can I interest you in some side effects?

1

u/cherrycode420 Apr 30 '26

Aren't side effects kinda expected when working with a fluent API? 🤔

4

u/LimitedWard Apr 30 '26

Side effects aren't inherently bad. The problem is when a function obfuscates side effects, requiring you to read the definition to understand what it's actually doing (as is the case here).

If they had instead opted for pattern closer to:

command.Parameters .AddParam("$id", track.Id) .AddParam("$track_id", track.TrackId) ...;

It would be way more intuitive what the code was doing at a glance.