Profil for PeteLightSleeperBloggLister Verktøy Hjelp

Blogg


    26. juni

    API Principles

     
    Recently I've been thinking about APIs. Pause for a moment to consider the best API family you've ever used. Dreamy, wasn't it? Everything worked the way you expected, you barely needed to look at the API reference, and the resulting code virtually documented itself.
     
    Now think about the worst. I recall when I first started doing Windows programming and came across LPARAMs and WPARAMs in Windows messages. The whole concept of passing nebulous parameters that might be pointers or might be values was quite disconcerting. Or how about this fun little tidbit in the documentation for RegQueryValueEx: "If the data has the REG_SZ type, the string may not have been stored with the proper terminating null characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it." I wonder how many applications have been bitten by this API issue?
     
    Creating great APIs is hard. But wow, is it important for those of us who ship code libraries to developers. When we don't get APIs right, there's nothing that can truly compensate for the failure -- not great documentation, not great sample code, not great user education.
     
    Here are some principles my teams have been using as we think about APIs:
     
    • APIs must be intuitive and discoverable. AddToolbarButton is more intuitive than ExecCommand. A developer reading the function prototype should have a great understanding of the function without reading docs.
    • APIs must be simple. They should do one thing perfectly and do no more. APIs that take the swiss army knife approach are not simple. APIs with a long list of parameters are not simple.
    • APIs must be difficult to misuse. They should use strong typing that help developers find errors at compile time rather than runtime.
    • APIs must not have unexpected side effects. Functions that set globals are evil.
    • APIs must be consistent. Developers come to rely on certain idioms and conventions, including naming, parameter usage, parameter order and so forth. It's better to match well-known idioms than invent new ones.
    • APIs must be reviewed before coding. Refactoring APIs is costly. Getting them right at the very beginning is worth the upfront cost.
    • APIs must be reviewed by the right people. The right people typically include subject matter experts, people in different disciplines, and potential customers. For instance, an audio API should be reviewed by audio experts. Disciplines might include developers, testers and documentation writers.
    If you're developing your own API set, these guidelines can help ensure that your customers have that dreamy experience developers get when using a great API. You'll also spend less time having to update documentation, write complex sample code, or staffing a large support team.
     

    Kommentarer

    Vent litt...
    Kommentaren du har skrevet inn, er for lang. Du må forkorte den.
    Du har ikke skrevet inn noe. Prøv på nytt.
    Vi kan dessverre ikke legge til kommentaren nå. Prøv på nytt senere.
    Du må ha tillatelse fra den som kontrollerer kontoen, for å kunne legge til en kommentar. Be om tillatelse
    Den som kontrollerer kontoen, har deaktivert kommentarer.
    Vi kan dessverre ikke slette kommentaren nå. Prøv på nytt senere.
    Du har nådd grensen for antall kommentarer som kan legges inn per dag. Prøv på nytt om 24 timer.
    Kommentarfunksjonen i kontoen din er deaktivert fordi systemene våre har registrert at du kanskje sender ut søppelpost til andre brukere. Hvis du mener at deaktiveringen er gjort ved en feiltakelse, kontakter du kundestøtten for Windows Live.
    Gjennomfør sikkerhetskontrollen nedenfor for å gjøre ferdig kommentaren.
    Tegnene du skriver inn i sikkerhetskontrollen, må samsvare med tegnene på bildet eller i lydklippet.

    Hvis du vil legge til en kommentar, må du logge på med Windows Live ID (hvis du bruker Hotmail, Messenger eller Xbox LIVE, har du en Windows Live ID). Logg på


    Har du ikke en Windows Live ID? Registrer deg

    Tilbakesporinger

    URL-adressen for trackback av denne oppføringen er:
    http://pkisensee.spaces.live.com/blog/cns!3C84486A9D832EB7!410.trak
    Weblogger som refererer til denne oppføringen
    • Ingen