Improve Your JavaScript Knowledge By Reading Source Code — Smashing Magazine
Skip to main content Start reading the article Jump to list of all articles Jump to all topics 11 min read Coding , JavaScript , Workflow Share on Twitter , LinkedIn About The Author Carl Mungazi is a frontend developer at Limejump, an energy startup based in London. He spends his time digging deep into the depths of all things JavaScript … More about Carl ↬ Email Newsletter Weekly tips on front-end & UX . Trusted by 182,000+ folks.
Designing Websites That Convert with Paul Boag Building Modern HTML Emails with Rémi Parmentier Celebrating 10 million developers Smart Interface Design Patterns, 45 lessons + UX training Smart Interface Design Patterns, 45 lessons + UX training When you are still early on in your programming career, digging into the source code of open source libraries and frameworks can be a daunting endeavor. Reading source code is difficult at first but it becomes easier with time.
The goal is not to understand everything but to come away with a different perspective and new knowledge.In this article, Carl Mungazi shares how he got over his fear and began using source code to improve his knowledge and skills. He also uses Redux to demonstrate how he approaches breaking down a library. Do you remember the first time you dug deep into the source code of a library or framework you use frequently? For me, that moment came during my first job as a frontend developer three years ago.
We had just finished rewriting an internal legacy framework we used to create e-learning courses. At the beginning of the rewrite, we had spent time investigating a number of different solutions including Mithril, Inferno, Angular, React, Aurelia, Vue, and Polymer. As I was very much a beginner (I had just switched from journalism to web development), I remember feeling intimidated by the complexity of each framework and not understanding how each one worked.
My understanding grew when I began investigating our chosen framework, Mithril, in greater depth. Since then, my knowledge of JavaScript — and programming in general — has been greatly helped by the hours I have spent digging deep into the guts of the libraries I use daily either at work or in my own projects. In this post, I will share some of the ways you can take your favorite library or framework and use it as an educational tool.
My first introduction to reading code was via Mithril’s hyperscript function. ( Large preview ) The Benefits Of Reading Source Code One of the major benefits of reading source code is the number of things you can learn. When I first looked into Mithril’s codebase, I had a vague idea of what the virtual DOM was. When I finished, I came away with the knowledge that the virtual DOM is a technique which involves creating a tree of objects that describe what your user interface should look like.
That tree is then turned into DOM elements using DOM APIs such as document.createElement . Updates are performed by creating a new tree describing the future state of the user interface and then comparing it with objects from the old tree. I had read about all of this in various articles and tutorials, and whilst it was helpful, being able to observe it at work in the context of an application we had shipped was very illuminating for me.
It also taught me which questions to ask when comparing different frameworks. Instead of looking at GitHub stars, for example, I now knew to ask questions such as, “How does the way each framework performs updates affect performance and the user experience?” Another benefit is an increase in your appreciation and understanding of good application architecture. Whilst most open-source projects generally follow the same structure with their repositories, each of them contains differences.
Mithril’s structure is pretty flat and if you are familiar with its API, you can make educated guesses about the code in folders such as render , router and request . On the other hand, React’s structure reflects its new architecture. The maintainers have separated the module responsible for UI updates ( react-reconciler ) from the module responsible for rendering DOM elements ( react-dom ). One of the benefits of this is that it is now easier for developers to write their own custom renderers by hooking into the react-reconciler package.
Parcel, a module bundler I have been studying recently, also has a packages folder like React. The key module is named parcel-bundler and it contains the code responsible for creating bundles, spinning up the hot module server and the command-line tool. It will not be long before the source code you are reading leads you to the JavaScript specification. ( Large preview ) Yet another benefit — which came as a welcome surprise to me — is you become more comfortable reading the official JavaScript specification which defines how the language works.
The first time I read the spec was when I was investigating the difference between throw Error and throw new Error (spoiler alert — there is none ). I looked into this because I noticed that Mithril used throw Error in the implementation of its m function and I wondered if there was a benefit to using it over throw new Error . Since then, I have also learnt that the logical operators && and || do not necessarily return booleans , found the rules which govern how the == equality operator coerces values and the reason Object.prototype.toString.call({}) returns '[object Object]' .
Techniques For Reading Source Code There are many ways of approaching source code. I have found the easiest way to start is by selecting a method from your chosen library and documenting what happens when you call it. Do not document every single step but try to identify its overall flow and structure. I did this recently with ReactDOM.render and consequently learned a lot about React Fiber and some of the reasons behind its implementation.
Thankfully, as React is a popular framework, I came across a lot of articles written by other developers on the same issue and this sped up the process. This deep dive also introduced me to the concepts of co-operative scheduling , the window.requestIdleCallback method and a real world example of linked lists (React handles updates by putting them in a queue which is a linked list of prioritised updates). When doing this, it is advisable to create a very basic application using the library.
This makes it easier when debugging because you do not have to deal with the stack traces caused by other libraries. If I am not doing an in-depth review, I will open up the /node_modules folder in a project I am working on or I will go to the GitHub repository. This usually happens when I come across a bug or interesting feature. When reading code on GitHub, make sure you are reading from the latest version. You can view the code from commits with the latest version tag by clicking the button used to change branches and select “tags”.
Libraries and frameworks are forever undergoing changes so you do not want to learn about something which may be dropped in the next version. Another less involved way of reading source code is what I like to call the ‘cursory glance’ method. Early on when I started reading code, I installed express.js , opened its /node_modules folder and went through its dependencies. If the README did not provide me with a satisfactory explanation, I read the source.
Doing this led me to these interesting findings: Express depends on two modules which both merge objects but do so in very different ways. merge-descriptors only adds properties directly found directly on the source object and it also merges non-enumerable properties whilst utils-merge only iterates over an object’s enumerable properties as well as those found in its prototype chain. merge-descriptors uses Object.getOwnPropertyNames() and Object.getOwnPropertyDescriptor() whilst utils-merge uses for..in ; The setprototypeof module provides a cross platform way of setting the prototyp
The rest of this page is on the live site.