From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 22,600 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

- [Instructor] 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 current time. That's the only change I made. Now, the code will work exactly the same. And 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. Yeah, his 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. ReturnTheTimePlus 5-minute Interval. I have another one called…

Contents