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