Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Designing an API

Arjen Poutsma
September 28, 2016

Designing an API

Presented at TU Delft

Arjen Poutsma

September 28, 2016
Tweet

More Decks by Arjen Poutsma

Other Decks in Programming

Transcript

  1. About Arjen • University of Amsterdam • Philosophy • Artificial

    Intelligence • Twenty years of experience in Enterprise Software Development • Ten years of Spring • Spring Web Services • Spring REST • Spring Scala • Spring Reactive
  2. About Spring • Started in 2003, based on Rod Johnson’s

    book • Dowloaded million times per month • @MVC is most popular Java Web Framework • Upcoming 5.0 is going “Reactive”
  3. Principle of Least Astonishment “The result of performing some operation

    should be obvious, consistent, and predictable, based upon the name of the operation and other clues.” http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment
  4. Consistency • Naming • Types, methods, documentation, parameters, packages, …

    • Usage patterns • Thread-safety, immutability, parameter order, … • Similar to other popular APIs
  5. XML APIs • SAX void startElement(String namespaceURI, String localName, String

    qualifiedName, ...) • DOM Element createElementNS(String namespaceURI, String qualifiedName) • StAX void writeStartElement(String prefix, String localName, String namespaceURI)
  6. Simplicity • “Everything should be made as simple as possible,

    but no simpler” – Albert Einstein • “Make simple things simple, and complex things possible” – Alan Kay • “The simplest thing that could possibly work” – Kent Beck • “The first thing I could possibly think of”
  7. Hiding Complexity • The goal of any API is to

    hide complexity behind abstractions • Create multiple implementations to see if your abstractions are correct • The simplest thing that works! • You don’t have to ship them
  8. Cyclomatic Complexity • Number of linearly independent code paths •

    Trend is more important than absolute number https://en.wikipedia.org/wiki/Cyclomatic_complexity
  9. Dos and Don’ts ✓Focus outside ✓Use your own API ✓Multiple

    implementations of interfaces ✓Decrease Cyclomatic Complexity ✗Focus inside ✗Expose internal complexity
  10. Backward Compatibility • Source level • Existing source code compiles

    against new version • Binary level • Code compiled against old version will still run on new version • Java & Spring vs. Semantic Versioning
  11. What breaks compatibility? • Removing a public type • Adding

    abstract method • Removing an inherited method • Changing parameter • Change return type • Changing superclass
  12. Public vs Published • Published Interface is a class interface

    used outside the code base that it's defined in. http://martinfowler.com/bliki/PublishedInterface.html
  13. When in doubt, leave it out • You can always

    add a feature, but you can never take it out • So: do not add features you are unsure about http://www.artima.com/intv/blochP.html
  14. Change Event Horizon • Amount of time before you can

    undo the effect of a decision • Huge for programming languages • Small for Agile projects • APIs are more like languages https://dannorth.net/2006/05/28/how-simple-is-too-simple
  15. Dos and Don’ts ✓Backward compatibility ✓Leave out unclear features ✓Determine

    published interface ✗Add features prematurely ✗Remove code users depend on
  16. Flexibility • “Make simple things simple, and complex things possible”

    – Alan Kay • Ability to do things which you didn’t expect
  17. Strategy Interfaces • Allow people to customize inner workings •

    Use these strategies yourself • No hidden API!
  18. Circular Dependencies • Package A requires B, and B requires

    A • For example: java.lang and java.io • Spring has none • Tooling: IntelliJ, Structure 101 and SonarJ
  19. Dos and Don’ts ✓Create strategies to change default behavior ✓Use

    these strategies ✗Hidden APIs ✗Circular dependencies