with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

Surface level tips for good API writing

Monday, January 2nd, 2006

There are times when venturing into a new project where a developer is faced with the task of needing to write an API to either future proof the project, aid the development process, or develop for an entire group of developers. Believe me. This is no easy task. However this pain-staking process doesn’t need to be as hard as one thinks.

Let’s pretend…

We’re developing an API for a blogging system. So for humor sake, instead of WordPress, we’ll call it “LetterStamp“. We want to give the author/developer some tools to put together their new blog with ease.

Think Dumb

Imagine yourself using ‘the very’ API that you’re developing. Is it developer friendly? Does it give you the control you need? Or does the API need tweeking just to do what you’re trying to do? Consider some functions you wouldn’t mind having. Are they over-complicated? Under-complicated?

Abstraction

Have you ever thought to yourself Hey, it would be nice if I could plug in my own variables here…. Abstraction is key. Decoupling presentation from perhaps logic. Avoid ’set in stone’ variables and conditions. For instance, let’s say we had a function that gave us the last five articles from our article database table. Wouldn’t it better if it was the last n%? We could then allow the developer to plug in their own number.

You shouldn’t have to know how it works

You only need to know what you can do with it. For example, I don’t want to know that my processor communicates with my hard drive which then goes through a buss…or…whatever (I better stop before I really sound like I don’t know what I’m talking about). All I want to do is hit the power button, click an icon, and be surfing the web in seconds.

Applying this theory to our new LetterStamp blogging system - as developers using the API, we don’t need to know that in the background there’s a persistant database connection, a private call to some cleaning functions that prepare our content for ouput, or that query that gathers all our categories together. That stuff should be considered off-limits.

If you’ve found yourself playing around on the API side of things, then that is almost a sure sign that the API is flawed. If this were a tangible item - and you paid for it, I would most certainly take it back for a refund.

So what did we learn?

If anything, it’s something to consider when making an API. When I sit down to write a class or two, I consider the impact and usability. Am I going to be helping the developer? In practicality, you would want to make it as best as possible so you won’t be bothered so much in the future about it.

Work Backwards

I can guarentee it will help. In retrospect, the API is not developed first, but my pretend application. I like to imagine that it’s already built and I’m calling pretend functions that do exactly what I want them to do. When that part is finished, I can then begin working on my API development.

Todays question and book information

Ajax in Action Book

The winner of today’s contest will receive a copy of Ajax in Action by Dave Crane, Eric Pascarello, and Darren James.

Put together an “Ode to my first job” in exactly four lines. (Yours, not mine)

Please remember to put your answers encapsulated within a <blockquote> element, and the regular discussion as normal.

17 Responses to “Surface level tips for good API writing”

  1. Dante

    I’m currently working on an API for one of my Web Apps, and something I’m doing is developing a PHP interface to it. If I find the API too difficult to use, I change it.

    Songwriting, Songwriting!
    What a thing to do all day
    Make up riffs on a piano
    And for that I receive pay!

  2. Elliot Swan

    Some good ideas/tips. I think WordPress does a great job at this, in both usability and abstraction.

  3. Chad Lawson

    Now the nightmares will start again…

    Oh, phone support for early 90s ISP;
    To once again be the whipping bitch for newbies;
    How I long to once again hear that woman say…
    “I want your fu%@ing corpse roasting on a spit!”

    …sadly a true story… my second day on the job.

  4. aliotsy

    Congrats on making the 9Rules network!

  5. Michael Odden

    In a government company
    You’d find me whole-hearted
    They call it The Army
    Here I my coffe-drinking started

  6. Michael Odden

    Hey, (I guess you’re aware of this but here goes anyway =) ) your server-side parsing of comments seperates from the live preview as only the live preview replace newlines with <br />’s
    Have a nice day!

  7. Jim A.

    An Ode (or cadence) to my first job:
    •C5 Galaxy Rolling Down the Strip
    •Communications soldiers on a one way trip
    •We raise those antennas and extend them high
    •We shoot those radio waves into the sky

  8. Blog Contest Site - a social experiment (Best Wishes For New Year) » Win a copy of Ajax in Action

    [...] rane, Eric Pascarello, and Darren James. For details check the entry at the bottom of this post. Look for similar articles under these categories:  Contests Was [...]

  9. Stuart

    Nice thoughts. Although logic maybe says that an API should be built on top of an app, since I heard that Flickr is the biggest user of it’s own API, it made me wonder if creating the app out of the API could be interesting way to go.

    My ode:

    My colleagues were dumb,
    and selling phones really sucked.
    But I got lots of satisfaction
    when I told them to get coffee.

  10. Carl Camera

    Yes, if I were creating a web content management system, I would want to provide default settings when no input is provided and customization when the user requests it. Oh that’s right, I am creating a web content management system. Better keep this list handy! Good post.

    a haiku
    phone stirs me from sleep,
    another database is down.
    i hate tech support.

  11. Carl Camera

    this is probably considered soft spam but sorry — that haiku I submitted has been bugging me all day; it’s awful. I have to resubmit a better one to get it off my mind: (not that I want the book mind you — and besides haikus only have three lines so I’m disqualified from the start) okay new haiku: phone rings. cat awakes / then falls blissf’ly back to sleep. / tech support she’s not.

  12. Craig Buchek

    I was thinking about submitting an ode to my first job, but there’s no way I could write anything better than Jim A. in #7. Especially since it was a job in fast food. Plus, I already own a copy of the book. Love the site!

  13. Jim A.

    @Craig: Aww, thanks!

  14. Justin Perkins

    Working on bikes,

    wrenching on the brakes

    oh how I miss thee.

    Fixing a flat or lubing the chain

    you always entertained me.

    That boss was a dick

    but what do I know

    I was a 14 year old prick.

    I don’t really think I was a prick, but what the heck, it rhymed.

  15. Ronald Yau’s Blog » Blog Archive » Surface level tips for good API writing

    [...] ;

    Surface level tips for good API writing

    Tips for writing API read more

    [...]

  16. And the Winners are…

    [...] g a lovely meal. 11) Surface level tips for Good API Writing Write an Ode to my first job Jim A http://www.auldridges.com Jim wrote a fancy little ode abou [...]

  17. katalog

    Someone else below asked this already.
    I am getting nailed with Spam in my guestbook for our catalog website. Is there anyway to stop this? If not, there really isn’t any point in leaving it up and active. Any help will be greatly appreciated.

    Thanks

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

Submit a Prototype

All content copyright © 2003 - 2007 under the Creative Commons License. Wanna know something? Just ask.

About | Archives | Blog Search

[x] close

Loading...

Submit a prototype

By checking this prototype I agree that I am not submitting false credentials, pornography, or a hate crime website. I also understand that by submitting my entry I may or may not be accepted, and if accepted, my entry may be taken down at any given time if I violate these terms.