Posts

Showing posts from May, 2019

Immutability

Last time, we looked at pure vs impure functions and we said that functional programming favors pure over impure functions. Using pure functions leads to another concept: immutability . Immutability means that a variable can only have its value set once. After that, the value never changes. So much so that in functional programming, the phrase "assigning to a variable" is often replaced by "binding to a symbol", since the word "variable" is not applicable anymore. Let's consider a non-immutable example: // Declare a class named Car public class Car { public string Color { get; set; } public bool IsStarted { get; set; } } // Instantiate a new instance var myCar = new Car { Color = "Red", IsStarted = true }; // After that, change the values of the car's properties myCar.Color = "Blue"; myCar.IsStarted = false; The properties inside of the Car class are not immutable since they can be changed after...