From the course: Advanced C#: Functional Programming Patterns

Write pure functions for better code - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Write pure functions for better code

- [Instructor] Pure functions are a key part of functional programming. An application made with them will be robust and testable. Yet in real world programming there are many operations that don't match the purity model. This tension is one reason that programmers drift away from functional programming ideals. Let's look at what purity means first, then we can discuss the trade offs and why it is still a good idea to strive for pure functions when possible. A pure function is defined by two characteristics. First, the function returns a value that is calculated in the function. The return value is based on the inputs when a function is called with a set of inputs, calling it again later with the same values results in the same output. Second, the function does not have any observable side effects. Most applications and code libraries have effects. Let's be clear, your code is going to read and write and update data and output that information. The facts themselves are desirable. What is undesirable is for a function to cause a side effect. In other words, the function does his calculations and then perform some action that affects other parts of the program. Let's look at a pure function written in C Sharp. This is a really simple, pure function. It's called calcDiscount, it returns a decimal value and it takes two decimal parameters the amount and the discount rate. It has a single line of code online 15 that performs the operation. So it's obvious from looking at this code that the operation is based entirely on the input parameters and there is no side effects, there is no external data used in the method, only the value is past in effect the output value. Now I can test this by writing a unit test which I've done over here in this project. It's in this test, Test CS file, it's in a class called APureFunction Should, the name of my test method is ReturnedSameResults WhenSameInputs. What I'm doing here is instantiating my examples class, setting two variables the sales amount and the discount amount. And then I'm calling CalcDiscount twice because I need two values to compare to see that every time I call it, I get back the same results with the same inputs. So I call it twice and then I have two assertions. On line 21, I assert that the calculation is correct, so here's the calculated value and here's the result. And then I also assert that result one and result two are the same. Now I'll open up test Explorer. It's over here on the side and I'll pin that. If you don't see that you can go to test, test Explorer. Here you can see my unit tests, I'll open up the tree, there is my unit test. I'll run it, and I get the green checkmark which means it ran successfully. That's our first look at a pure function. Now that you've seen a pure function example, what are the benefits from using them? Results from a pure function call are cacheable as the same input always yields the same output. With caching the expensive operations are only performed the first time the function is called. To be clear to utilize caching, you have to add it to your application. Pure functions make testing much easier. You saw that I ran a unit test in my example. We don't have to mark a payment gateway, we don't need to run a set up before the test and assert that the state of the world is right after each test. We simply give the function the test inputs and assert the correct output. We can run any pure function in parallel, since it does not need access to shared memory. And it cannot by definition have a raised condition due to some side effect. Also pure functions tend to be self-documenting as the functions dependencies are explicitly listed. In conclusion, it's best to strive for pure functions where possible. As the course progresses, we'll see that it's impossible to build application consisting only of pure functions. We'll see how to engineer your code to mitigate these issues.

Contents