Designing Good API  and  its importance Imran M Yousuf Entrepreneur Smart IT Engineering Ltd.
Outline What is an API?
Why is API important?
Designing API
General Principles
OOP Design Principles Smart IT Engineering Ltd.
What is an API? API  stands for –  Application Programming Interface
API is a source code-based  specification   intended to be used as an interface by  software components to communicate  with each other.
ABI (Application Binary Interface) differs to API in that the former is specified on binary interface
In this presentation we will consider ABI as API too Smart IT Engineering Ltd.
Why is API important? What do we Software Engineers develop? How do others interact with our software?
Do we integrate Facebook API (FB Login, Like, Share, etc.)? How?
How do we develop softwares that require system level integration?
How do Web 2.0 portals communicate with its server?
How do Smart Phone / Tablet Apps communicate with server? Smart IT Engineering Ltd.
Why is API important? We, software engineers, develop softwares which users use through UI, write plugins for, use as a library and/or use by executing and working with the output of the software.
A lot of (Almost all) websites integrate with FB using its JavaScript API
For Windows we use MFC or Windows API; for *nix system we use POSIX etc.
Web 2.0 / Smart Phone / Tablet Apps make server side calls via HTTP protocol either from a App or from browser using AJAX. Smart IT Engineering Ltd.
Why is API important? All of this signifies -  “Software components communicate with each other” So either we are  developing and using API  everyday. That is not enough for getting our attention? Smart IT Engineering Ltd.
Why is API important? Imagine the following FB JavaScript API is so difficult to use that you are not able to use them. What would you do?
You are developing an Android or iPhone App and having problem using their API as you not understanding how to simply achieve them. What would you do?
Your client asked you to use a Web Service to integrate one service to another and the Web Service makes no sense what so over and furthermore, it does not have ample documentation. What would you do?
What would be your opinion of such API providers? Smart IT Engineering Ltd.
Why is API important? Contact FB and ask them how to use it
Contact Andoid community mailing lists/forums and ask there
Contact iPhone Developer Community and ask there
Ask my customer to get me documentation or ask the company/developer for help
We think those companies and their developers are crap and we are better than them Smart IT Engineering Ltd.
Why is API important? API can cause a Company and its Developer both Glory and Gloom depending on its user experience
API can be among a company's greatest assets Customers invest heavily: buy, write, learn
Cost to stop/change API can be prohibitive
Successfully Public APIs capture customers Bad API causes unending support request and becoming a liability to the company Smart IT Engineering Ltd.
Why is API important? If you code, you are an API designer Good code is modular – each module has an API Useful modules tend to get reused Once module has users, can't change API at will

