SlideShare a Scribd company logo
API Design




Ferenc Mihaly
Senior Software Architect



November 9, 2011



                                                                             Slide 1
                            Copyright © Open Text Corporation. All rights reserved.
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 2
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 3
Why it matters?




      “Software artifacts that
      cannot attract programmers
      are not reused, and fade
      into oblivion.”
                      Brian Foote

                                     Slide 4
Why it matters?




      “The impact of API design
      choices on users sometimes
      shows time penalties of a
      factor of 3 to 10.”
                      Brad Myers

                                    Slide 5
Why it matters?




      “Public APIs, like
      diamonds, are forever. You
      have one chance to get it
      right so give it your best .”
                         Joshua Bloch

                                         Slide 6
Why it matters?




      “A key lesson here is that API
      is not just a documented class.
      And, APIs don't just happen;
      they are a big investment..”
                       Erich Gamma

                                        Slide 7
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 8
Consider the perspective of the caller

                          Implementation focus results in poor APIs:

                           class ContentInstance
                           java.lang.Object
                              extended by DataObject
                                 extended by ManagedObject
                                    extended by ExtensibleObject
                                       extended by ContentItem
                                          extended by ContentInstance


                           All Implemented Interfaces:
                           IAttributedObject, IChannelAssociate, IPersistable,
                             IRelatedAttribute, java.io.Serializable




                                                                           Slide 9
Consider the perspective of the caller

                           Implementation perspective
                            class Document {
                                String getTag();
                                boolean isTagged(String tag);
                                void remove(String tag);
                                void removeTag(String tag);
                            }




                           Caller perspective
                          if(document.getTag().equals(“Best Practices”))


                          if(document.isTagged(“Best Practices”))


                          document.remove(“Best Practices”)


                          document.removeTag(“Best Practices”)


                                                                           Slide 10
Consider the perspective of the caller

                          Write client code first
                          Write client code for all major use cases
                          Ask yourself
                            Is this code simple?
                            Is this code intuitive?
                            Is this code consistent?
                            Is this code performant?
                            Does this code reveal any implementation
                             details?

                          Not a wasted effort
                            Code samples reused in tests
                            Code samples reused in documentation



                                                                       Slide 11
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 12
Keep it simple




      “Simplicity is the ultimate
      sophistication”

                       Leonardo da Vinci

                                            Slide 13
Keep it Simple

 Measuring Conceptual Complexity
  try {
      AuthenticationProvider20 provider = new LocalAuthenticationProvider19();
      SearchCriteria18 criteria = new SearchCriteria17(EntityName16.USER15);
      criteria.addPropertyToFetch14(PropertyName13.COMMON_NAME12);
      criteria.addPropertyToFetch(PropertyName.PHONE11);
      criteria.addPropertyToMatch10(PropertyName.DEPARTMENT9, "R&D");
      criteria.addPropertyToMatch(PropertyName.LOCATION8, "Waterloo");
      criteria.setSortProperty7(PropertyName.COMMON_NAME);
      ProfileIterator6 iterator = provider.search5(criteria);
      while(iterator.hasNext()4){
          Profile3 profile = iterator.next()2;
          Property1 commonName = profile.getProperty0(PropertyName.COMMON_NAME);
          Property phone = profile.getProperty(PropertyName.PHONE);
          System.out.println(commonName.getValue()-1, “ ”, phone.getValue());
      }
  }
  catch(AuthenticationProviderException-2 e) {
  }

 The higher score is better; less than zero means too complex!
                                                                                   Slide 14
Keep it simple

                  Accidental complexity
                    Avoid asking callers to extend classes
                    Avoid asking callers to implement interfaces
                    Avoid “Gang of Four” design patterns
                    Provide alternate implementations
                    Handle change requests carefully



                  Essential complexity
                    Organize large APIs into smaller parts
                    Increase API granularity
                    Give up some control
                    Leave functionality out!



                                                              Slide 15
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 16
Strive for consistency



                          Do the same thing the same way every
                          time
                          Rules, patterns, and conventions makes
                          everyday life more predictable
                          A consistent API has no frivolous or
                          unnecessary variations in it
                            int fscanf(FILE* stream, const char* format,...);
                            char* fgets(char* str, int num, FILE* stream);

                          Consistent APIs are easy to
                           learn, remember and use




                                                                             Slide 17
