SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
Practices	
  and	
  Tools	
  for	
  Building	
  Better	
  APIs

	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  @PeterHendriks80,	
  +PeterHendriks
Objectives	
  of	
  this	
  talk
• API	
  from	
  a	
  Java	
  app	
  developer	
  perspective	
  
!

•
•
•
•

Why	
  API	
  is	
  important	
  
What	
  makes	
  a	
  better	
  API	
  
Practical	
  tips	
  and	
  tools	
  
Measuring	
  your	
  existing	
  codebase
Some	
  API	
  terminology
• API	
  Consumer:	
  code	
  that	
  uses	
  API	
  
• API:	
  the	
  interface	
  
• API	
  Implementer:	
  code	
  that	
  implements	
  API
API	
  
Consumer

API

API	
  
Implementer

Data	
  Access	
  Object

JDBC

Database	
  Driver
We	
  are	
  using	
  a	
  lot	
  of	
  API
Bazillion	
  Open	
  
Source	
  Web	
  
Framework	
  API’s	
  
Spring:	
  27	
  API’s
Java	
  EE:	
  37	
  API’s
Java	
  SE:	
  45	
  API’s

Enterprise	
  Service	
  
API’s
Public	
  Service	
  API’s
Cloud	
  API’s
Middleware	
  API’s
We	
  are	
  building	
  more	
  of	
  them,	
  too
• API	
  is	
  key	
  to	
  a	
  succesful	
  module	
  design	
  
– Essential	
  for	
  agile	
  teams	
  at	
  scale
Customer	
  
Front-­‐End

Warehouse	
  
Front-­‐End

Order	
  
Management
Shared	
  code
Distance	
  to	
  API	
  implementation	
  is	
  increasing
• API	
  quality	
  increasing	
  important	
  when	
  
implementation	
  becomes	
  a	
  black	
  box

Same	
  
team

Different	
  
team

Open	
  
source

Binary	
  
only

Other	
  
datacenter
The	
  API	
  we	
  use	
  shapes	
  our	
  code
• Most	
  code	
  we	
  write	
  calls	
  an	
  API	
  
• Code	
  that	
  calls	
  an	
  API	
  is	
  shaped	
  to	
  fit	
  the	
  API	
  
• Therefore,	
  APIs	
  determine	
  the	
  shape	
  of	
  
application	
  code	
  
– In	
  some	
  ways,	
  even	
  more	
  impactful	
  than	
  high	
  level	
  
design,	
  test	
  driven	
  development	
  or	
  code	
  
craftsmanship
!

http://docs.oracle.com/javaee/1.4/tutorial/examples/jaxp/xslt/samples/
TransformationApp04.java	
  

!

