Book Review:Build Your Own Angularjs: O C T O B E R 3 1, 2 0 1 6
Book Review:Build Your Own Angularjs: O C T O B E R 3 1, 2 0 1 6
While I didn’t take notes while reading, some ideas stood out. Thus, this
post describes some of the concepts I have picked up from the book.
THE GOOD
Angular has support for transclusion which allows directives to do whatever they
want with some piece of DOM structure. A tricky concept but very powerful since it
allows you to clone and manage the scope in transcluded content.
There is also support for element transclusion. Unlike the regular transclude which
will include some DOM structure in some new location; element transclusion
provides control over the element itself.
So why is this important? Imagine you can add this to some element to only show
up under certain conditions? Then you can use element transclusion to ensure that
the DOM structure is only created and linked when you need it. Need some DOM
content to be repeated x times? Just use element transclusion, clone and append it
the x times. These two examples are over-simplifications of ng-if and ng-
repeat respectively.
Such great fundamentals allow engineers to build complex things from simple
pieces – the whole is greater than the sum of parts.
This was my first project built from the scratch using TDD and it was a pleasant
experience.
The array of about 863 tests helped identify critical regressions very early. It gave
me the freedom to rewrite sections whenever I disagreed with the style. And since
the tests were always running (and very fast too, thanks Karma!); the feedback was
immediate. Broken tests meant my ‘refactoring’ was actually a bug injection. I don’t
even want to imagine what would have happened if those tests didn’t exist.
This is a big lesson for me and something I’d like to replicate in more of my projects:
software should be easy to configure and extend.
The Angular team put a lot of thought into making the framework easy to configure
and extend. There are reasonable defaults for people who just want to use it out of
the box but as expected, there would be people who want a bit more power and
they can get desires met too.
4. Simplified tooling
I have used grunt and gulp extensively in the past however the book used npm in
conjunction with browserify. The delivery pipeline was ultimately simpler and easier
to manage.
If tools are complex, then when things go wrong (bound to happen on any
reasonably large project), you’d have to spend a lot of time debugging or trying to
figure out what went wrong.
Recursion
The compile file which would allow two functions to pass references to each other
– an elegant way to handle state handovers while also allowing for recursive loops.
1. As reference values: The other insightful trick was using function objects to
ensure reference value integrity. Create a function to use as the reference.
2. As dictionaries: functions are objects after all and while it is unusual to use them
as objects, there is nothing saying you can’t.
Angular
Most of the component hooks will work for directives as well – in reality,
components are just a special class of directives. So you can use the $onInit,
$onDestroy and so on hooks. And that might even lead to better performance.
Issues
Tero did an awesome job writing the book – it is over a 1000 pages long! He really is
a pro and knows Angular deeply; by the way, you should check out his blog for
awesome deep dives.
My only issues had to do with issue resolution; there were a few issues with
outdated dependencies but nothing too difficult. If he writes an Angular 2 book, I’d
like to take a peek too.
CONCLUSION
I took a peek at the official AngularJS repository and was quite surprised by how
familiar the structure was and how it was easy to follow along based on the
concepts explained in the book.
I’ll rate the book about 3.9 / 5.0. A good read if you have the time, patience and
curiosity to dive deep into the Angular 1 framework. Alas Angular has moved on to
2 but Angular 1 is still around. Moreover, learning how software is built is a great
exercise always.