What Javascript taught me about closure


December 4 2015

Understanding closure on an intrinsic level is easy. Humans are motivated to seek it. Loose ends can leave us with an uneasy feeling of ambiguity, misunderstanding and, often, dissatisfaction. The irony of closure is that it’s elusive and fleeting. Although ambiguity can be a fun place to visit once in awhile and it’s challenging to explore new concepts. Satisfaction with a completed project, task or goal is rewarding, and the level of difficulty is strongly associated with the satisfaction we feel. The low of difficulty and the highs of satisfaction have been on my mind a lot since I started learning to code several months ago. To be honest, it’s been mostly difficult, but it’s changed the way I look at the learning process and how I interpret closure.

How I got started

I like to make things. It’s in my blood. It’s why I trained and worked as an industrial designer. Often when I set out to create, the result is rarely how I imagined it in my mind's eye. I’m not sure why, but I usually attribute it to lack of time, understanding or planning. The process makes me better and what I am able to create going forward is stronger. But my satisfaction and need for completion are not met. This is frustrating. I feel that I’m just not skilled enough or smart enough to figure it out. When building software, the material is lines of code. But learning to code is one thing and truly understanding code is another. The biggest difference is the feeling of closure that comes from the latter.

Learning to code

Picking a language to study is difficult. There are many arguments for which language to learn first and why. The best best is pick one and stick with it or decide early it’s not a good choice and switch to something easier. I chose JavaScript because it’s relevant, easier to understand than lower-level languages and it’s interpreted. With no compiler step, all that is required is a browser and a text editor. JS certainly has its quirks that were not immediately obvious. But I’d rather jump right in and learn than spend weeks contemplating which language is best for me. Too much too soon I’d read that Eloquent Javascript by Marjin Haverbeke is a great place to start. What it should have said is Eloquent Javascript is a great place to start for those with a background in computer science. Or for those who learned jQuery before Javascript and are retracing their steps. It assumes a foundation in programming that I didn’t have. Halfway through the third chapter the book has lessons on JavaScript closures. I convinced myself that I understood it and was ready for the problem sets. What followed was days of frustrating moments that mostly involved guess and check. The feeling after finishing the chapter on closures was anything but closure. I felt confused, frustrated and dumb. From there the book goes into recursion and data structures -- arrays and objects. After Googling recursion and then picking up bits of my brain scattered around the room, I continued to plow ahead. By Chapter Five, I wasn’t sure if I should chuck my computer into the river or curl up into a fetal position and sob uncontrollably. I realized I was lacking fundamental understanding and this journey was going require more than just a few chapters in a book.

Taking a step back

What I forgot was that learning requires patience. It’s a process of struggle that is not mastered with one resource. I had to relearn how to learn. I took a step back by using a more basic resource. Codecademy is a brilliant tool that teaches an array of Web-related technologies in simple terms that often border on handholding and spoonfeeding. That’s what I needed. Codecademy has a nice user interface and tracks progress of the courses. Students used to earn badges, but that has been replaced with a progress bar, and that’s a better solution. The course is fast paced, and I completed it within a week. I moved on to Udacity, which isn’t as well designed. But the JS course is free. I learned JSON structure and DOM manipulation, something the basic JS course on Codecademy does not include. The last JS course I took was through Code School. This was my favorite because it was the most challenging, included insightful videos and had the best overall experience. It’s a paid resource, but I highly recommend it for novices. Each of these resources include challenges in order to move on to the next step. Do all of them. If you don’t understand the question or the concept, then Google it. Use the hints and try to figure out what is happening. Don’t be tempted to just Google the answer or use the ‘solve’ button Code School provides. I did not get any closure out of having someone else answer the question for me.

Branching off on my own

Learning JavaScript didn’t quite satisfy my appetite for closure either. It only exacerbated the problem. I still felt as if I was trying to access a local variable outside of a function's scope -- the exact opposite of how I understand closure as it relates to humanity. Finally feeling creative in a medium that I had just begun to scratch the surface of was exciting. I was ready to apply the basic knowledge I had learned to DOM manipulation. A friend of mine, suggested I create a basic image gallery. I felt this was a great place to start. Unfortunately I hadn’t realized that a simple image gallery isn’t actually simple. I was soon inundated with thoughts of confusion, frustration and was starting to get the familiar feeling: I can see the path to completing something but don’t actually know how to do it. For me it’s often accompanied with a hot and cold physical sensation. It’s as if I can see the light at the end of the rabbit hole, but it’s just the beady little eyes of JavaScript mocking me. Sometimes it’s best to take a step back and think about a problem in non-technical terms. In other words, learn to write pseudo code. I also learned to compartmentalize and break a problem down. I listed all of the functions the photo gallery needed to do (ie. load images, expand images, previous, next) then tackled those one by one by replacing my pseudocode with with real JavaScript. I spent about five weeks on the on the gallery page. It pulls down my Instagram feed via their API, displays the images etc. I cooked up enough spaghetti code to feed an entire family, but I did it. Now I’m working on a project using the Pinterest API that sorts and displays my boards on a single page. (Yes I love Pinterest. There I said it.) Closure can be a constant push and pull journey with varying degrees of learning, failure and satisfaction -- or lack thereof. Computer science is far too vast for one person to know everything. I once thought of closure as a final understanding or fulfillment from completion of an objective. Studying code has showed it’s more nuanced than that. Sometimes closure can be as simple as understanding a way to solve future problems or it can be realizing to not beat myself up over a lack of domain knowledge. My feelings of fatalism, inadequacy and frustration have been replaced with understanding. I’m now equipped with a set of tools to manage daunting tasks. And that to me is closure.