Strive for consistency

                          Follow established conventions!
                            C#:   IPublishable, PublishDocument()

                            Java: Publishable,   publishDocument()


                          Create your own conventions
                            eliminate unnecessary variations
                            parameter ordering, error handling, use of
                             null, etc.

                          Enforce consistency with code reviews
                          Use design patterns
                            Business Service – Business Object

                          Beware of false consistency
                            Attributes getAttributes() throws RemoteException




                                                                          Slide 18
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 19
Choose memorable names

                     Avoid silly naming mistakes
                          antiquated naming conventions
                          spelling and grammar
                          synonyms
                          overly generic terms
                          inaccurate terms
                          meaningless terms

                     Choose names first
                          Design abstractions are difficult to name
                            – LLValue, DTree
                            – DocumentWrapperReferenceBuilderFactory
                          Best names come from the problem domain
                            – Familiar, intuitive, accurate, memorable
                          API as a domain-specific extension
                            – Establishing a domain vocabulary



                                                                         Slide 20
Choose memorable names

                     Survey problem domain for suitable names

                          “In online computer systems terminology, a tag is a non-
                           hierarchical keyword or term assigned to a piece of
                           information (such as an internet bookmark, digital
                           image, or computer file). This kind of metadata helps
                           describe an item and allows it to be found again by
                           browsing or searching. Tags are generally chosen
                           informally and personally by the item's creator or by its
                           viewer, depending on the system.”




                     Let names guide design

                         void assignTag(Item item, String tag);
                         Metadata describeItem(Item item);
                         Item[] searchByTag(String tag);



                                                                                   Slide 21
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 22
Specify the behavior

                        Consider the class:
                       TeamsIdentifier
                         Uniquely identifies an Artesia entity.
                       Methods:
                          TeamsIdentifier(String id)

                             Constructs an identifier from a string.
                          java.lang.String asString()

                             Returns the id as a String.
                          TeamsIdentifier[] asTeamsIdArray()

                             Convenience method to return this id as an array.
                          boolean equals(java.lang.Object o)
                          boolean equalsId(TeamsIdentifier id)

                             Checks if two ids are equal.
                          java.lang.String getTeamsId()

                             Intended for hibernate use only.
                          java.lang.String toString()

                             Returns a string representation of the id.


                                                                                 Slide 23
Specify the behavior

                        Try answering the questions:
                       Expression                        True or False
                       TeamsIdentifier id1 = new
                       TeamsIdentifier(“name”);                ?
                       TeamsIdentifier id2 = new
                       TeamsIdentifier(“Name”);
                       id1.equals(id2)
                       id1.equalsId(id2);                      ?
                       id1.toString().equals(“name”)           ?
                       id1.getTeamsId.equals(“name”)           ?
                       TeamsIdentifier id = new
                       TeamsIdentifier(“a.b.c”)                ?
                       id.asTeamsIdArray().length == 3
                       TeamsIdentifier id = new
                       TeamsIdentifier(“a:b:c”)                ?
                       id.asTeamsIdArray().length == 3
                       AssetIdentifier assetId = new           ?
                       AssetIdentifier(“Donald”)
                       UserIdentifier userId = new
                       UserIdentifier(“Donald”)
                       assetId.equals(userId)
                       assetId.equalsId(userId)                ?




                                                                     Slide 24
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 25
Make it safe

                Developers make mistakes
                Prevent access to dangerous code
                  Keep implementation code private
                  Prevent class extension
                  Control class initialization

                Prevent data corruption
                Maximize compiler checks
                Avoid out and in-out parameters
                Check arguments at runtime
                Provide informative error messages
                Make method calls atomic
                Write thread-safe code
                                                      Slide 26
