Designing Better JavaScript APIs

Get yourself up to speed on designing APIs

At some point or another, you will find yourself writing JavaScript code that exceeds the couple of lines from a jQuery plugin. This article covers the most important things that you will need to consider before and while writing your own utilities and libraries. Rodney Rehm will focus on how to make your code accessible to other developers. A couple of topics will be touching upon jQuery for demonstration, yet this article is neither about jQuery nor about writing plugins for it.

 

Table of Contents

  • Fluent Interface
  • Consistency
  • Handling Arguments
  • Extensibility
  • Hooks
  • Generating Accessors
  • The Reference Horror
  • The Continuation Problem
  • Handling Errors
  • Going Asynchronous
  • Debugging Fluent Interfaces
  • Documenting APIs

Fluent Interface

The Fluent Interface is often referred to as Method Chaining (although that’s only half the truth). To beginners it looks like the jQuery style. While Rodney Rehm believes the API style was a key ingredient in jQuery’s success, it wasn’t invented by them. Aside from major simplifications, jQuery offered to even out severe browser differences. It has always been the Fluent Interface that he has loved most about this extremely successful library. He has come to enjoy this particular API style so much that it became immediately apparent that he wanted this style for URI.js, as well. While tuning up the URI.js API, Rodney Rehm constantly looked through the jQuery source to find the little tricks that would make my implementation as simple as possible.

Comments

Be the first to write a comment

You must me logged in to write a comment.