Synchronous Software Development
getUserDashboardData function upon invocation. Since the database
read calls are blocking, dependencies are obtained one after the other in a sequential fashion. This is much different compared to asynchronous software development.
Asynchronous Software Development
Again, examine the following code fragment.
Refactor using Async.js auto
Async.js library there is a method called auto which accepts an object literal containing keys and methods. The
auto method will resolve all dependencies before calling the target method and do this for all methods that need to be executed (with the exception of an error condition). The following quote is from theAsync.js documenation:
“Determines the best order for running the functions in
tasks, based on their requirements. Each function can optionally depend on other functions being completed first, and each function is run as soon as its requirements are satisfied.”
And yet again, examine the following code fragment.
With the aforementioned refactor we accomplished quite a bit. Let’s review the return on the effort.
Before the refactor we had a rather obfuscated
getUserDashboardData method. It was hard to follow what method calls depended on each other, and if a method call really depended on another at all. After the refactor we have a
getUserDashboardData method that is mostly declarative. The
auto method is responsible for determining the order in which to call dependant methods.
Before the refactor our dependant methods had varying method signatures. After the refactor we now have dependant methods that take the same type and number of parameters. These methods could be placed in a library and reused for other invocations of
auto without the requirement of modifying said methods.
Before the refactor the
getUserDashboardData method it was becoming unruly, and additions to the method would have made its condition worse. If we want to add more data to the user dashboard it’s simply one more line of code in the
Along the same lines as Code Cleanliness, the declarative style of the
auto method makes it simple to understand. With the addition of the
processResults method, each method has a single responsibility and well abstracted.
All of the methods in the refactor are “pure” methods, which means that they are idempotent and without side effects. Mocking and stubbing depenencies are incredibly simple. We can pass in the mocks or stubs as parameters to these methods and test that they’ve been called correctly.
auto method to declaratively handle dependant method call chains couldn’t be easier and it’s return on value for Clean Code is excellent. If you aren’t using this method perhaps you should give it a try.
Questions, Comments, or Concerns?
Join the conversation on Twitter #asyncjs.