.NET Looks at Functional Programming Techniques.
Tags: csharp, aspnetcore, entityframework
Published: 2020-04-04 15:46:21

Consder this code:

```csharp
 private ModelThing SaveThing(ModelThing modelThing)
        {

            Thing modelChangesAsEntity = modelThing.ToEntity(User.Identity.Name);
            Func<ConfigEntities.Thing, ConfigEntities.Thing> UpdateExisting = (existingThing) =>
            {
                modelChangesAsEntity.ID = existingThing.ID;              
                _store.Entry(existingThing).CurrentValues.SetValues(modelChangesAsEntity);
                _store.SaveChanges();                 
                return existingThing;
            }; 
            Func<int, ConfigEntities.Thing> findExistingByID = (modelID) =>
            {
                var entityfound = _store.ThingsControls.FirstOrDefault(item => item.ID == modelID);
                return entityfound;
            };
        
            Func<ModelThing, ConfigEntities.Thing> findSimilar = (model) =>
            {
                var found = _store.ThingsControls.FirstOrDefault(item =>
                      item.DisplayName == modelThing.DisplayName &&
                      item.ThingGroupID == modelThing.GroupID &&
                      item.Type == modelThing.Type);
                return found;
            };
            //work flow starts here
            var foundByID = findExistingByID(modelThing.ID);
            if (foundByID != null)
            {
                UpdateExisting(foundByID);
            }
            else
            {
                var found = findSimilar(modelThing);               
                if (found != null)
                {
                      var updated =  UpdateExisting(found);
                    _store.SaveChanges();
                    modelThing.ID = updated.ID;
                }
                else
                {
                    _store.ThingsControls.Add(modelChangesAsEntity);
                    _store.SaveChanges();
                    modelThing.ID = modelChangesAsEntity.ID;
                }
            }
            return modelThing;
        }
```

We see some functional programming styles above. One of them being a static helper method ToEntity().  The rest are de-marked by the Func keyword.

Yes it could be argued there's no real value here if we have the perspective these functions will always (throughout the lifetime of this application) always remain here.  On the other hand we are separating concerns within a method itself. We could have refactored to other methods which work perfectly.

The takeaway here is that prior to adding these functions we had a monolith of code in SaveThing method. A flow controlled solely on If/Then statements which are still there, but the code in each branch is simply a call to another function.

What's the point? Well we are demonstrating to any code reviewer that we are thinking ["SOLIDLY"](https://en.wikipedia.org/wiki/SOLID).  This is a pre-requisite to creating reusable toolboxes for the further usuages. Functions (and methods) should only do one thing!

Unfortunately, C# doesn't treat Functions as a first class citizen like a Class. Javascript does!

BTW I submitted code similar to this 7 years ago for a code review. Nobody liked it. I had to rip it out and put in method calls which for their eyes, at the time, was easier to take. 

I do admit that this example may be a bit much, but learning how to use Functions allows use to use Functions in method parameters, to "call-back" something, to get very specific information, at just the right time. 

JWP2020