From the course: Advanced C#: Functional Programming Patterns

Why expressions are better for functional programs - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Why expressions are better for functional programs

- [Instructor] One fundamental difference between functional and imperative style is that imperative code relies on statements; functional code relies on expressions. Therefore, consider how you can write your code to favor expressions over statements. Languages like C# make extensive use of statements. We often program with if statements and for statements. By nature, statements perform actions and have side effects. Expressions, by contrast, represent a value. In many cases, this value is the result of a calculation. But that is only a subset of what C# considers an expression. Another way of stating this is an expression yields a value and can be used in places where a value is expected. For example, an integer variable is an expression, a representation of a value to use in our code. We can change the value in the variable by assigning a literal value, performing a calculation, or invoking a function. If we refactor our coding to more expression friendly, it'll be more functional. These comments I took from the Microsoft documentation. It tells us that expressions can consist of a literal value, a method invocation, an operator and its operands, and something called a simple name. And then it explains that simple names can be the name of a variable, a type member, a method parameter, a namespace, or a type. Now, as I said earlier, an expression yields a value and we can use it anyplace where a value is expected, so we tend to use variables to hold the value. Here's a string variable and an int variable, but these are also considered to be expressions. Remember the name of a variable is an expression, though you and I don't say that. We say, "I'm declaring a variable here." We don't say, "We're declare an expression." Once you have the variable, then I can fill it with another expression result, so here I'm going to Int 32 MaxValue, which is a type member and I'm using that to determine what the maximum value is for an Int 32 and it's running that in the variable. Literals are also expressions. Here's a string literal and here's a numeric literal, an int literal in this case. And since variables are expressions, what we're doing here is declaring a new variable on line 32 called sayHello, and then I'm assigning the contents of this expression or variable to the new variable. Invocations are also another way of doing an expression, so here I'm calling ToUpper, doing a concatenation here. And I think line 40 is what many of us as programmers think of as an expression. We're doing a calculation here that's doing some mathematical operations and then it's returning the results. Yes, this is an expression. It's just one type of expression. Remember that operators and operands are also expressions. Here I'm using the greater than operator. I have the literal expression on one side and the variable expression on the other side and I'm assigning that to this variable. Here's another operator. This is called the conditional operator, this one here. And we'll talk more about that in the next chapter, but this is one way of assigning a value based on a condition. So that gives you a quick overview of the types of expressions available in C#.

Contents