Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn about the core concepts of the functional programming style.
-
0:00
[MUSIC]
-
0:04
Before we get into using methods syntax with LINQ.
-
0:07
I wanna over a set of features in C# that fit the style of functional programming.
-
0:14
Functional programming languages focus on computations
-
0:17
as opposed to executing a series of commands.
-
0:19
C# is an object oriented language.
-
0:22
However, many of the object oriented languages out there have adopted elements
-
0:27
of functional programming and give us the ability to program in a functional style.
-
0:32
LINQ queries are inherently functional because as we write our queries,
-
0:36
we are expressing what we want, and not how we want it.
-
0:41
If all this sounds a bit confusing don't worry,
-
0:44
I'll show you some examples that should help clear it up.
-
0:47
We'll only touch lightly on functional programming for LINQ but
-
0:50
we'll cover it in more detail in a future course.
-
0:54
There are some guidelines that functional programming languages follow, and
-
0:58
it can be helpful to keep them in mind when writing code.
-
1:01
The big one is that there are no side effects.
-
1:04
When you run a function,
-
1:05
it should not modify anything outside of the function scope.
-
1:09
For instance, if we have a function that evaluates a collection that exists
-
1:13
outside the function, it shouldn't add or remove anything from that collection.
-
1:18
Another concept is that functions are first class citizens.
-
1:22
This means that functions are treated like any other type and
-
1:26
can be passed around as objects.
-
1:28
We'll be going over delegates and other function types in C# that let us do that.
-
1:34
We also wanna make sure that a function has the smallest scope and performs
-
1:38
the least amount of work possible, which is usually computational in nature.
-
1:43
We should be able to treat it like a piece of data.
-
1:45
The function takes input and gives us the result of the computation.
-
1:50
We could replace the function call with its expected result and
-
1:54
everything would run the same.
-
1:56
This is a good practice, in general.
-
1:57
But it is definitely showcased when applying functional programming concepts.
-
2:02
And last but not least is the use of expressions.
-
2:06
For instance, instead of creating a variable without a value and
-
2:09
then running some kind of logic to assign its value.
-
2:13
You use an expression to assign a value as you create it.
-
2:16
This concept goes very well with LINQ.
-
2:19
Applying these concepts in your code has huge benefits.
-
2:23
Your code becomes more readable, easier to maintain and easier to test.
-
2:28
However don't feel like you have to use functional programming just for
-
2:32
the sake of it.
-
2:33
You can take these concepts and
-
2:35
apply them in your applications if it's the right fit.
-
2:38
The goal is to help you be a better programmer.
-
2:41
You don't have to relearn how to program in a functional way but you can
-
2:45
take from these concepts and write code that's cleaner and easier to maintain.
You need to sign up for Treehouse in order to download course files.
Sign up