Make it safe
               public Job {
                    private cancelling = false;
                    public void cancel() {
                    ...
                    cancelling = true;
                    onCancel();
                    cancelling = false;
                        ...
                    }
                    //Override this for custom cleanup when cancelling
                    protected void onCancel() {
                    }
                    public void execute() {
                    if(cancelling) throw
                   IllegalStateException(“Forbidden call to execute()
                   from onCancel()”);
                   ...
                    }
               }

                                                                    Slide 27
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 28
Anticipate evolution

                        Maintain binary backwards compatibility
                          Clients work without an explicit upgrade
                          Technology-dependent compatibility rules
                             – Implemented Java interfaces not extensible
                             – Java constant values hard-coded in client code
                             – C++ has more compatibility issues than C
                             – Adding field or method breaks C++, not Java
                          Not the same as source-compatibility!

                        Maintain functional backwards compatibility
                          Allowed changes
                             – Weakening preconditions
                             – Strengthening postconditions
                             – Strengthening invariants
                          Testing is essential
                        SPIs evolve very differently from APIs
                                                                          Slide 29
Anticipate evolution

                        API versioning cannot be entirely avoided
                          major technology innovations
                          unanticipated requirements
                          quality degradation over time
                        Incompatible API = major API upgrade
                          Planned, not accidental
                          Significant new functionality
                          Cleanup and reorganization
                          Removal of deprecated constructs

                        Old API remains unchanged
                          must co-exist with the new API
                          Must be supported for years
                          often re-implemented as an Adaptor


                                                                Slide 30
Agenda

 Introduction: Why it matters?
 Consider the perspective of the caller
 Keep it simple
 Strive for consistency
 Choose memorable names
 Specify the behaviour
 Make it safe
 Anticipate evolution
 Write helpful documentation




                                           Slide 31
Write helpful documentation

                        FALSE: APIs are self documenting
                          Behavior
                          Design concepts and abstractions
                          Design patterns and conventions
                        TRUE: Nobody reads documentation
                          Just-in-time learning preferred
                          Documentation is referenced
                          Information can be hard to find
                        FALSE: Nobody uses documentation
                          Adobe Flex Online, July, 2008
                          24,293 programmers, 101,289 queries

                        TRUE: Users don’t want documentation
                          They want assistance (help)

                                                                 Slide 32
Write helpful documentation

                        Typical question from an online forum

                          Question: “With java.sql.ResultSet is there a
                           way to get a column's name as a String by
                           using the column's index? I had a look
                           through the API doc but I can't find anything.”


                          Answer: “See ResultSetMetaData:
                         ResultSet rs =
                           stmt.executeQuery("SELECT a, b, c
                           FROM TABLE2");
                         ResultSetMetaData rsmd =
                          rs.getMetaData();
                         String name =
                          rsmd.getColumnName(1);”



                                                                      Slide 33
Write helpful documentation

                        Think like a friend providing assistance
                          Write short sections (10 minutes or less)
                          Answer specific questions
                          Make it easy to find



                        Forms of API documentation
                          Developer’s Guide (overview)
                          Reference manual (details)
                          Cookbook (usage scenarios, code snippets)
                          Working code (test drive)
                          Tutorial (optional)
                          FAQ or Knowledge Base (ease of update)



                                                                       Slide 34
Summary


1) Consider the perspective of the caller
2) Keep it simple
3) Strive for consistency
4) Choose memorable names
5) Specify the behaviour
6) Make it safe
7) Anticipate evolution
8) Write helpful documentation

                                            Slide 35
Further reading:




                   The Amiable API
             http://theamiableapi.com




                                        Slide 36
Thank You




            Slide 37

More Related Content