Designing Good API & Its Importance

  • 1.
    Designing Good API and its importance Imran M Yousuf Entrepreneur Smart IT Engineering Ltd.
  • 2.
  • 3.
    Why is APIimportant?
  • 4.
  • 5.
  • 6.
    OOP Design PrinciplesSmart IT Engineering Ltd.
  • 7.
    What is anAPI? API stands for – Application Programming Interface
  • 8.
    API is asource code-based specification intended to be used as an interface by software components to communicate with each other.
  • 9.
    ABI (Application BinaryInterface) differs to API in that the former is specified on binary interface
  • 10.
    In this presentationwe will consider ABI as API too Smart IT Engineering Ltd.
  • 11.
    Why is APIimportant? What do we Software Engineers develop? How do others interact with our software?
  • 12.
    Do we integrateFacebook API (FB Login, Like, Share, etc.)? How?
  • 13.
    How do wedevelop softwares that require system level integration?
  • 14.
    How do Web2.0 portals communicate with its server?
  • 15.
    How do SmartPhone / Tablet Apps communicate with server? Smart IT Engineering Ltd.
  • 16.
    Why is APIimportant? We, software engineers, develop softwares which users use through UI, write plugins for, use as a library and/or use by executing and working with the output of the software.
  • 17.
    A lot of(Almost all) websites integrate with FB using its JavaScript API
  • 18.
    For Windows weuse MFC or Windows API; for *nix system we use POSIX etc.
  • 19.
    Web 2.0 /Smart Phone / Tablet Apps make server side calls via HTTP protocol either from a App or from browser using AJAX. Smart IT Engineering Ltd.
  • 20.
    Why is APIimportant? All of this signifies - “Software components communicate with each other” So either we are developing and using API everyday. That is not enough for getting our attention? Smart IT Engineering Ltd.
  • 21.
    Why is APIimportant? Imagine the following FB JavaScript API is so difficult to use that you are not able to use them. What would you do?
  • 22.
    You are developingan Android or iPhone App and having problem using their API as you not understanding how to simply achieve them. What would you do?
  • 23.
    Your client askedyou to use a Web Service to integrate one service to another and the Web Service makes no sense what so over and furthermore, it does not have ample documentation. What would you do?
  • 24.
    What would beyour opinion of such API providers? Smart IT Engineering Ltd.
  • 25.
    Why is APIimportant? Contact FB and ask them how to use it
  • 26.
    Contact Andoid communitymailing lists/forums and ask there
  • 27.
    Contact iPhone DeveloperCommunity and ask there
  • 28.
    Ask my customerto get me documentation or ask the company/developer for help
  • 29.
    We think thosecompanies and their developers are crap and we are better than them Smart IT Engineering Ltd.
  • 30.
    Why is APIimportant? API can cause a Company and its Developer both Glory and Gloom depending on its user experience
  • 31.
    API can beamong a company's greatest assets Customers invest heavily: buy, write, learn
  • 32.
    Cost to stop/changeAPI can be prohibitive
  • 33.
    Successfully Public APIscapture customers Bad API causes unending support request and becoming a liability to the company Smart IT Engineering Ltd.
  • 34.
    Why is APIimportant? If you code, you are an API designer Good code is modular – each module has an API Useful modules tend to get reused Once module has users, can't change API at will
  • 35.
    Good reusable modulesare corporate assets Thinking in terms of APIs improves code quality Smart IT Engineering Ltd.
  • 36.
    Characteristics of aGood API Easy to learn
  • 37.
    Easy to use,even without documentation
  • 38.
  • 39.
    Easy to readand maintain code that uses it
  • 40.
    Sufficiently powerful tosatisfy requirements
  • 41.
  • 42.
    Appropriate to audienceSmart IT Engineering Ltd.
  • 43.
    Designing API SmartIT Engineering Ltd.
  • 44.
    Gather Requirements Weusually get a project to work in the form of a solution We should keep in mind better solutions may exist We must extract true requirements in form of use-cases .
  • 45.
    Can be easierand more rewarding to build something more general Beware of the cost factor and target audience Smart IT Engineering Ltd.
  • 46.
    Start with shortspec Start of all API should begin with writing a spec, preferably within 1 page
  • 47.
    At the verybeginning agility trumps completeness
  • 48.
    Bounce spec offas many “head” as possible Listen to their input seriously Once confidence is there that we understand what it is grow the spec This growing could involve coding, e.g., prototypes Smart IT Engineering Ltd.
  • 49.
    Write it early& often API comes before the implementation API is the concept and should elaborate on behavior
  • 50.
    Thus saving implementingsomething that is not useful API code helps you show the spec itself Saves time in writing documents that is useless Continue writing to API Smart IT Engineering Ltd.
  • 51.
    Write an SPIService Providers Interface (SPI) Plugin API enabling multiple implementations
  • 52.
    It serves asan API used by API implementations
  • 53.
    For example, DataAccess Layer implementations Write multiple SPI implementations before release If its a target that we want to support multiple implementations then we should implement at least a couple of them.
  • 54.
    If you writeone, it probably won't support another
  • 55.
    If you writetwo, it will support more with difficulty
  • 56.
    If you writethree, it will work fine Smart IT Engineering Ltd.
  • 57.
    Maintain Realistic ExpectationMost API designs are over constrained You won't be able to please everyone
  • 58.
    Aim to displeaseeveryone equally
  • 59.
    Stick to whatyou want to achieve Expect to make mistakes Real world use will continuously flush out the mistakes
  • 60.
    Expect to evolveAPI Smart IT Engineering Ltd.
  • 61.
    General Principles SmartIT Engineering Ltd.
  • 62.
    Should Do OneThing & Do It Well Functionality should be easy to explain If it's hard to name, it indicates rethink
  • 63.
    Good names enablesAPI to thrive
  • 64.
    Be open tosplitting and merging modules Functionality should be well specified Specify how the API should be used
  • 65.
  • 66.
    Specification will getmore specific through usage Smart IT Engineering Ltd.
  • 67.
    Keep it Smallbut No Smaller API should satisfy its requirements
  • 68.
    When in doubtleave it out API extended but not curtailed once it is public Conceptual weight more important than bulk Decrease the conceptual weight, i.e. things to learn new Look for a power-to-weight ratio Do more by reusing interfaces, i.e. add implementations targeted at solving different problems Smart IT Engineering Ltd.
  • 69.
    Implementation shouldn't impactAPI Implementation details Confuse users
  • 70.
    Inhibit freedom tochange implementation Be aware of what is an implementation detail Do not over specify the behavior, e.g., hash functions Don't let implementation details “leak” into API On-disk and on-the-wire formats, exceptions Smart IT Engineering Ltd.
  • 71.
    Minimize Accessibility ofEverything Make classes and members as private as possible
  • 72.
    Public class shouldhave no public fields other than constants
  • 73.
  • 74.
    Allows modules tobe used, understood, built, tested and debugged independently Smart IT Engineering Ltd.
  • 75.
    Names Matter –API is a Language Names should be largely self-explanatory Avoid cryptic abbreviations Be consistent – Same word means same thing
  • 76.
    Be regular –strive for symmetry
  • 77.
    Code should readlike prose Smart IT Engineering Ltd.
  • 78.
    Documentation Matters Reuseis something that is far easier to say then to do. Doing it requires both good design and very good documentation. Even when we see good deign, which is still infrequently, we won't see the components reused without good documentation - D. L. Parnas, Software Aging. Proceedings of 16 th International Conference on Software Engineering, 1994 Smart IT Engineering Ltd.
  • 79.
    Document Religiously Documentevery class, interface, method, constructor, parameter and exception Class: What an instance represents
  • 80.
    Method: Contract betweenmethod and its client Preconditions, postconditions, side-effects Document state space very carefully Smart IT Engineering Ltd.
  • 81.
    Performance Consequence Baddecisions can limit performance
  • 82.
    Do not wrapAPI to gain performance
  • 83.
    Be aware ofstateful vs stateless requirements Smart IT Engineering Ltd.
  • 84.
    OOP Design PrinciplesSmart IT Engineering Ltd.
  • 85.
    Class Design MinimizeMutability Classes should be immutable
  • 86.
    If mutable keepstate space small, well-defined Subclass only where it makes sense Subclass only when is-a relationship exists
  • 87.
  • 88.
    Bad example ofinheritance is Properties of Java Document for inheritance else prohibit it Smart IT Engineering Ltd.
  • 89.
    Method Design Reduceneed for boilerplate code Generally done via cut/copy-and-paste Don't violate the principle of least astonishment User of API should not be surprised by behavior Fail Fast – report errors as soon as possible
  • 90.
    Provide programmatic accessto all data available in string form to avoid user parsing strings Smart IT Engineering Ltd.
  • 91.
    Method Design Overloadwith care Avoid ambiguous overloadings. Preferable no two methods with same number of arguments
  • 92.
    Just because youcan doesn't mean you should Use appropriate parameter and return types Favor interface over classes for input
  • 93.
    Use most specificpossible input parameter type
  • 94.
    Avoid using float(32 bits) in favor of double (64 bits) Smart IT Engineering Ltd.
  • 95.
    Method Design Useconsistent parameter ordering across methods
  • 96.
    Avoid long parameterlist Three or fewer parameters is ideal
  • 97.
    Lon list ofidentically typed params is harmful
  • 98.
    Break up methodsor create helper class to hold parameters Avoid return values that demand exceptional processing Return zero-length array or empty collection instead of null. Smart IT Engineering Ltd.
  • 99.
    Exception Design Throwexceptions to indicate exceptional conditions Don't force client to use exceptions for control flow
  • 100.
    Conversely, don't failsilently Favor unchecked exceptions Checked – Client must take recovery action
  • 101.
    Unchecked – Programmingerror Include failure capture information in exceptions Smart IT Engineering Ltd.
  • 102.
    Conclusion API designis a notable and rewarding craft
  • 103.
    This presentation coveredsome heuristics of the craft
  • 104.
  • 105.
    Once a APIis Public it can evolve but has to support earlier releases Smart IT Engineering Ltd.
  • 106.
    Questions? Smart ITEngineering Ltd.