The full video and many others like it are all available as part of our Frontend Masters subscription.

Brian Lonsdorf

Brian Lonsdorf has taught several workshops and training courses on functional programming with javascript. He'a a regular speaker at conferences and helps organize/host FP events around the Bay Area.

Brian Lonsdorf

SFJS Co-Organizer

The first part of creating silence in your code is omitting needless names. This process can help separate inputs from the environment. Joe provides an exercise to help demonstrate this. He shows how testing a "daysThisMonth" function is much more difficult that testing a "daysInMonth" function.

Get Unlimited Access Now

The first bit of the silence is to emit the listener names and the way we're going to do that is like I said through the separations and recognitions So this is maybe a little small I can zoom in a time so we can see it but this is an example of the first type of separation we seeing these a lot.

So the first separation we can do is to think to ourselves we have functions and we would like to separate the things the function is acting on its inputs from just random things in the environment like if your function wants to you can go ask for things you know it can just go out there and and from the signature from the arguments it's not really apparent what it uses it would be really cool if as we'll see what it uses is it because it is explicit.

So here we have two functions the one on the left is days this month. So it does some day. Calculations it just tells you how many days a month but it has a secret input which is time itself which is kind of weird way to think about it.

Like if you varies but really libraries they have a thing called Time Cop So if you want to write a test to make that test reproducible on a function that works with time you have to like fake out the system time or just kidding it's a year ago you know I run my code.

I hope it works its stead of making it really explicit like this one of the on the right always works the same days in month because you just give it the year in the month. So this is our first going to be our first coding experience so you don't have to listen to me we can go to J.S. been slash yo Europe and when you first go there.

Don't worry it's going to be totally blank. But it's alright that's just the way it works. So I'm going to make this the smaller clothes my mail and stuff. So here's a looks. Blank scary but if you hover over the upper right it's at it see editing just been.

Just been has a lot of things we can look at but for the purposes of what we're doing we should just be seeing the Java Script and the console. It's kind of wrapping a little bit. I'll assume a little later after I read the comments. So there's some tests in this file and when you press the run button.

It's actually going to say the tests are passing but that's just because this test is going to ask you to uncomment a line and when you're on comments as you'll see it's going to break. So just the gist of it and I'll let you read it is that we want to try showing how a function which very clearly separates its inputs and acts like reliably on those inputs is really easy to test compared to one that doesn't.

So we have it in the fall right now it's a way to test that day's this month thing. So this code as gnarly as it is I think the point of this exercise is just to look at how we're testing the complicated one. Because you know if you do that.

So it'll be your job to write the test for the nice one which is days in the month at the bottom of the file. So here's the structure the top kind of gives you an overview of what's going on and then there's something you have to do and then there's background code and you can look at it if you like as needed some of it's just boilerplate that's going to make the stuff run.

So give it a try try and read through the file and just shout out any questions. Adam says you know what what you do even if it was the middle of the Third it's all about read about all of that just to better. Through just get it get a feel for it just been playing around.

Yeah that's mostly it we're going to be using this thing again some of the new exercise a bit later exercises agree more. And all of the tests will fail but this one is the slide here a certain equal days a month question question question we take this out and the point is just to fill these values in so that it passes.

I mean ultimately it's just to say the way we would test days a month is as easy to say insert that a certain month a year has a certain value. We don't have to like Mark things the way we did above. If I assert twenty fourteen March thirty one.

As I caught. I'm caught expected thirty point nine five not a large number of people thirty. That's not our lives. So my own flirtations that the extra credit folks that we just picked a different I guess. Which assertion libraries. Just yours are just we're just we're just really really stupid simple ready for a show.

It's just what are your thoughts. My one function going to vary but there we go I want to survive. I'm going. Yeah. So it's all it's all everything that we're running is in this box. Pretty much except for the stuff. Wait maybe maybe this one but another ones who are going to put some libraries require that you'll see a couple little Ok this one's pretty pretty pretty basic.

So there's nothing hidden other than what we see here. There's an H.T.M.L. javascript. Yeah OK later ones will include other libraries using if you open the H.T.M.L. you'll see I'm just pulling in the screen kind of just how is everybody feeling in the room about this one has understand it's been so like a player on the earth.

Because if you do if you see the point where you run it and you're getting some kind of here because you're feeling something at least it's like yeah I kind of feel a lot of feel stuff and OK OK OK Such first kind of stuff aeration is going to be a lot more if you want to walk through the solution.

So yeah it's a good idea T.V. So my solution for the way of thinking of it was in order to test this thing you have to fill in some kind of value. So twenty four. It seems. Let's not use the one million rather than the regulars the three.

If you like one hundred thirty days had September so it's thirty one to be much easier for the patient to take that stuff in a mile an hour with a line calling from A to B. so fast it's just make sure the tests are twenty nine take so to test things that make that behavior.

Predictably arguments you just pass arguments of your choice to test things that have secret inputs. You have to mark out the secret points in some way so they can know what's happening inside function to a greater degree. It's also worth it to mention this one's probably way more likely to work both on the server and the climb other javascript environment on rather than one rooted in specific secret input environment.

Yeah absolutely. So there's a request for you to speak up a little bit from me you know. So there is this will very soon. Yeah as Brian was saying. If you make your inputs more explicit Not only is it easier to test. As the next few slides will show you there are other benefits such as it's more likely to run in other places.

So you can share your code easier between the server and the client to the extent that you can even if you're trying to use like a location in the window or you just got a lock. You just don't have it but for other things. It's better like time.

This would work. You know your way.

Ready to take your code to the next level?

Intense courses with world-class teachers and unlimited access to our growing library of videos for the great price of $39 per month.

Get Unlimited Access Now