Functional programming follows the idea that programmers should use immutable datatypes, expressions, and functions to avoid mutating the state or data. Functional programming involves many more topics than what is discussed in this article. However, many modern languages are adding functional programming features because of the benefits they provide.
In the world of object-oriented programming, functional programming is truly more of an ideal than a standard since programmers will always encounter circumstances where updates or deletes are unavoidable, such as updating the user interface. However, this website incorporates functional programming language features, which has made the code more concise and readable. It forces developers to use best coding practices, such as preserving raw values and further separating UI logic from service logic. One possible antipattern is that a developer could arbitrarily split code into a large number of compossible functions and, unwittingly, make the code difficult to follow and understand. However, functional programming features have largely proven to be beneficial to the application’s readability and long-term maintainability.
The makers of C#, used to write this website, introduced a number of functional programming language features, including record types, switch expressions, lambda expressions, pattern matching, Language Integrated Query (LINQ), method extensions, function delegates, and others. The example below demonstrates a classic OOP method followed by the same condition evaluated using a functional programming approach.
A Conditional Example
public static string MutableCondition(int age)
{
string answer = string.Empty;
if (age <= 40)
answer = "You are too young.";
else
answer = "You are over forty.";
return answer;
}
public static string ImmutableCondition(int age) =>
age <= 40 ? "You are too young." : "You are over forty.";
Notice how the first mutates the answer by assigning an empty string and then assigning a different value and provides plenty of room for future developers to break the code by introducing changes. The second prevents mutation and provides little room for changes or breakage. Composable functions, which are similar to the second example, also allow the developer to plug in additional functions and reuse functions.
Other C# Examples
Extension Methods
public bool IsLetter(this char c) =>
c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
Property Patterns
public bool IsMeetingDay(DateTime date) =>
date is { Year: 2024, Month: 1, Day: 3 or 4 or 5 };
public bool IsWeekDay(DateTime date) =>
date.DayOfWeek is >= DayOfWeek.Monday and <= DayOfWeek.Friday;
Switch Expressions
public string GetRoomAvailability(int groupSize, DateTime date) =>
(groupSize, date.DayOfWeek) switch
{
( >= 1, DayOfWeek.Saturday or DayOfWeek.Sunday) => "This is scheduled for a weekend.",
( >= 1, DayOfWeek.Monday) => "All rooms are available.",
( >= 1 and < 20, >= DayOfWeek.Monday or <= DayOfWeek.Friday) => "All rooms are available.",
( >= 20, >= DayOfWeek.Monday or <= DayOfWeek.Friday) => "Not enough capacity exists.",
_ => throw new ArgumentException("Nothing scheduled.")
};
Language Integrated Query (LINQ)
public IEnumerable FindByName(List Names, string name) =>
Names.Where(e => e!.StartsWith(name));