Writing a working solution to a problem using code is one thing, but being able to read someone else's code is a different ballgame. When we write a program, the code that we created -and hopefully commented- is a reflection of our thinking process while trying to solve an specific task.
The thing is, for every person that tackles a problem you can find the same amount of solutions... totally different inputs with similar outputs. Since most of time we are reading and working on top of the code written by another person, it's imperative that we get used to the uncomfortable feeling of taking a deep dive in the thinking process of a fellow coder (or a group of people for that matter), and do our best to understand how they approached the problem and why the took the decisions they did.
In that regard, is that I’m devoting time everyday to read open source code, to force myself into an unfamiliar subject. Currently I’m working with Accounting JS (repo here), partly because it was recommended per a curriculum as a not overly simple but not overly complex source code with no dependencies (so you focus on core logic) that also has an abordable size (around 400 lines).
My approach to understand this code base has been first to get a generic look, then play a bit with the app and what is supposed to do (thankfully they have this nice demo), I read the tests both as means to get ideas on how to write better tests, and also as good source of info regarding what should the methods do and what are the expected outputs. Finally, I used Chrome's debugger to track how a number is formatted into ‘money notation’, following closely which methods call what and how the process is run.
There is still a lot to learn from this repo, but beyond what each method does (that would be of very little interest I assume to a reader) I will point out some patterns I’ve noticed as well as practices.
function propertyChecker(object) {
let obj = object || {}
return obj.hasOwnProperty(whatever)
}
propertyChecker
isn't passed an object, it would be undefined
. Since undefined
is a primitive, it doesn’t have .hasOwnProperty()
as a method, therefore our code crushes. The ||
enables us to set obj
as either object
(if it passed in the best case) or if that returns falsy it defaults as an empty object {}
.
This pattern is used a lot throughout the code.