This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.

Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Omit Needless Names" Lesson is part of the full, Hardcore Functional Programming in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

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

Transcript from the "Omit Needless Names" Lesson

>> [MUSIC]

>> Joe Nelson: The first bit of the silence is to omit needless names and the way we're going to do that is, like I said, is through the separations and recognitions. So this is maybe a little small, I can zoom in at time, so we can see it but this is an example of the first type of separation.

[00:00:22] You'll be 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.

[00:00:38] It can just go out there 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.

[00:00:54] So it does some day calculations, it just tells you how many days in 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 like Ruby libraries they have a thing called timecops. So if you wanna write a test to make that test reproducible on a function that works with time.

[00:01:10] You have to like fake out the system timer, just kidding, it's a year ago. I run my code, I hope it works. Instead of making it really explicit, like this one on the right always works the same days in the month. Because you just give it the year in the month.

[00:01:27] 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 jsbin/yoyip and when you first go there don't worry it's going to be totally blank. But it's all right that's just the way it works. So I'm going to make this the smaller, like close my mail and stuff.

[00:01:49] So here's how it looks blank scary but if you hover over the upper right, it says Edit, see Edit in JS Bin. Now JS Bin has a lot of things we can look at but for the purposes of what we're doing, we should just be seeing the JavaScript and the Console.

>> Joe Nelson: It's kind of wrapping a little bit, I'll zoom in 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 uncomment it as you'll see, it's going to break.

[00:02:34] So just the gist of it and then 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 file right now is a way to test that days this month thing, so this code as normally as it is.

[00:02:59] I think the point of this exercise is just to look at how we're testing the complicated one, they're like we don't have to that. So it'll be your job to write the test for the nice one, which is days in month at the bottom of the file, so here's the structure.

[00:03:16] The top kind of gives you an overview of what's going on.
>> Joe Nelson: 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.

[00:03:32] Try and read through the file and just shout out any questions.
>> Speaker 2: Adam is asking what you do with the code.
>> Speaker 3: There's a line down the console right in the middle. Yeah, right in the middle it says insert equal and it's pointing it out it's alt.testpass. Just to play around with that thing.

>> Speaker 2: It's really just to get a feel for JS then and play around with it.
>> Joe Nelson: Yeah, that's mostly, we're gonna be using this thing again. Some of the new exercises or the later exercises will be more involved with test or fail. But this one is this line here, assert equal days in month equals, we take this out.

>> Joe Nelson: 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 daysInMonth is as easy as saying assert that a certain month and year is a certain value. We don't have to, like, mock things the way we did above.

>> Speaker 2: So, if I assert 2014 March 31. Says, UNCOD expected 30.95 then a whole bunch of numbers to equal 31, that's not rounding?
>> Joe Nelson: So my own limitations, there will be extra credit to fix that. But we could just pick a different month I guess.
>> Speaker 4: Which assertion library is this?

[00:05:02] Just curious.
>> Speaker 2: Yeah, we're just really, stupid, simple. [CROSSTALK] It's my one function library. [LAUGH]
>> Speaker 3: There we go.
>> Joe Nelson: It's called one search.
>> Speaker 4: I'm good.
>> Joe Nelson: Yeah.
>> Speaker 4: So it's all it's all everything that we're running is in this box.
>> Joe Nelson: Pretty much except for the stuff.

[00:05:20] Wait maybe maybe this one but another ones who are going to put some libraries.
>> Speaker 2: Requires that you'll see a couple little.
>> Joe Nelson: Ok this one's pretty basic.
>> Speaker 4: So there's nothing hidden other than what we see here. There's an HTML, the JavaScript.
>> Joe Nelson: Yeah, later ones will include other libraries using if you open the HTML you'll see I'm just pulling in the screen kind of just how is everybody feeling in the room about this one.

>> Speaker 3: Do you guys understand Js has been enough to like play around with it on is there something we get from anybody?
>> Joe Nelson: I guess if you tell the point were you run it and you're getting some kind of air because you're feeling something at least it's like yeah I kind of feel a lot of feel stuff and [INAUDIBLE] Okay.

>> Joe Nelson: Okay. [INAUDIBLE] Okay, so that's the first kind of separation. There's gonna be a lot more.
>> Speaker 2: Did you want to walk through the solution?
>> Joe Nelson: Yeah. That's a good idea.
>> Joe Nelson: So my solution, for the way I was thinking of it, was in order to test this thing, we have to fill in some kind of value.

[00:06:44] So 2014, Let's not use the one that made it round inappropriately.
>> Speaker 2: [LAUGH] One instead of three.
>> Joe Nelson: If you like one like thirty days had September, so it's 31 to be much easier for the patient to take that stuff.
>> Speaker 2: in a mile an hour with a line calling from A to B.

>> Joe Nelson: So, it's fast it's just make sure the tests are 29 take of. 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 inputs in some way. So you can like know what's happening inside function to a greater degree.

>> Speaker 2: It's also worth it to mention that this one's probably way more likely to work both on the server and the client and other JavaScript environments. Rather than one rooted in specific secret input environment.
>> Joe Nelson: Yeah absolutely.
>> Speaker 2: There's a request for you to speak up a little bit from you know [INAUDIBLE]

>> Joe Nelson: Yeah as Brian was saying. If you make your inputs more explicit not only is it easier to test. But 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.

[00:08:15] You just don't have it but for other things it's better like time, this would work anyway.