Code

Fundamental Javascript Principles Explained with Popular Films

Fundamental Javascript Principles Explained with Popular FilmsPreview: Fundamental Javascript Principles Explained with Popular Films

We asked Northcoders student Rose to talk about what she loves about coding...

Learning any new skill is going to come with its fair share of new information, and technical terms that you need to wrap your head around. Learning to code is no exception. There are a lot of seemingly complex principles that you’re going to have to master, and it can be easy to become overwhelmed and confused at what they all mean.

While trying to navigate this tricky road we call code, I’ve turned to metaphor to try and translate things into terms that are more familiar and comfortable to me – and what could be more familiar and comfortable than some of your favourite films from years gone by? You know, the kind you’ve watched time and time again on ITV2 on a Sunday afternoon? And with that, I give you: Fundamental Javascript Principles Explained with Popular Films!

Closure

Closure in coding is focused around retaining information after it would normally have been wiped from memory. By placing a function within another function, and a variable neatly sandwiched within the two, the inner function can use the information stored within the outer function, even once the local execution has been garbage collected. We can't communicate with the outer function anymore - it's gone. But we can access some of the information it knew about. These functions are defined as having a “parent-child” relationship (you’re going to want to remember that bit for the next paragraph when we get all figurative).

Incidentally, have you seen the 1998 Michael Keaton film Jack Frost? If not, don’t worry, it’s a pretty classic motif - Semi-pro rockstar dies and comes back as a wise-cracking, strangely attractive snowman. Obviously, that all sounds fantastic and you’re now trying to slowly back away from this blog without me noticing so you can go and watch it. I wouldn’t blame you, but stay with me.

Fundamental Javascript Principles Explained with Popular FilmsPreview: Fundamental Javascript Principles Explained with Popular Films

You see, Jack Frost is really just ONE BIG ANALOGY FOR CLOSURE. Jack is gone, just like that parent function that ought to have been cleared away. But he’s left behind a son - a son who is having trouble with bullies and is not dedicating anywhere near enough time to ice hockey practice as he ought to be. He could really use a bit of fatherly advice, but his dad’s gone, right? WRONG. Here’s a snowman possessed by the ghost of Michael Keaton to show him the way.

Recursion

Recursion refers to a function that calls itself, until it meets a certain requirement, or “base case”, that means it doesn’t need to do so anymore. You can use it for things like totalling a nested array full of numbers. We look through the top-level array, add those numbers together, lovely, but what if one of the items in that top-level array is an array itself? And what if that array contains another array? Well, we start the whole thing again each time we hit a new array. Once we’ve met our base case of having gone through every item in the array, we can finally put this whole matter to bed and return our total.

Bending your mind a little bit? Don’t worry, Bill Murray is going to help me explain.

Groundhog Day is a belter of a classic, on that we can all agree. If you haven’t seen it, cool your jets with all this learning to code business and go and watch it post-haste. Your priorities are all out of whack.

Fundamental Javascript Principles Explained with Popular FilmsPreview: Fundamental Javascript Principles Explained with Popular Films

Bill Murray plays churlish local news reporter, Phil. Phil has a pretty nice life. He’s a local celebrity, he makes good money, and he looks just like Bill Murray. This isn’t enough for Phil, though. He doesn’t appreciate what he has - he’s irritated by his coworkers, uses people to meet his own selfish ends, and is frustrated by having to cover light-weight stories - having to go every year to report on a groundhog who can predict the weather.

Somehow, Phil manages to get himself trapped in a recursive loop, living the same day over and over again, until he meets the requirements to break the cycle (in this case, becoming a decent person). Phil initially tries to beat the system, to gear this situation to benefit himself by trying to trick his beautiful producer into falling in love with him, and commit petty crimes in the area of Punxsutawney. Eventually, though, by learning to be honest and compassionate to his fellow man Phil manages to break out of this loop and move forward to execute the rest of his code/ live a beautiful life. Base case, in your face.

Async

Asynchronous programming involves gearing your code to run in perfect harmony, and meet your end goal in the most smooth and efficient way possible. Maybe part of your code involves sending a request for information to a server somewhere out there on the internet. What are you going to do, sit around and wait for it to come back like some kind of schmuck? No, no, no, you have a thread to be executing. Instead, how about running several operations all at once, ultimately returning one final result once that info comes back, with the help of a nifty little callback function?

In Ocean’s Eleven, does George Clooney wait for Matt Damon etc. to create a diversion, then once they’re finished, get cracking on stealing the money? No. That would be stupid. Why would you wait for the diversion to be over before doing the thing you’re diverting attention from?

Fundamental Javascript Principles Explained with Popular FilmsPreview: Fundamental Javascript Principles Explained with Popular Films

I guess in this situation, George is the asynchronous function. He’s sent his request. His contortionist/acrobat mate has been smuggled into the vault to steal many millions of dollars, but while we’re waiting for that money to be stolen, George is also orchestrating a bunch of other stuff - smooth and efficient. Once that money has been successfully stolen, GC needs everyone else in that casino to be distracted. Matt Damon is pretending to be from the Nevada Gaming Commission, and Don Cheadle’s creating a city-wide blackout to cause chaos in the casino and turn the security lasers off. Brad Pitt’s pretending to be a doctor for some reason, I don’t remember why, but he’s doing it brilliantly and he looks GREAT.

Then, nice and tidy, once they’ve got the money out, everything pays off at once. All the lads across the casino achieve their individual little objectives, to contribute to one big output.

 

Want to learn all these programming techniques and more? Learn to code with Northcoders' Developer Pathway 12 week bootcamp.