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.
Function categories - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Function categories
In functional programming, we can categorize functions based on their behavior and effects. We looked briefly at some of these terms in the last chapter. Here are the main categories. A pure function always produces the same output given the same input with no side effects. They don't modify any external state or rely on anything other than their input parameters. An impure function can be considered the opposite of a pure function. It may produce different outputs even with the same input due to reliance on external state or randomness. It may cause side effects such as modifying variables, performing I/O operations like reading and writing files, or interacting with a database. Sometimes we must write impure functions, especially when doing I/O work. But the key takeaway for us is that we need to know the difference. Always strive for pure functions where possible and isolate the I/O functions from the pure ones. An effectful function is a function that produces side effects, hence…
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)
-
-
-
-
-
-
-
-
-