Functional-Light JavaScript, v3

Code is Provable

Kyle Simpson

Kyle Simpson

You Don't Know JS
Functional-Light JavaScript, v3

Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Code is Provable" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle explains how and why provability is one of the major benefits of functional programming.

Preview
Close

Transcript from the "Code is Provable" Lesson

[00:00:00]
>> Kyle Simpson: Another interesting perspective on why functional programming. And this is more from the mathematical perspective. Functional programming though it looks very programming esque, it looks like function calls, it's actually math. Now, I don't know the math. And I would guess that at least some of you don't know the math either.

[00:00:24]
But you know what, that's okay. This isn't a math course, I don't need to teach you the math for you to benefit from it and here's why. I'm sure all of you at some time or another, probably recently, have eaten out in a restaurant. And they brought you the bill and you had to do some basic arithmetic to figure what tip to add and how much you're gonna charge on your credit card, okay?

[00:00:49]
Now, you probably learned, most of these at least, probably learned basic arithmetic back in 1st, 2nd, 3rd grade something like that. And you've been using those life skills ever since. And I'm also willing to bet that the vast majority if not all of you, have never actually gone through the formal proof of why 1 plus 1 equals 2.

[00:01:12]
You've never gone to that level of math. Did you know that there's an actual formal proof for it? And did you know that it's amazingly complex to prove that 1 plus 1 equals 2. I can't even begin to explain the math you of that, so I'm not even gonna try.

[00:01:26]
The point is that we're able to take advantage of a mathematical principle even if we can't prove it to ourselves. We can add 1 plus 1 equals 2. And guess what, we never ever question 1 plus 1 equals 2, do we? Is there ever been a time where you were like, well, I added 10 and 9 together and I got 19, but is it really 19?

[00:01:47]
I'm not sure, like, no, you just do it. So when I tell you that functional programming is based on mathematical principles, you can rest on the fact that if you do 1 plus 1 and you get 2, that's the end of the story. The math proves that whether you can go through the formal proof or not, is a secondary issue.

[00:02:10]
Functional programmers, by the way, seem to get a particular joy in life from going through the formal proofs and that's totally okay, but it didn't work for me. I should say, it has not yet worked for me. I'm not at the point of my journey yet, where to learn a thing means that I need to go learn some mathematical laws and apply it, and do some notations and some conversions of the equations.

[00:02:35]
That isn't how I learned this stuff. And yet, I can still take advantage of the fact that if I know that this thing done this way, has this result, it's the same thing as relying up on 1 plus 1 equals 2. If I do it the right way, I know what the end result's gonna be.

[00:02:56]
How many of you have ever written a test in your program before? Anybody done tests, probably a lot of you do TDD, I know that's very popular. TDD, where you write a test and then you run some code and you're hoping that it passes the test, right? Or maybe you do it the other way around, you write some code and then you write a test and the test fails and now you're questioning, is my test bad or is my code bad?

[00:03:16]
Not really sure, for me, it's usually both. But we're trying to get the test to run and we're trying to get it to pass, we want the green. Somewhat universal experience for us. Here's what happens for me and maybe it happens for you. I write some code and then I go to run the test, I'm not really sure whether it's gonna pass or not.

[00:03:42]
I think it will, I hope it will, maybe, let's see. Nope, it didn't pass, failed. So, what do I do? I go back and I wrap some parentheses around something and I try it again. No, didn't work, okay then I add one to it. Nope didn't work and I just keep fumbling my way through because if I really understood it in the first time that I did it, the test would have passed, right?

[00:04:08]
So we are clearly into territory, this gray area where most of us spend most of our career. We're in this gray area where we only partially understand the problem we're trying to solve, not zero. You are a smart people, you understand some of it. You just don't understand enough of it, cuz if you did understand all of it, then it would pass the first time.

[00:04:28]
Unless you fat finger a typo or something. But most of the time our bugs are like logic bugs where we kind of understand the problem domain, we kind of understand the algorithm, but not entirely. So we fumble our way toward it. This is how I've done almost all of my programming for 25 plus years, so I fumble my way towards something where the test passed.

[00:04:48]
And then I'm like thank God, and then I move on to the next thing. And it's this building and building and building.
>> Kyle Simpson: Some of you are nodding your heads like you can relate to that, what would it be like? What would it change in your programming, in your profession, in your day to day work?

[00:05:11]
If almost every time that you went to run a test, you were 100% certain of the outcome?
>> Kyle Simpson: What would that do for you? Would you spend less cycles trying to fumble your way through figuring it out? Maybe, hopefully.
>> Kyle Simpson: I'm not suggesting that you would never run the test, but maybe you'd run them less often because you begin to develop a sense of confidence.

[00:05:40]
I know what I'm doing, I'm not an imposter anymore. When I do it, it does the right thing. Maybe that you would make you a more effective programmer.
>> Kyle Simpson: One of the reasons why functional programming is compelling, at least to me, is it offers the promise that we can use principles of programming.

[00:06:00]
Not that somebody just wrote about in a blog post or gave a fancy conference talk about, but it's based on real hard math. And even if I don't understand the math, I can trust the mathematicians.
>> Kyle Simpson: I'd like to have more of my code like that. I don't know about you, but I'd like to have more of my code in that category of, I guarantee you it's gonna do the right thing every time.

[00:06:28]
If I use it correctly, if I legitimately do one and one and I get two, I don't have to question that again, that's done. And that would free up our attention, I think, to focus on the more important parts. Those higher level important things, those logic and business domain questions that we should be spending our time on.

[00:06:48]
It'd free us up for that and I think that'd be a good thing if we could free up our attention and shift our attention to those things.
>> Kyle Simpson: So that's one of the compelling reasons why functional programming. We know that these techniques are not just a nicely designed API, as a matter of fact some of them are kinda weirdly designed.

[00:07:06]
They're not just about a nice design of an API or a clever name, they're about using mathematical principles. In a mathematically sound way that just happens to look like programming, and it happens to let us solve programming problems.
>> Kyle Simpson: Ultimately, what I'm getting at here, I think this is the ultimate reason why.

[00:07:28]
So you have some people say, there's a lot of different things that people will say about what is the best code. I would say that the best code is the code that didn't have to be read. And that might sound a little strange to you, but let me explain it this way.

[00:07:45]
There's a code base that has 10,000 lines of code. What if 9900 lines of that code was based upon proven mathematical principles and I didn't ever have to second guess any of that stuff. And there were just 100 lines of business logic code that I spent all day every day thinking about.

[00:08:04]
Would that code base be more effective for me to manage? Would it be easier for me to find and fix bugs, if I reduce the surface area of what I needed to focus on? I think that's one of the reasons why functional programming is compelling. I hope these ideas are gonna help you when you have that inevitable conversation with the other people that you code with.

[00:08:31]
People you work with on open-source projects or people that you work with at your job, people you work with in your class if you are in school. You're gonna have to ask them to come on the journey as well. You gonna have to convince them to come on the journey as well and those are some reasons why functional program.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now