From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 24,600 courses taught by industry experts.
Don't be making side effects - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Don't be making side effects
Side effects are any state changes that occur outside of a called function. A pure function, which is our ideal function, does the calculations and returns the results. There shouldn't be any other consequences of calling that function. In C#, a method on an object can change the internal state of that object instance, such as modifying a property value. This is an example of a side effect, because calling the method alters values outside of the method itself. A function has side effects if it does any actions on this list. Global state is any state that is visible outside of the function scope. Mutating global state is a side effect, and we've already seen that this is bad in the shared state discussion. Altering the values passed into a function is also considered a side effect. Throwing exceptions is a side effect. This is less obvious why this is an issue. A longer discussion of this topic is elsewhere in the course. Finally, any I/O operation is a side effect. Any interaction…