First Class Functions: C# and JavaScript

Last week I found myself looking at two C# methods I’d written. They were nearly identical, with different names and they differed by the execution of one single predicate function at the very end to filter out exactly the kind of thing I was looking for.

I found this to be pretty dissatisfying.

I know that C# has first-class functions, so I could have cleaned this up in some way or another, but there are two issues that I ran into with this.

Two Issues

The first issue is that I find first-class functions in C# pretty clunky. Honestly. At least, I used to. This is a result of some bias in favor of another language (more on that in a moment), and because for a majority of my time spent writing C#, it felt like trying to shove square pegs in round holes. While I’m not above trying to use a mallet to accomplish that, it felt like something the rest of the team probably wouldn’t appreciate.

Speaking of the team, they’re at the heart of the second issue. I figured that two predictable methods that looked a lot like the rest of the methods in the project were probably a better idea than whatever it was I had in mind. As much as duplicating a couple of lines of code hurt me, personally, it was definitely the lesser of two pains relative to having something look wildly out of place.

Solutions

The second issue doesn’t have a great, immediate solution, which is fine. It’s important to recognize when you’re doing maintenance programming that you’re not slinging code in a vacuum and maybe consistency is valued over cleverness. Especially when this stuff is making money for the company that employs you. You know, “the hand that feeds” and all. There’s also always the possibility that I could make this change in a future pull request and it’d pass the code review just fine, but in the moment I figured that was best to do another time.

The first issue, however, does have a great solution, sort of. I simply had to go looking for the approach to accomplish what I wanted to accomplish. It’s probably time to disclose the bias I have with regard to how I think about first-class functions, though.

JavaScript

That’s right, it’s JavaScript! I’m really a big fan of how straightforward and easy it is to pass functions around in that language. A lot of that has to do with the type system and how you can really shoot yourself in the foot if you’re not careful. I understand it’s not perfect, but in this case, there is a gulf of difference between “perfect” and “convenient.”

I would definitely choose for similar convenience in C# if I could!

Some Good News

There is a similar convenience in C#. There are some important differences that will look a bit strange, though, if you’re used to the nearly “anything goes” style found in JavaScript.

First, if you’re not working with C# 10, you don’t get to have the compiler infer the type of the function you’re producing, so you don’t get to use “var” the way that you might like to. Instead, you’d have to define it a bit more like this:

Func<int, int, bool> compareInts = (int i, int j) => i == j;

If you’re working with C# 10, you can do this:

var compareInts = (int i, int j) => i == j;

This is all fine, but usually, you’re not working with this stuff inside of a single method body. The whole reason I started on this path is that I wanted to pass this stuff around, right? So, as far as I can tell, you have to provide that type information in the method signature, kind of like this:

private void ThisTakesAFunction(Func<int, int, bool> fancyFunc)
{
    //do stuff!
}

Are Those Contrived Examples?

In this case, no. I actually did wish to compare two integers, but the issue was that they were two different ways of identifying a particular object. The business case I was working with required one sometimes, and a different one at other times, mostly because this was intended as a validation tool to ensure that relationships had been set up correctly and I had to work with the existing workflow.

This approach would have been very useful, and I intend to keep it in my back pocket in case I see another problem where this would lead to a more readable solution with less repeated code! That would be tremendously exciting.

Published by Joe

I'm a software developer from Minnesota. I also ride bikes!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: