From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Refactor impure function to pure

Refactor impure function to pure - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Refactor impure function to pure

I'll fix this impure function by refactoring it to only work with parameters that are passed into the function. The reason this function was impure is because of line 16. This relies on the system clock on my computer, and that varies every second on my computer, so it's impure. So what I did is I commented out line 16 and instead I added a second parameter to this method. Here it is. It's called startTime, and I use that in my calculation, not the currentTime. That's the only change I made. Now the code will work exactly the same. You see, it's in this case, I think I have a 12-minute interval set for this one. Let's go take a look over in Program.cs. Yes, this one's set for a 12-minute interval and I'm calling DateTime.Now. And that works. And the other benefit I get from this is testability. So now I can write a unit test. Here it is, ReturnTheTimePlus5Interval. I have another one called ReturnTheTimePlus3Interval. And you see that I'm instantiating my example class. I'm creating a…

Contents