PDF
Airbus Internship Presentation 2012
PDF
Framework Engineering_Final
PDF
Refactoring AOMs For AgilePT2010
PDF
Design patterns difference between interview questions
PDF
Testing untestable code - oscon 2012
PPT
Introduction to OSLC
PDF
Lean agile pt
PDF
Pragmatic notdogmatictdd
Airbus Internship Presentation 2012
Framework Engineering_Final
Refactoring AOMs For AgilePT2010
Design patterns difference between interview questions
Testing untestable code - oscon 2012
Introduction to OSLC
Lean agile pt
Pragmatic notdogmatictdd

What's hot (19)

PDF
01 traditional analytics
PPT
Introduction to OSLC and Linked Data
PDF
How to Design Frameworks
PDF
Java Technicalities
PPTX
Application Quality with Visual Studio 2010
PDF
Pragmatic Not Dogmatic TDD Agile2012 by Joseph Yoder and Rebecca Wirfs-Brock
PDF
【中級編】実用レベルのLINE Botを爆速で! チャットボット開発者が絶対に知るべき3つのツボ
PDF
Android NDK: Entrando no Mundo Nativo
PPT
Extending Appcelerator Titanium Mobile through Native Modules
PPTX
Introduction to Module Development with Appcelerator Titanium
DOC
Dot net 2005 vs 2003
PDF
Cocoa fundamentalswert
PPTX
2010 06-24 karlsruher entwicklertag
PDF
Testing on Android
PDF
Epic.NET: Processes, patterns and architectures
PDF
An Overview of Open Source & FOSS4G
PDF
Quality Best Practices & Toolkit for Enterprise Flex
01 traditional analytics
Introduction to OSLC and Linked Data
How to Design Frameworks
Java Technicalities
Application Quality with Visual Studio 2010
Pragmatic Not Dogmatic TDD Agile2012 by Joseph Yoder and Rebecca Wirfs-Brock
【中級編】実用レベルのLINE Botを爆速で! チャットボット開発者が絶対に知るべき3つのツボ
Android NDK: Entrando no Mundo Nativo
Extending Appcelerator Titanium Mobile through Native Modules
Introduction to Module Development with Appcelerator Titanium
Dot net 2005 vs 2003
Cocoa fundamentalswert
2010 06-24 karlsruher entwicklertag
Testing on Android
Epic.NET: Processes, patterns and architectures
An Overview of Open Source & FOSS4G
Quality Best Practices & Toolkit for Enterprise Flex
Ad

Viewers also liked (20)

PPTX
Multi threading
PPT
Java util concurrent
PPT
12 multi-threading
 
PPTX
Alternate concurrency models
PPT
Why both scrum and lean in dist dev 07092010
PPT
Inter threadcommunication.38
PPTX
Best Coding Practices in Java and C++
PDF
(Ebook resume) job interview - 101 dynamite answers to interview questions ...
PDF
Concurrency and Multithreading Demistified - Reversim Summit 2014
PPTX
Inner Classes & Multi Threading in JAVA
PPT
Data Structures- Part7 linked lists
PPTX
Advanced Introduction to Java Multi-Threading - Full (chok)
PPT
Java Performance, Threading and Concurrent Data Structures
PPT
Data Structures- Part5 recursion
PPTX
Java concurrency - Thread pools
PDF
Top 10 Java Interview Questions and Answers 2014
PDF
Java SE 8 library design
DOCX
Java questions for viva
DOCX
Java codes
PDF
Java programming-examples
Multi threading
Java util concurrent
12 multi-threading
 
Alternate concurrency models
Why both scrum and lean in dist dev 07092010
Inter threadcommunication.38
Best Coding Practices in Java and C++
(Ebook resume) job interview - 101 dynamite answers to interview questions ...
Concurrency and Multithreading Demistified - Reversim Summit 2014
Inner Classes & Multi Threading in JAVA
Data Structures- Part7 linked lists
Advanced Introduction to Java Multi-Threading - Full (chok)
Java Performance, Threading and Concurrent Data Structures
Data Structures- Part5 recursion
Java concurrency - Thread pools
Top 10 Java Interview Questions and Answers 2014
Java SE 8 library design
Java questions for viva
Java codes
Java programming-examples
Ad

