Transcript from the "Scope" Lesson
[00:00:19] And as you see here, the goal is to lead us towards a pattern of code that improves our code organization. So we need to understand lexical scope so that we can understand closure, and we need to understand closure so that we can understand the module pattern. So that is our goal for this unit of discussion.
[00:00:37] Let's dive in. All right, scope is where we look for things. That's your first what-you-need-to-know definition on this topic. It's where to look for things, but that definition should ask a couple of questions in your mind. Number one, what is it that we are looking for? So very simply, we're looking for identifiers.
[00:00:57] Here you see an x that's being assigned to, or a y that's being a value retrieved from. Whether you realize this or not, all variables are in one of those two roles in your program. All variables are either receiving the assignment of some value, or you are retrieving a value from the variable.
[00:01:56] And this is a red marble, so it goes into the red bucket. So it's fundamentally a game of sorting colored marbles. This was the best picture I could find on Google. I could either find colored buckets or I could find colored marbles, I couldn't find them together. So here you go, colored buckets, okay?
[00:03:54] There was some processing step, so what does that processing step look like? Well, I'm an old compiler theory nerd for my computer science day. I am one of those weird folks that writes compilers for fun, I enjoy doing that sort of stuff. And so in compiler theory, there are essentially four stages to a compiler.
[00:04:16] Sometimes the first two are combined into one stage, sometimes they're separate, my textbook had them separate. So there's lexing and tokenization. There's parsing, which turns the stream of tokens into what's called an abstract syntax tree. And then the last step is what's called code generation, taking an abstract syntax tree and producing some kind of other executable form of that program.
[00:05:08] So in other words, a lot of people think about this difference between interpreted and compiled as the distribution model for the binary. But that's not really the right axis to be thinking about. The right axis is, is the code processed before it is executed or not? We do have languages that exist which generally don't get processing before execution.
[00:05:30] For example, Bash script. In a Bash script, if you write a malformed command on line 4, lines 1, 2, and 3 are gonna run. And when line 4 fails that might screw stuff up, because you wanna undo [LAUGH] lines 1, 2, and 3. This is a perpetual problem in something like a true scripting language.
[00:06:10] If you think about this marble bucket sorting stuff is happening at runtime, it turns out that not only is that an inaccurate mental model, but it turns out it can create bugs within your program because you assume something to be true and something is different. That divergence, remember the law?
[00:06:28] This is a divergence where you think something and the computer thinks something else. And guess who wins? The computer, okay? So what we wanna do is try to align our brains more like the computer, we want to think like a compiler. The good news is that even though we're gonna talk a little bit about compiler theory, there's a whole bunch of it that we're gonna completely gloss over.
[00:06:48] Bunch of complexity that is not important for this discussion. But the one thing that is important is our marble sorting. Marble sorting is the thing. It is essentially that metaphorically is processing of our scopes and putting all of these identifiers, that is the colored marbles, into their correct buckets.
[00:07:07] So the way that processing first happens before we execute, is that there is a stage where it goes through all of that compilation, It goes through all of that parsing, and it produces this abstract syntax tree. But it also produces, essentially, a plan for the lexical environment. That is, where all the lexical scopes are, and what marbles are gonna be in them, what identifiers.
[00:08:41] So we wanna make sure we dig in and get this. What happens when we process our code, which I would call compilation, if you feel better calling it parsing, doesn't matter. But we are gonna process the code first, and set up the scopes, and then we are gonna execute it, okay?