public	
  class	
  TransformationApp04	
  {	
  
	
  	
  	
  	
  public	
  static	
  void	
  main(String[]	
  argv)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  if	
  (argv.length	
  !=	
  1)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.err.println("Usage:	
  java	
  TransformationApp	
  filename");	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.exit(1);	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  

!

	
  	
  	
  	
  	
  	
  	
  	
  //	
  Create	
  the	
  sax	
  "parser".	
  
	
  	
  	
  	
  	
  	
  	
  	
  AddressBookReader02	
  saxReader	
  =	
  new	
  AddressBookReader02();	
  
var	
  json_data	
  =	
  JSON.stringify(obj);
	
  	
  	
  	
  	
  	
  	
  	
  try	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  File	
  f	
  =	
  new	
  File(argv[0]);	
  

!
!

	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Use	
  a	
  Transformer	
  for	
  output	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  TransformerFactory	
  tFactory	
  =	
  TransformerFactory.newInstance();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Transformer	
  transformer	
  =	
  tFactory.newTransformer();	
  

!

	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Use	
  the	
  parser	
  as	
  a	
  SAX	
  source	
  for	
  input	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  FileReader	
  fr	
  =	
  new	
  FileReader(f);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  BufferedReader	
  br	
  =	
  new	
  BufferedReader(fr);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  InputSource	
  inputSource	
  =	
  new	
  InputSource(fr);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  SAXSource	
  source	
  =	
  new	
  SAXSource(saxReader,	
  inputSource);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  StreamResult	
  result	
  =	
  new	
  StreamResult(System.out);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  transformer.transform(source,	
  result);	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  catch	
  (TransformerConfigurationException	
  tce)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Error	
  generated	
  by	
  the	
  parser	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.out.println("n**	
  Transformer	
  Factory	
  error");	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.out.println("	
  	
  	
  "	
  +	
  tce.getMessage());	
  

!
What	
  makes	
  a	
  better	
  API
API	
  Quality:	
  Value
• Value	
  =	
  benefits	
  -­‐	
  costs	
  
• Benefits	
  
– Features	
  
– Data	
  
– ...	
  

• Costs	
  
–
–
–
–

License/pay	
  per	
  use	
  
Performance/footprint	
  
Security	
  
…
API	
  Quality:	
  Usability
• API	
  Design	
  ==	
  Human	
  Interface	
  design	
  
– Although	
  API	
  is	
  for	
  machine/machine	
  interaction,	
  
APIs	
  need	
  to	
  be	
  understood	
  by	
  programmers	
  to	
  
write	
  the	
  code	
  
– Programmers	
  are	
  humans
API	
  is	
  not	
  just	
  about	
  capability	
  and	
  structural	
  
design,	
  but	
  also	
  about	
  human	
  understanding
A	
  simple	
  model	
  for	
  API	
  Usability
Generic	
  usability	
  model	
  by	
  Ben	
  Shneiderman:
Learnability:	
  How	
  easy	
  is	
  it	
  to	
  accomplish	
  basic	
  
tasks	
  the	
  first	
  time?	
  
Efficiency:	
  Once	
  users	
  learned	
  the	
  design,	
  how	
  
quickly	
  can	
  they	
  perform	
  tasks?	
  
Memorability:	
  When	
  users	
  return	
  after	
  a	
  while,	
  
how	
  easily	
  can	
  they	
  re-­‐establish	
  proficiency?	
  
Errors:	
  How	
  many	
  errors	
  do	
  users	
  make,	
  how	
  
severe	
  are	
  these	
  errors,	
  and	
  how	
  easily	
  can	
  they	
  
recover	
  from	
  the	
  errors?	
  
Satisfaction:	
  How	
  pleasant	
  is	
  it	
  to	
  use	
  the	
  design?
API	
  Quality:	
  stability
Able	
  to	
  enhance/grow	
  implementation	
  without	
  
breaking	
  existing	
  consumer	
  code
Google	
  AppEngine	
  release	
  notes:	
  
Version	
  1.7.5	
  -­‐	
  February	
  13,	
  2013	
  
The	
  Conversion	
  API,	
  which	
  was	
  decommissioned	
  last	
  release,	
  has	
  been	
  
removed	
  from	
  the	
  SDK.	
  In	
  a	
  future	
  release,	
  the	
  API	
  will	
  be	
  removed	
  from	
  the	
  
runtime	
  and	
  applications	
  that	
  attempt	
  to	
  use	
  it	
  may	
  stop	
  working.	
  Applications	
  
in	
  production	
  that	
  import	
  the	
  library	
  should	
  be	
  fixed	
  as	
  soon	
  as	
  possible.	
  
Version	
  1.8.4	
  -­‐	
  September	
  9,	
  2013	
  
The	
  Conversion	
  API,	
  a	
  decommissioned	
  feature,	
  has	
  been	
  removed	
  from	
  the	
  
SDK.	
  In	
  the	
  next	
  release,	
  the	
  API	
  will	
  be	
  removed	
  from	
  the	
  runtime	
  and	
  any	
  
attempt	
  to	
  import	
  the	
  API	
  will	
  raise	
  an	
  exception.	
  
Version	
  1.8.5	
  -­‐	
  September	
  25,	
  2013	
  
The	
  Conversion	
  API,	
  a	
  decommissioned	
  feature,	
  has	
  been	
  removed	
  from	
  the	
  
runtime.	
  Applications	
  in	
  production	
  that	
  import	
  the	
  API	
  should	
  be	
  fixed	
  as	
  
soon	
  as	
  possible.
API	
  Quality:	
  A	
  hierarchy	
  of	
  needs
Stability	
  only	
  matters	
  when	
  
API	
  is	
  valuable	
  and	
  usable	
  

Stable

Usable

Valuable
Building	
  Better	
  APIs
API	
  Design:	
  start	
  with	
  examples
• Examples	
  most	
  important	
  in	
  API	
  
documentation	
  
– Concrete	
  use	
  case,	
  usability	
  check	
  

• Make	
  examples	
  exemplary	
  
– Example	
  code	
  will	
  be	
  copy-­‐pasted	
  

• Create	
  a	
  cheat	
  sheet	
  
– Improves	
  learnability	
  
– Shows	
  design	
  issues
API	
  Design:	
  multiple	
  perspectives
Names,	
  Completeness,	
  
Grouping

UML:

public	
  class	
  Document	
  {	
  

!
!

	
  	
  private	
  List<Page>	
  pages	
  =	
  new	
  ArrayList<>();	
  

Impl:

Consumer:

	
  	
  public	
  Page	
  addPage()	
  {	
  
	
  	
  	
  	
  Page	
  page	
  =	
  new	
  Page();	
  
	
  	
  	
  	
  pages.add(page);	
  
	
  	
  	
  	
  page.setPageNumber(pages.size());	
  
	
  	
  	
  	
  return	
  page;	
  
	
  	
  }	
  
	
  	
  	
  
	
  	
  public	
  void	
  report()	
  {...}	
  
}

Page	
  page	
  =	
  document.addPage();	
  
page.setContents("Once	
  upon	
  a	
  time...");

Can	
  it	
  be	
  built?

Is	
  it	
  usable?
API	
  practice:	
  Keep	
  it	
  simple
• Try	
  to	
  stick	
  to	
  just	
  methods	
  and	
  constructors	
  
– Factories,	
  Delegates,	
  etc.	
  not	
  always	
  necessary	
  
!

• “When	
  in	
  doubt	
  leave	
  it	
  out”	
  
– Less	
  classes,	
  methods	
  
– Less	
  parameters,	
  return	
  values

Josh	
  Bloch,	
  Java	
  API	
  guru
API	
  practice:	
  avoid	
  verbose	
  constructs
• Constructs	
  that	
  create	
  verbose	
  consumer	
  code	
  
– Extending	
  class/implementing	
  interface	
  
– Checked	
  exception	
  
– Required	
  casting	
  
– Required	
  generic	
  type	
  
– Required	
  if,	
  for,	
  while,	
  try/finally	
  
– Required	
  synchronization/locking	
  
– Hard	
  coded	
  parameter	
  value
Example:	
  doing	
  some	
  loggin’
• Demo:	
  different	
  styles	
  of	
  logging	
  API
Why	
  Lambdas	
  are	
  so	
  much	
  better	
  than	
  inner	
  
classes
• Inner	
  classes,	
  or	
  extending	
  existing	
  classes	
  
often	
  needed	
  
– Resource	
  handling	
  
– Looping/visiting	
  collections	
  
– Event	
  handling	
  

• Lambdas	
  provide	
  a	
  much	
  more	
  usable	
  
construct	
  
– Shorter,	
  safer,	
  better	
  footprint/performance
Example:	
  Lambda	
  benefits
• Demo:	
  Netbeans	
  example
API	
  practice:	
  fail	
  fast,	
  fail	
  hard
• Fail	
  immediately	
  on	
  wrong	
  input	
  
• Limit	
  the	
  use	
  of	
  null	
  as	
  valid	
  input/output
public	
  void	
  save(String	
  filename)	
  {	
  
	
  	
  if	
  (filename	
  ==	
  null)	
  {	
  
	
  	
  	
  	
  return;	
  //	
  bad	
  api:	
  error	
  is	
  ignored!	
  
	
  	
  }	
  
	
  	
  ...	
  //	
  save	
  happens	
  here	
  
}

Better

public	
  void	
  save(String	
  filename)	
  {	
  
	
  	
  Objects.requireNonNull(filename);	
  
	
  	
  //	
  Throws	
  NPE	
  immediately	
  
	
  	
  ...	
  //	
  save	
  happens	
  here	
  
}
API	
  Tool:	
  checking	
  for	
  null
• Tool:	
  FindBugs,	
  a	
  static	
  code	
  analyzer	
  
– Reports	
  errors	
  at	
  compile-­‐time	
  

• Annotations	
  for	
  Software	
  Defect	
  Detection

(JSR	
  305:	
  Dormant)	
  
– @Nonnull:	
  may	
  never	
  be	
  null	
  
– @CheckForNull:	
  should	
  always	
  be	
  checked	
  before	
  
dereferenced	
  
– @Nullable:	
  caller	
  decides	
  if	
  null	
  check	
  is	
  needed	
  

• Java	
  8	
  will	
  add	
  more	
  annotation	
  options	
  for	
  tools
FindBugs	
  demo	
  in	
  Eclipse
Finding	
  API	
  problems	
  in	
  your	
  code
• People	
  
– Team	
  knowledge	
  
– Policy,	
  “best	
  practices”,	
  conventions	
  
– Elaborate	
  explanation,	
  “hard	
  to	
  do”	
  
– Defect	
  lists/exceptions/stack	
  traces	
  

• Tools	
  
– Finding	
  most	
  used	
  API:	
  Dependency	
  Structure	
  
Matrix	
  (DSM)
How	
  a	
  Dependency	
  Structure	
  Matrix	
  works
Classes/Packages

Document
Document

2

TextBlock

-­‐

Page

Page

TextBlock

-­‐
10

#	
  of	
  direct	
  dependencies	
  from	
  
Document	
  to	
  Page

-­‐
Demo:	
  SonarQube	
  DSM
API	
  design	
  cheat	
  sheet
API	
  matters:	
  
Reuse	
  3rd	
  party	
  
Modular	
  design	
  
Distance	
  to	
  impl.
A	
  Better	
  API	
  is:	
  
1. Valuable	
  	
  
2. Usable	
  	
  
3. Stable
API	
  usability:	
  
• Learnability	
  
• Efficiency	
  
• Memorability	
  
• Error	
  rate	
  
• Satisfaction

API	
  Design	
  Perspectives:	
  
UML:	
  names,	
  completeness,	
  grouping	
  
Implementation:	
  can	
  it	
  be	
  built	
  
API	
  usage:	
  how	
  usable	
  is	
  it	
  
Examples	
  First
JSR	
  305/FindBugs:	
  
@Nonnull:	
  may	
  never	
  be	
  null	
  
@CheckForNull:	
  always	
  check	
  
@Nullable:	
  caller	
  decides	
  null	
  check
DSM:	
  finding	
  most	
  used	
  API
Document
Document

2

TextBlock

-­‐

Page

Page

TextBlock

-­‐
10

	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  @PeterHendriks80,	
  +PeterHendriks

-­‐

API	
  Design	
  practices:	
  
When	
  in	
  doubt	
  leave	
  it	
  out	
  
Limit	
  API	
  consumer	
  code	
  to	
  a	
  minimum	
  
• Extending	
  class/interface	
  
• Checked	
  Exception	
  
• Required	
  casting	
  
• Required	
  Generic	
  Type	
  
• Hard	
  coded	
  parameter	
  value	
  
• Required	
  if,	
  for,	
  while,	
  try	
  
• Required	
  synchronization/locking	
  

!

Be	
  predictable	
  and	
  obvious	
  
• Methods	
  calleable	
  in	
  any	
  order	
  
• Constructor	
  should	
  only	
  construct	
  
• Don’t	
  have	
  surprising	
  side	
  effects	
  
• Limit	
  null	
  as	
  input/output	
  
• Fail	
  fast,	
  fail	
  hard

Eval!

Mais conteúdo relacionado

Mais procurados

RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019Gebreigziabher Ab
 
Introduction to Map Reduce
Introduction to Map ReduceIntroduction to Map Reduce
Introduction to Map ReduceApache Apex
 
A guided SQL tour of bioinformatics databases
A guided SQL tour of bioinformatics databasesA guided SQL tour of bioinformatics databases
A guided SQL tour of bioinformatics databasesYannick Pouliot
 
Intro to modern cryptography
Intro to modern cryptographyIntro to modern cryptography
Intro to modern cryptographyzahid-mian
 
HMAC - HASH FUNCTION AND DIGITAL SIGNATURES
HMAC  - HASH FUNCTION AND DIGITAL SIGNATURESHMAC  - HASH FUNCTION AND DIGITAL SIGNATURES
HMAC - HASH FUNCTION AND DIGITAL SIGNATURESPACHIYAPPAN PACHIYAPPAS
 
2.7 normal forms cnf & problems
2.7 normal forms  cnf & problems2.7 normal forms  cnf & problems
2.7 normal forms cnf & problemsSampath Kumar S
 
Crucial decisions in designing a data warehouse
Crucial decisions in designing a data warehouseCrucial decisions in designing a data warehouse
Crucial decisions in designing a data warehouseManju Rajput
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databasesJames Serra
 
Database ,14 Parallel DBMS
Database ,14 Parallel DBMSDatabase ,14 Parallel DBMS
Database ,14 Parallel DBMSAli Usman
 
Mining Data Streams
Mining Data StreamsMining Data Streams
Mining Data StreamsSujaAldrin
 
Security services and mechanisms
Security services and mechanismsSecurity services and mechanisms
Security services and mechanismsRajapriya82
 
Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...
Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...
Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...Amazon Web Services
 

Mais procurados (20)

Context free grammar
Context free grammar Context free grammar
Context free grammar
 
RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019
 
Chap4
Chap4Chap4
Chap4
 
Data mining tasks
Data mining tasksData mining tasks
Data mining tasks
 
Dynamic Itemset Counting
Dynamic Itemset CountingDynamic Itemset Counting
Dynamic Itemset Counting
 
Introduction to Map Reduce
Introduction to Map ReduceIntroduction to Map Reduce
Introduction to Map Reduce
 
A guided SQL tour of bioinformatics databases
A guided SQL tour of bioinformatics databasesA guided SQL tour of bioinformatics databases
A guided SQL tour of bioinformatics databases
 
Intro to modern cryptography
Intro to modern cryptographyIntro to modern cryptography
Intro to modern cryptography
 
HMAC - HASH FUNCTION AND DIGITAL SIGNATURES
HMAC  - HASH FUNCTION AND DIGITAL SIGNATURESHMAC  - HASH FUNCTION AND DIGITAL SIGNATURES
HMAC - HASH FUNCTION AND DIGITAL SIGNATURES
 
7 data management design
7 data management design7 data management design
7 data management design
 
Ip security
Ip security Ip security
Ip security
 
2.7 normal forms cnf & problems
2.7 normal forms  cnf & problems2.7 normal forms  cnf & problems
2.7 normal forms cnf & problems
 
Crucial decisions in designing a data warehouse
Crucial decisions in designing a data warehouseCrucial decisions in designing a data warehouse
Crucial decisions in designing a data warehouse
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databases
 
Database ,14 Parallel DBMS
Database ,14 Parallel DBMSDatabase ,14 Parallel DBMS
Database ,14 Parallel DBMS
 
Server side scripting
Server side scriptingServer side scripting
Server side scripting
 
Web Security
Web SecurityWeb Security
Web Security
 
Mining Data Streams
Mining Data StreamsMining Data Streams
Mining Data Streams
 
Security services and mechanisms
Security services and mechanismsSecurity services and mechanisms
Security services and mechanisms
 
Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...
Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...
Using AWS Batch and AWS Step Functions to Design and Run High-Throughput Work...
 

Semelhante a Practices and tools for building better API (JFall 2013)

Practices and Tools for Building Better APIs
Practices and Tools for Building Better APIsPractices and Tools for Building Better APIs
Practices and Tools for Building Better APIsPeter Hendriks
 
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)Tom Johnson
 
Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017
Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017
Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017Amazon Web Services
 
Alfresco Development Framework Basic
Alfresco Development Framework BasicAlfresco Development Framework Basic
Alfresco Development Framework BasicMario Romano
 
Infinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterSachin G Kulkarni
 
Review Paper on Online Java Compiler
Review Paper on Online Java CompilerReview Paper on Online Java Compiler
Review Paper on Online Java CompilerIRJET Journal
 
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...Charles Beyer
 
Schema-First API Design
Schema-First API DesignSchema-First API Design
Schema-First API DesignYos Riady
 
API First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipelineAPI First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipelinePronovix
 
Managing the Continuous Delivery of Code to AWS Lambda
Managing the Continuous Delivery of Code to AWS LambdaManaging the Continuous Delivery of Code to AWS Lambda
Managing the Continuous Delivery of Code to AWS LambdaAmazon Web Services
 
Vinay Kumar [InfluxData] | InfluxDB API Overview | InfluxDays 2022
Vinay Kumar [InfluxData] | InfluxDB API Overview  | InfluxDays 2022Vinay Kumar [InfluxData] | InfluxDB API Overview  | InfluxDays 2022
Vinay Kumar [InfluxData] | InfluxDB API Overview | InfluxDays 2022InfluxData
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCTim Burks
 
How to build a JavaScript toolkit
How to build a JavaScript toolkitHow to build a JavaScript toolkit
How to build a JavaScript toolkitMichael Nelson
 
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Tom Johnson
 
Application Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless WorldApplication Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless WorldAmazon Web Services
 
2022 APIsecure_Securing APIs with Open Standards
2022 APIsecure_Securing APIs with Open Standards2022 APIsecure_Securing APIs with Open Standards
2022 APIsecure_Securing APIs with Open StandardsAPIsecure_ Official
 
API workshop by AWS and 3scale
API workshop by AWS and 3scaleAPI workshop by AWS and 3scale
API workshop by AWS and 3scale3scale
 
Application Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless WorldApplication Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless WorldAmazon Web Services
 

Semelhante a Practices and tools for building better API (JFall 2013) (20)

Practices and Tools for Building Better APIs
Practices and Tools for Building Better APIsPractices and Tools for Building Better APIs
Practices and Tools for Building Better APIs
 
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)
 
Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017
Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017
Building CI/CD Pipelines for Serverless Applications - SRV302 - re:Invent 2017
 
Alfresco Development Framework Basic
Alfresco Development Framework BasicAlfresco Development Framework Basic
Alfresco Development Framework Basic
 
Infinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in Android
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
 
Review Paper on Online Java Compiler
Review Paper on Online Java CompilerReview Paper on Online Java Compiler
Review Paper on Online Java Compiler
 
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
 
Schema-First API Design
Schema-First API DesignSchema-First API Design
Schema-First API Design
 
API First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipelineAPI First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipeline
 
Managing the Continuous Delivery of Code to AWS Lambda
Managing the Continuous Delivery of Code to AWS LambdaManaging the Continuous Delivery of Code to AWS Lambda
Managing the Continuous Delivery of Code to AWS Lambda
 
Vinay Kumar [InfluxData] | InfluxDB API Overview | InfluxDays 2022
Vinay Kumar [InfluxData] | InfluxDB API Overview  | InfluxDays 2022Vinay Kumar [InfluxData] | InfluxDB API Overview  | InfluxDays 2022
Vinay Kumar [InfluxData] | InfluxDB API Overview | InfluxDays 2022
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPC
 
How to build a JavaScript toolkit
How to build a JavaScript toolkitHow to build a JavaScript toolkit
How to build a JavaScript toolkit
 
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
 
How to design effective APIs
How to design effective APIsHow to design effective APIs
How to design effective APIs
 
Application Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless WorldApplication Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless World
 
2022 APIsecure_Securing APIs with Open Standards
2022 APIsecure_Securing APIs with Open Standards2022 APIsecure_Securing APIs with Open Standards
2022 APIsecure_Securing APIs with Open Standards
 
API workshop by AWS and 3scale
API workshop by AWS and 3scaleAPI workshop by AWS and 3scale
API workshop by AWS and 3scale
 
Application Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless WorldApplication Lifecycle Management in a Serverless World
Application Lifecycle Management in a Serverless World
 

Último

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Último (20)

DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

Practices and tools for building better API (JFall 2013)

  • 1. Practices  and  Tools  for  Building  Better  APIs                                @PeterHendriks80,  +PeterHendriks
  • 2. Objectives  of  this  talk • API  from  a  Java  app  developer  perspective   ! • • • • Why  API  is  important   What  makes  a  better  API   Practical  tips  and  tools   Measuring  your  existing  codebase
  • 3. Some  API  terminology • API  Consumer:  code  that  uses  API   • API:  the  interface   • API  Implementer:  code  that  implements  API API   Consumer API API   Implementer Data  Access  Object JDBC Database  Driver
  • 4. We  are  using  a  lot  of  API Bazillion  Open   Source  Web   Framework  API’s   Spring:  27  API’s Java  EE:  37  API’s Java  SE:  45  API’s Enterprise  Service   API’s Public  Service  API’s Cloud  API’s Middleware  API’s
  • 5. We  are  building  more  of  them,  too • API  is  key  to  a  succesful  module  design   – Essential  for  agile  teams  at  scale Customer   Front-­‐End Warehouse   Front-­‐End Order   Management Shared  code
  • 6. Distance  to  API  implementation  is  increasing • API  quality  increasing  important  when   implementation  becomes  a  black  box Same   team Different   team Open   source Binary   only Other   datacenter
  • 7. The  API  we  use  shapes  our  code • Most  code  we  write  calls  an  API   • Code  that  calls  an  API  is  shaped  to  fit  the  API   • Therefore,  APIs  determine  the  shape  of   application  code   – In  some  ways,  even  more  impactful  than  high  level   design,  test  driven  development  or  code   craftsmanship
  • 8. ! http://docs.oracle.com/javaee/1.4/tutorial/examples/jaxp/xslt/samples/ TransformationApp04.java   ! public  class  TransformationApp04  {          public  static  void  main(String[]  argv)  {                  if  (argv.length  !=  1)  {                          System.err.println("Usage:  java  TransformationApp  filename");                          System.exit(1);                  }   !                //  Create  the  sax  "parser".                  AddressBookReader02  saxReader  =  new  AddressBookReader02();   var  json_data  =  JSON.stringify(obj);                try  {                          File  f  =  new  File(argv[0]);   ! !                        //  Use  a  Transformer  for  output                          TransformerFactory  tFactory  =  TransformerFactory.newInstance();                          Transformer  transformer  =  tFactory.newTransformer();   !                        //  Use  the  parser  as  a  SAX  source  for  input                          FileReader  fr  =  new  FileReader(f);                          BufferedReader  br  =  new  BufferedReader(fr);                          InputSource  inputSource  =  new  InputSource(fr);                          SAXSource  source  =  new  SAXSource(saxReader,  inputSource);                          StreamResult  result  =  new  StreamResult(System.out);                          transformer.transform(source,  result);                  }  catch  (TransformerConfigurationException  tce)  {                          //  Error  generated  by  the  parser                          System.out.println("n**  Transformer  Factory  error");                          System.out.println("      "  +  tce.getMessage());   !
  • 9. What  makes  a  better  API
  • 10. API  Quality:  Value • Value  =  benefits  -­‐  costs   • Benefits   – Features   – Data   – ...   • Costs   – – – – License/pay  per  use   Performance/footprint   Security   …
  • 11. API  Quality:  Usability • API  Design  ==  Human  Interface  design   – Although  API  is  for  machine/machine  interaction,   APIs  need  to  be  understood  by  programmers  to   write  the  code   – Programmers  are  humans API  is  not  just  about  capability  and  structural   design,  but  also  about  human  understanding
  • 12. A  simple  model  for  API  Usability Generic  usability  model  by  Ben  Shneiderman: Learnability:  How  easy  is  it  to  accomplish  basic   tasks  the  first  time?   Efficiency:  Once  users  learned  the  design,  how   quickly  can  they  perform  tasks?   Memorability:  When  users  return  after  a  while,   how  easily  can  they  re-­‐establish  proficiency?   Errors:  How  many  errors  do  users  make,  how   severe  are  these  errors,  and  how  easily  can  they   recover  from  the  errors?   Satisfaction:  How  pleasant  is  it  to  use  the  design?
  • 13. API  Quality:  stability Able  to  enhance/grow  implementation  without   breaking  existing  consumer  code Google  AppEngine  release  notes:   Version  1.7.5  -­‐  February  13,  2013   The  Conversion  API,  which  was  decommissioned  last  release,  has  been   removed  from  the  SDK.  In  a  future  release,  the  API  will  be  removed  from  the   runtime  and  applications  that  attempt  to  use  it  may  stop  working.  Applications   in  production  that  import  the  library  should  be  fixed  as  soon  as  possible.   Version  1.8.4  -­‐  September  9,  2013   The  Conversion  API,  a  decommissioned  feature,  has  been  removed  from  the   SDK.  In  the  next  release,  the  API  will  be  removed  from  the  runtime  and  any   attempt  to  import  the  API  will  raise  an  exception.   Version  1.8.5  -­‐  September  25,  2013   The  Conversion  API,  a  decommissioned  feature,  has  been  removed  from  the   runtime.  Applications  in  production  that  import  the  API  should  be  fixed  as   soon  as  possible.
  • 14. API  Quality:  A  hierarchy  of  needs Stability  only  matters  when   API  is  valuable  and  usable   Stable Usable Valuable
  • 16. API  Design:  start  with  examples • Examples  most  important  in  API   documentation   – Concrete  use  case,  usability  check   • Make  examples  exemplary   – Example  code  will  be  copy-­‐pasted   • Create  a  cheat  sheet   – Improves  learnability   – Shows  design  issues
  • 17. API  Design:  multiple  perspectives Names,  Completeness,   Grouping UML: public  class  Document  {   ! !    private  List<Page>  pages  =  new  ArrayList<>();   Impl: Consumer:    public  Page  addPage()  {          Page  page  =  new  Page();          pages.add(page);          page.setPageNumber(pages.size());          return  page;      }            public  void  report()  {...}   } Page  page  =  document.addPage();   page.setContents("Once  upon  a  time..."); Can  it  be  built? Is  it  usable?
  • 18. API  practice:  Keep  it  simple • Try  to  stick  to  just  methods  and  constructors   – Factories,  Delegates,  etc.  not  always  necessary   ! • “When  in  doubt  leave  it  out”   – Less  classes,  methods   – Less  parameters,  return  values Josh  Bloch,  Java  API  guru
  • 19. API  practice:  avoid  verbose  constructs • Constructs  that  create  verbose  consumer  code   – Extending  class/implementing  interface   – Checked  exception   – Required  casting   – Required  generic  type   – Required  if,  for,  while,  try/finally   – Required  synchronization/locking   – Hard  coded  parameter  value
  • 20. Example:  doing  some  loggin’ • Demo:  different  styles  of  logging  API
  • 21. Why  Lambdas  are  so  much  better  than  inner   classes • Inner  classes,  or  extending  existing  classes   often  needed   – Resource  handling   – Looping/visiting  collections   – Event  handling   • Lambdas  provide  a  much  more  usable   construct   – Shorter,  safer,  better  footprint/performance
  • 22. Example:  Lambda  benefits • Demo:  Netbeans  example
  • 23. API  practice:  fail  fast,  fail  hard • Fail  immediately  on  wrong  input   • Limit  the  use  of  null  as  valid  input/output public  void  save(String  filename)  {      if  (filename  ==  null)  {          return;  //  bad  api:  error  is  ignored!      }      ...  //  save  happens  here   } Better public  void  save(String  filename)  {      Objects.requireNonNull(filename);      //  Throws  NPE  immediately      ...  //  save  happens  here   }
  • 24. API  Tool:  checking  for  null • Tool:  FindBugs,  a  static  code  analyzer   – Reports  errors  at  compile-­‐time   • Annotations  for  Software  Defect  Detection
 (JSR  305:  Dormant)   – @Nonnull:  may  never  be  null   – @CheckForNull:  should  always  be  checked  before   dereferenced   – @Nullable:  caller  decides  if  null  check  is  needed   • Java  8  will  add  more  annotation  options  for  tools
  • 25. FindBugs  demo  in  Eclipse
  • 26. Finding  API  problems  in  your  code • People   – Team  knowledge   – Policy,  “best  practices”,  conventions   – Elaborate  explanation,  “hard  to  do”   – Defect  lists/exceptions/stack  traces   • Tools   – Finding  most  used  API:  Dependency  Structure   Matrix  (DSM)
  • 27. How  a  Dependency  Structure  Matrix  works Classes/Packages Document Document 2 TextBlock -­‐ Page Page TextBlock -­‐ 10 #  of  direct  dependencies  from   Document  to  Page -­‐
  • 29. API  design  cheat  sheet API  matters:   Reuse  3rd  party   Modular  design   Distance  to  impl. A  Better  API  is:   1. Valuable     2. Usable     3. Stable API  usability:   • Learnability   • Efficiency   • Memorability   • Error  rate   • Satisfaction API  Design  Perspectives:   UML:  names,  completeness,  grouping   Implementation:  can  it  be  built   API  usage:  how  usable  is  it   Examples  First JSR  305/FindBugs:   @Nonnull:  may  never  be  null   @CheckForNull:  always  check   @Nullable:  caller  decides  null  check DSM:  finding  most  used  API Document Document 2 TextBlock -­‐ Page Page TextBlock -­‐ 10                                @PeterHendriks80,  +PeterHendriks -­‐ API  Design  practices:   When  in  doubt  leave  it  out   Limit  API  consumer  code  to  a  minimum   • Extending  class/interface   • Checked  Exception   • Required  casting   • Required  Generic  Type   • Hard  coded  parameter  value   • Required  if,  for,  while,  try   • Required  synchronization/locking   ! Be  predictable  and  obvious   • Methods  calleable  in  any  order   • Constructor  should  only  construct   • Don’t  have  surprising  side  effects   • Limit  null  as  input/output   • Fail  fast,  fail  hard Eval!