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.
Write pure functions for better code - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Write pure functions for better code
I've said this several times in this course. 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. Let's look at what purity means first, then we can discuss the tradeoffs and why it's 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 returned value is based on the inputs. When a function is called with a set of inputs, calling it again 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. Effects themselves are desirable. What is undesirable is for a function to cause a side effect. In other…
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)
-
-
-
-
-
-
-
-
-