From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Example of an impure function - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Example of an impure function
In this example, we'll look at a useful function, but it's considered impure from the functional programming perspective. So the code is in this method called GetCurrentTimeRoundedUpToCustomMinuteInterval. So I'm passing in a value of four minutes of interval time, and it'll take my system clock on my computer and take the current time, and round it up to the nearest minute based on this interval. So when I run it on my computer at the moment, I can see it's 8:07 on my clock and it's rounded it up to 8:08. And when this gets to 50 and when it gets to 60, I should say we're back to zero. We'll see this value change. Now it's rounded it up to the next four-minute interval. And it would be 16 after that, and so on. Now this is a useful function perhaps for your application. But it's impure because given the same value of four, I get back different results. And the reason why is easy to see when you take a look at the function itself, it's over here. Here's the inbound parameter called…
Contents
-
-
-
-
(Locked)
Function categories3m 18s
-
(Locked)
Write pure functions for better code3m 52s
-
(Locked)
Example of an impure function1m 39s
-
(Locked)
Refactor impure function to pure2m 21s
-
(Locked)
Reduce code side effects2m 18s
-
(Locked)
Refactor side effect example1m 4s
-
(Locked)
Don't mutate input arguments1m 8s
-
Use an immutable argument3m 41s
-
(Locked)
What about impure class properties3m 26s
-
(Locked)
-
-
-
-
-
-
-
-
-