Similar to Developer Friendly API Design (20)

PDF
Api design
PPTX
Clean code, Feb 2012
PDF
How to design good api
PPTX
How to define an api
PDF
How To Design A Good A P I And Why It Matters G O O G L E
PDF
Keynoteof A P I
PDF
Clean code
PPT
Api desgin
PPTX
Golden Rules of API Design
PPT
How to design effective APIs
PDF
Be My API How to Implement an API Strategy Everyone will Love
PDF
How to Design a Good API and Why it Matters.pdf
PPTX
How to build Sdk? Best practices
PPTX
Code reviews
PDF
Practices and tools for building better API (JFall 2013)
PDF
Practices and tools for building better APIs
PPTX
Clean Code Part III - Craftsmanship at SoCal Code Camp
PDF
ApiDesign
PDF
API design
PPTX
Do's and Don'ts of APIs
Api design
Clean code, Feb 2012
How to design good api
How to define an api
How To Design A Good A P I And Why It Matters G O O G L E
Keynoteof A P I
Clean code
Api desgin
Golden Rules of API Design
How to design effective APIs
Be My API How to Implement an API Strategy Everyone will Love
How to Design a Good API and Why it Matters.pdf
How to build Sdk? Best practices
Code reviews
Practices and tools for building better API (JFall 2013)
Practices and tools for building better APIs
Clean Code Part III - Craftsmanship at SoCal Code Camp
ApiDesign
API design
Do's and Don'ts of APIs

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
SOPHOS-XG Firewall Administrator PPT.pptx
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25-Week II
A comparative analysis of optical character recognition models for extracting...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
20250228 LYD VKU AI Blended-Learning.pptx
1. Introduction to Computer Programming.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Developer Friendly API Design

  • 1. API Design Ferenc Mihaly Senior Software Architect November 9, 2011 Slide 1 Copyright © Open Text Corporation. All rights reserved.
  • 2. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 2
  • 3. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 3
  • 4. Why it matters? “Software artifacts that cannot attract programmers are not reused, and fade into oblivion.” Brian Foote Slide 4
  • 5. Why it matters? “The impact of API design choices on users sometimes shows time penalties of a factor of 3 to 10.” Brad Myers Slide 5
  • 6. Why it matters? “Public APIs, like diamonds, are forever. You have one chance to get it right so give it your best .” Joshua Bloch Slide 6
  • 7. Why it matters? “A key lesson here is that API is not just a documented class. And, APIs don't just happen; they are a big investment..” Erich Gamma Slide 7
  • 8. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 8
  • 9. Consider the perspective of the caller  Implementation focus results in poor APIs: class ContentInstance java.lang.Object extended by DataObject extended by ManagedObject extended by ExtensibleObject extended by ContentItem extended by ContentInstance All Implemented Interfaces: IAttributedObject, IChannelAssociate, IPersistable, IRelatedAttribute, java.io.Serializable Slide 9
  • 10. Consider the perspective of the caller  Implementation perspective class Document { String getTag(); boolean isTagged(String tag); void remove(String tag); void removeTag(String tag); }  Caller perspective if(document.getTag().equals(“Best Practices”)) if(document.isTagged(“Best Practices”)) document.remove(“Best Practices”) document.removeTag(“Best Practices”) Slide 10
  • 11. Consider the perspective of the caller  Write client code first  Write client code for all major use cases  Ask yourself  Is this code simple?  Is this code intuitive?  Is this code consistent?  Is this code performant?  Does this code reveal any implementation details?  Not a wasted effort  Code samples reused in tests  Code samples reused in documentation Slide 11
  • 12. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 12
  • 13. Keep it simple “Simplicity is the ultimate sophistication” Leonardo da Vinci Slide 13
  • 14. Keep it Simple  Measuring Conceptual Complexity try { AuthenticationProvider20 provider = new LocalAuthenticationProvider19(); SearchCriteria18 criteria = new SearchCriteria17(EntityName16.USER15); criteria.addPropertyToFetch14(PropertyName13.COMMON_NAME12); criteria.addPropertyToFetch(PropertyName.PHONE11); criteria.addPropertyToMatch10(PropertyName.DEPARTMENT9, "R&D"); criteria.addPropertyToMatch(PropertyName.LOCATION8, "Waterloo"); criteria.setSortProperty7(PropertyName.COMMON_NAME); ProfileIterator6 iterator = provider.search5(criteria); while(iterator.hasNext()4){ Profile3 profile = iterator.next()2; Property1 commonName = profile.getProperty0(PropertyName.COMMON_NAME); Property phone = profile.getProperty(PropertyName.PHONE); System.out.println(commonName.getValue()-1, “ ”, phone.getValue()); } } catch(AuthenticationProviderException-2 e) { }  The higher score is better; less than zero means too complex! Slide 14
  • 15. Keep it simple  Accidental complexity  Avoid asking callers to extend classes  Avoid asking callers to implement interfaces  Avoid “Gang of Four” design patterns  Provide alternate implementations  Handle change requests carefully  Essential complexity  Organize large APIs into smaller parts  Increase API granularity  Give up some control  Leave functionality out! Slide 15
  • 16. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 16
  • 17. Strive for consistency  Do the same thing the same way every time  Rules, patterns, and conventions makes everyday life more predictable  A consistent API has no frivolous or unnecessary variations in it int fscanf(FILE* stream, const char* format,...); char* fgets(char* str, int num, FILE* stream);  Consistent APIs are easy to learn, remember and use Slide 17
  • 18. Strive for consistency  Follow established conventions!  C#: IPublishable, PublishDocument()  Java: Publishable, publishDocument()  Create your own conventions  eliminate unnecessary variations  parameter ordering, error handling, use of null, etc.  Enforce consistency with code reviews  Use design patterns  Business Service – Business Object  Beware of false consistency  Attributes getAttributes() throws RemoteException Slide 18
  • 19. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 19
  • 20. Choose memorable names  Avoid silly naming mistakes  antiquated naming conventions  spelling and grammar  synonyms  overly generic terms  inaccurate terms  meaningless terms  Choose names first  Design abstractions are difficult to name – LLValue, DTree – DocumentWrapperReferenceBuilderFactory  Best names come from the problem domain – Familiar, intuitive, accurate, memorable  API as a domain-specific extension – Establishing a domain vocabulary Slide 20
  • 21. Choose memorable names  Survey problem domain for suitable names  “In online computer systems terminology, a tag is a non- hierarchical keyword or term assigned to a piece of information (such as an internet bookmark, digital image, or computer file). This kind of metadata helps describe an item and allows it to be found again by browsing or searching. Tags are generally chosen informally and personally by the item's creator or by its viewer, depending on the system.”  Let names guide design void assignTag(Item item, String tag); Metadata describeItem(Item item); Item[] searchByTag(String tag); Slide 21
  • 22. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 22
  • 23. Specify the behavior  Consider the class: TeamsIdentifier Uniquely identifies an Artesia entity. Methods: TeamsIdentifier(String id) Constructs an identifier from a string. java.lang.String asString() Returns the id as a String. TeamsIdentifier[] asTeamsIdArray() Convenience method to return this id as an array. boolean equals(java.lang.Object o) boolean equalsId(TeamsIdentifier id) Checks if two ids are equal. java.lang.String getTeamsId() Intended for hibernate use only. java.lang.String toString() Returns a string representation of the id. Slide 23
  • 24. Specify the behavior  Try answering the questions: Expression True or False TeamsIdentifier id1 = new TeamsIdentifier(“name”); ? TeamsIdentifier id2 = new TeamsIdentifier(“Name”); id1.equals(id2) id1.equalsId(id2); ? id1.toString().equals(“name”) ? id1.getTeamsId.equals(“name”) ? TeamsIdentifier id = new TeamsIdentifier(“a.b.c”) ? id.asTeamsIdArray().length == 3 TeamsIdentifier id = new TeamsIdentifier(“a:b:c”) ? id.asTeamsIdArray().length == 3 AssetIdentifier assetId = new ? AssetIdentifier(“Donald”) UserIdentifier userId = new UserIdentifier(“Donald”) assetId.equals(userId) assetId.equalsId(userId) ? Slide 24
  • 25. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 25
  • 26. Make it safe  Developers make mistakes  Prevent access to dangerous code  Keep implementation code private  Prevent class extension  Control class initialization  Prevent data corruption  Maximize compiler checks  Avoid out and in-out parameters  Check arguments at runtime  Provide informative error messages  Make method calls atomic  Write thread-safe code Slide 26
  • 27. Make it safe public Job { private cancelling = false; public void cancel() { ... cancelling = true; onCancel(); cancelling = false; ... } //Override this for custom cleanup when cancelling protected void onCancel() { } public void execute() { if(cancelling) throw IllegalStateException(“Forbidden call to execute() from onCancel()”); ... } } Slide 27
  • 28. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 28
  • 29. Anticipate evolution  Maintain binary backwards compatibility  Clients work without an explicit upgrade  Technology-dependent compatibility rules – Implemented Java interfaces not extensible – Java constant values hard-coded in client code – C++ has more compatibility issues than C – Adding field or method breaks C++, not Java  Not the same as source-compatibility!  Maintain functional backwards compatibility  Allowed changes – Weakening preconditions – Strengthening postconditions – Strengthening invariants  Testing is essential  SPIs evolve very differently from APIs Slide 29
  • 30. Anticipate evolution  API versioning cannot be entirely avoided  major technology innovations  unanticipated requirements  quality degradation over time  Incompatible API = major API upgrade  Planned, not accidental  Significant new functionality  Cleanup and reorganization  Removal of deprecated constructs  Old API remains unchanged  must co-exist with the new API  Must be supported for years  often re-implemented as an Adaptor Slide 30
  • 31. Agenda  Introduction: Why it matters?  Consider the perspective of the caller  Keep it simple  Strive for consistency  Choose memorable names  Specify the behaviour  Make it safe  Anticipate evolution  Write helpful documentation Slide 31
  • 32. Write helpful documentation  FALSE: APIs are self documenting  Behavior  Design concepts and abstractions  Design patterns and conventions  TRUE: Nobody reads documentation  Just-in-time learning preferred  Documentation is referenced  Information can be hard to find  FALSE: Nobody uses documentation  Adobe Flex Online, July, 2008  24,293 programmers, 101,289 queries  TRUE: Users don’t want documentation  They want assistance (help) Slide 32
  • 33. Write helpful documentation  Typical question from an online forum  Question: “With java.sql.ResultSet is there a way to get a column's name as a String by using the column's index? I had a look through the API doc but I can't find anything.”  Answer: “See ResultSetMetaData: ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2"); ResultSetMetaData rsmd = rs.getMetaData(); String name = rsmd.getColumnName(1);” Slide 33
  • 34. Write helpful documentation  Think like a friend providing assistance  Write short sections (10 minutes or less)  Answer specific questions  Make it easy to find  Forms of API documentation  Developer’s Guide (overview)  Reference manual (details)  Cookbook (usage scenarios, code snippets)  Working code (test drive)  Tutorial (optional)  FAQ or Knowledge Base (ease of update) Slide 34
  • 35. Summary 1) Consider the perspective of the caller 2) Keep it simple 3) Strive for consistency 4) Choose memorable names 5) Specify the behaviour 6) Make it safe 7) Anticipate evolution 8) Write helpful documentation Slide 35
  • 36. Further reading: The Amiable API http://theamiableapi.com Slide 36
  • 37. Thank You Slide 37