Pure functions
Pure function is that:
- Always returns the same output given the same inputs.
- Does not modify the state of object or any external state.
- Does not have any side effects such as I/O operations, exceptions or modifying external variables.
- In other words, a pure function takes input, process it and returns the output without modifying anything else.
- Pure functions are easy to test and debug.
Examples
- Mathematical calculations
public int Add(int a, int b)
{
return a + b;
}
- String manipulation
public string Concatenate(string a, string b)
{
return a + b;
}
- Array operations
public int[] SortArray(int[] array)
{
return array.OrderBy(x => x).ToArray();
}
- Data transformations
public string ConvertToString(int value)
{
return value.ToString();
}
- Validation
public bool IsValidEmail(string email)
{
return email.Contains("@");
}
Impure Functions
Impure function is that
[Read More]