Placeholder Image

字幕列表 影片播放

  • Pierre functions are one of the quickest ways to make your code easier to understand.

  • But what exactly are Pierre functions?

  • Why do you want to use him?

  • And most importantly, how do we actually create pure functions?

  • We're gonna cover all that and more in this video starting now.

  • My name's Kyle and this is webbed.

  • I've simplified where I simplify the web for you.

  • So if that sounds interesting, make sure you subscribe to my channel for more videos just like this one.

  • So, as I mentioned in the beginning this video, we're gonna be talking about pure functions.

  • And the first thing we need to talk about in order to understand Pierre functions is to actually understand what pure functions are and why they got brought about and essentially, pure functions were brought about because of the idea of functional programming and functional programming.

  • The main concept of it is that you have functions in your program that are pierre functions.

  • That's essentially what functional programming is built on his peer functions, and what appear function is it is a function that does not have any side effects.

  • And it's a function that always returns The exact same thing every single time.

  • You give it the same inputs.

  • So every single time you call this function with same input, it always gives you the same output, and it affects nothing else outside of it.

  • It essentially works exactly the same as a math function.

  • Does two plus two.

  • You give it the inputs two and two, and it'll always return for no matter what, it doesn't affect anything outside of it.

  • And just always you give it two and two and it gives you four.

  • So that's a little bit of the theoretical side of what of pure function is.

  • But the easiest way to understand appear function is to take a look at some examples and to write some.

  • So that's what we're gonna do.

  • We're gonna jump into V s code and start writing some peer functions to get started.

  • I just want to look at a very simple example of a non peer function, essentially an impure function, and this is a function here.

  • Add element to array, and when it does is you just pass it in element and it's going to pen that element to the array, which is this global variable we have to find here and we can actually use this function over here.

  • If we could just say over here we wanna add element to array and we're gonna add four to our ray because right now, our raise 1 to 3 and if we hit enter, you can see that's being called And then what we can do is we could just come in here and look at our array and you can see our raise 123 and four now and so as we know, this function is working properly.

  • But it is not appear function, and it's breaking pretty much every rule of a pure function.

  • But the easiest one to see is that it relies on external information.

  • It's affecting things outside of the function because with a pure function, ah, function is going to take in some inputs and it's only going to operate on those inputs in order to create the output.

  • Like I said, with the math example, two plus two is four.

  • It only uses the inputs of two and two to get the output of four.

  • But this actually takes this array, which is not an input of this function and uses that when it returns.

  • So it actually uses something that's outside the function in order to create the return result, which means it's not appear function because it has side effects and it actually uses things outside of it.

  • So one easy way we can change this is we can make this except an array so we could just pass it in.

  • All right, here, we're gonna call it a and we're gonna say a dot push and that element.

  • So now what it's going to do is actually going to take in an array, and it's going to add an element to that array.

  • So let's save us over here.

  • It's going to clear so we can check our ray.

  • Whoops.

  • I spelled it correctly and you can see it's just 123 right now.

  • And if we call add element to array, we pass it in our ray and we pass is an element in our case for and we actually look at our ray again you can see now are raise 1234 Our function still works, and now our function on Lee depends on the inputs.

  • The input of a an element.

  • And so you may think, Okay, this is a pure function, right?

  • Because we're only taking the inputs to create the output.

  • But again, the problem is that this actually has a side effect.

  • It is changing the input A Here, As you remember, we passed in array and we add that four element to it and actually changed our global array variable with a pure function, you never want to change the inputs that you're adding into the element.

  • So here we have our inputs of our function.

  • We can never change them in appear function because that's a side effect.

  • It's actually changing something outside of the function that would be like if you did two plus two equals four.

  • And every time you did that, it changed the number 3 to 7.

  • It just doesn't work that way.

  • So it can't work in this instance.

  • And this is the thing that most people get wrong when they're creating pure functions.

  • It's fairly easy to wrap your head around the idea that you shouldn't use anything that is not an input in your peer function, so you can't use any global variables, for example.

  • But it's really hard to make sure you don't actually change your variables.

  • You need to make sure you treat the inputs as immutable variables essentially, that they cannot change.

  • So another way we can change this is instead of actually adding to that array, we want to just create a new rate and add that element to it.

  • And we could do that very easily, using the rest operator so we can just come in here and we can say we want to do a which is our current array.

  • We want to spread that over everything and add the element of the end of it.

  • Now, if you're not familiar with this in tax, you can check out my E s six video on rest and spread.

  • I'm gonna link it in the cards and the description for you.

  • But essentially what this is doing is taking our current array and adding a new element to it.

  • But it's actually creating a brand new array to do that.

  • And so what we need to do is we actually need to return that so we can come in here and we can say return because it's not changing our global variable So now if we say bet and we come over here, we'll check and see.

  • Our Ray is still 123 And if we call at Element to array, pass it in the array and we pass it on four, you can see it's returning our new array 1234 But our older Ray here is still exactly the same.

  • It did not actually change this of rape, and now that actually makes this function here appear function.

  • You can see it doesn't rely on any external state.

  • It only uses its inputs.

  • And it doesn't actually change any of those inputs because it creates a new array instead of using the existing array and adding on to it.

  • Another really important thing about appear function that this actually does do correctly is that you need to make sure that when you give it the same input, it always returns the same things.

  • So if we go up here and we call at element to array, it doesn't matter how many times in a row we call this function at element to array.

  • You can see it's always returning the exact same thing, and that's perfect.

  • That's what we wanted to do.

  • Now in general, it's pretty difficult to make a function, actually do something different when you call it with the same inputs if you aren't actually doing anything with stuff outside of the inputs.

  • So, for example, in here we're only using the inputs, so it's hard to make it actually do something different because we're always using just the inputs and nothing outside of it but a function such as math dot random.

  • You're always going to be getting a random number every time you call this.

  • So this is a function that even though it's using the same exact inputs, it's actually returning a different output to us each time.

  • So, for example, inside here we also upended math at random to the end of this.

  • And now, if we save this and we run ad element to array with our array and element for you can see that it's actually giving us different return results when we call it because we're using math at random.

  • So even though we're using only the inputs we get and we're not affecting anything outside of our function, since our function actually gives a different result every single time we call it with the same inputs.

  • It's no longer appear function.

  • It has to always return to the same output every time you give it the same inputs.

  • And now you're probably thinking to yourself, Why is this important?

  • It just seems like it makes it more difficult to write our code.

  • Since we have to worry about not changing our inputs, we have to worry about not affecting things outside of our function.

  • It just seems really much more difficult to work with.

  • But the idea of adding all of these extra constraints on to the pure functions you create is that it makes them so much easier to use.

  • You know that every time you call add element to array, you're not affecting anything at all, except for the inputs you pass in and the output you get out, you're not gonna change anything accidentally.

  • You don't have any side effects happening.

  • You're not gonna be changing your state.

  • All it's doing is saying here is something that I'm going to give you and then give me something else.

  • Essentially, you can replace any pure function with just the actual code written out and it makes it really, really easy.

  • You can just replace it with the output.

  • For example, we could replace add element to array call so we could replace this ad element to array with array.

  • And four.

  • We could essentially replace this exact code, which is saying 1234 And that's the power of using these functions.

  • You can always think of them as just a way to neatly wrapped a small amount of code.

  • And because these codes don't have a side effect, they don't have any extra stuff, such as mutating the variables you pass into it.

  • It makes them incredibly easy to test.

  • Which, in my opinion, is the most important part of Pierre functions is that you could test them so easily because, you know, given this input, I could always get this output.

  • There's no side effects.

  • You have the test.

  • You don't have to mock anything.

  • It's dead.

  • Simple to unit test, and having strong unit tested code or just tested code in general is going to make your life so much easier moving Ford when you try to re factor your code in the future.

  • Now, one downside to pure functions is that you can't affect anything outside of them, which is also one of the benefits.

  • But the reason that this is a detriment is because it makes it very hard to do things such as calling a database, because calling a database and changing some of the records is changing something outside that function so appear function can access the database.

  • It can't access files.

  • It can't access anything outside of it.

  • So your code needs to have some way to interface with these other things.

  • So you do need in pure functions in order to do those things.

  • But I highly recommend making as much of your code is possible, pure functions just because how dead, simple there to use and how easy it makes your coat to test on Lee make the small things that actually need to interact with other things outside the function.

  • Make those in Pierre functions, but try as hard as you can to make everything else appear function.

  • I promise you, once you start getting into the habit of doing that, it's going to make your life so much easier because I can't tell you how many times I wrote code that was an impure function.

  • It did so many things inside of it.

  • And then I need to do change one thing in my application, And I had to go through and change a bunch of different functions because I didn't know if something was going to break because I had all these side effects all over the place.

  • It was just an absolute mess.

  • I've run into that so many different times, and it's so much easier when I know if I need to change, appear function, I could just change it.

  • And everything else is just going to work, since it doesn't affect anything else at all.

  • And that's all there is to peer functions.

  • If you enjoyed this video, make sure you check out other similar videos linked over here and subscribe to my channel for more videos just like this one.

Pierre functions are one of the quickest ways to make your code easier to understand.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

10分鐘內學會純函數 (Learn Pure Functions In 10 Minutes)

  • 2 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字