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.

Reduce code side effects

Reduce code side effects

- [Instructor] One of the principles of functional programming is to avoid side effects in pure functions. A side effect is a change in the system state or an observable interaction with the outside world, in other words, outside that function. Here's a list of things that a function should not do to avoid side effects. It shouldn't mutate shared state. It shouldn't mutate its input arguments. It should not throw exceptions. And it shouldn't perform any I/O operations. Here's a simple example up on line nine, I have an int variable underscore counter that's set to zero. You should already be thinking that because this is a class level variable, it can be shared across any of the methods inside this type. And that is a flag that tells you that this is mutatable across functions, so it's a problem. And you can see, this simple code here, in this UpdateByTwo method is updating it by two and UpdateByFive is updating by…

Contents