designing beautiful apis...designing beautiful apis 2 / 55 about me i tobias schlitt (toby) i degree...

127
Designing Beautiful APIs International PHP Conference 2012 Tobias Schlitt (@tobySen) October 16, 2012 Designing Beautiful APIs 1 / 55

Upload: others

Post on 28-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Designing Beautiful APIsInternational PHP Conference 2012

Tobias Schlitt (@tobySen)

October 16, 2012

Designing Beautiful APIs 1 / 55

Page 2: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Qafoopassion for software quality

Designing Beautiful APIs 2 / 55

Page 3: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Qafoopassion for software quality

Designing Beautiful APIs 2 / 55

Page 4: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Qafoopassion for software quality

Designing Beautiful APIs 2 / 55

Page 5: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Qafoopassion for software quality

Designing Beautiful APIs 2 / 55

Page 6: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Co-founder of

Qafoopassion for software quality

Designing Beautiful APIs 2 / 55

Page 7: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Co-founder of

Qafoopassion for software quality

Helping teams to createhigh quality web

applications.

Designing Beautiful APIs 2 / 55

Page 8: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

About Me

I Tobias Schlitt (Toby)I Degree in computer scienceI More than 10 years of

professional PHPI Open source enthusiastI Software Architecture,

Design, Testing . . .

Co-founder of

Qafoopassion for software quality

Helping teams to createhigh quality web

applications.

http://qafoo.com

Designing Beautiful APIs 2 / 55

Page 9: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Before we start . . .

I Please take a minute after the talk . . .I . . . to rate this talk . . .I . . . to make it better for . . .

I . . . OrganizersI . . . SpeakersI . . . You

I Just go to https://joind.in/7312I I’ll show the link later again :)

Designing Beautiful APIs 3 / 55

Page 10: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Before we start . . .

I Please take a minute after the talk . . .I . . . to rate this talk . . .I . . . to make it better for . . .

I . . . OrganizersI . . . SpeakersI . . . You

I Just go to https://joind.in/7312I I’ll show the link later again :)

Designing Beautiful APIs 3 / 55

Page 11: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Before we start . . .

I Please take a minute after the talk . . .I . . . to rate this talk . . .I . . . to make it better for . . .

I . . . OrganizersI . . . SpeakersI . . . You

I Just go to https://joind.in/7312I I’ll show the link later again :)

Designing Beautiful APIs 3 / 55

Page 12: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Before we start . . .

I Please take a minute after the talk . . .I . . . to rate this talk . . .I . . . to make it better for . . .

I . . . OrganizersI . . . SpeakersI . . . You

I Just go to https://joind.in/7312I I’ll show the link later again :)

Designing Beautiful APIs 3 / 55

Page 13: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Before we start . . .

I Please take a minute after the talk . . .I . . . to rate this talk . . .I . . . to make it better for . . .

I . . . OrganizersI . . . SpeakersI . . . You

I Just go to https://joind.in/7312I I’ll show the link later again :)

Designing Beautiful APIs 3 / 55

Page 14: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Before we start . . .

I Please take a minute after the talk . . .I . . . to rate this talk . . .I . . . to make it better for . . .

I . . . OrganizersI . . . SpeakersI . . . You

I Just go to https://joind.in/7312I I’ll show the link later again :)

Designing Beautiful APIs 3 / 55

Page 15: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast
Page 16: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Outline

On the beauty of APIs

Detecting APIs

Getting into code

Summary

Designing Beautiful APIs 5 / 55

Page 17: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

A beautiful API . . .

“A beautiful API enables you to realize evenunforseen features with elegant code.”

–Tobias Schlitt

Designing Beautiful APIs 6 / 55

Page 18: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

“Realize”

“A beautiful API enables you to realize even unforseen featureswith elegant code.”I It enables you to solve a taskI Helps you to reach your goal with less effortI Supports you in generating business value

Designing Beautiful APIs 7 / 55

Page 19: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

“Unforseen”

“A beautiful API enables you to realize even unforseen featureswith elegant code.”I It keeps you flexible to

I add new featuresI react to changing requirements

I Is open for adjustment and extension

Designing Beautiful APIs 8 / 55

Page 20: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

“Elegant Code”

“A beautiful API enables you to realize even unforseen featureswith elegant code.”I As few code as possibleI As much code as necessaryI Easy to read and understandI Simple and testable

Designing Beautiful APIs 9 / 55

Page 21: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Outline

On the beauty of APIs

Detecting APIs

Getting into code

Summary

Designing Beautiful APIs 10 / 55

Page 22: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

So, where . . .

Where do you find APIs?

Designing Beautiful APIs 11 / 55

Page 23: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Web APIs

I REST (or what calls itsel)I (XML)-RPCI SOAPI . . . and friends.I Not covered here . . .I . . . but principles apply in a similar way

Designing Beautiful APIs 12 / 55

Page 24: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Web APIs

I REST (or what calls itsel)I (XML)-RPCI SOAPI . . . and friends.I Not covered here . . .I . . . but principles apply in a similar way

Designing Beautiful APIs 12 / 55

Page 25: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Web APIs

I REST (or what calls itsel)I (XML)-RPCI SOAPI . . . and friends.I Not covered here . . .I . . . but principles apply in a similar way

Designing Beautiful APIs 12 / 55

Page 26: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Code APIs

I Libraries / frameworksI Layer boundaries / components / modulesI Interfaces (and abstract classes!)I . . . every fu**ing class?!

Designing Beautiful APIs 13 / 55

Page 27: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Code APIs

I Libraries / frameworksI Layer boundaries / components / modulesI Interfaces (and abstract classes!)I . . . every fu**ing class?!

Designing Beautiful APIs 13 / 55

Page 28: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Code APIs

I Libraries / frameworksI Layer boundaries / components / modulesI Interfaces (and abstract classes!)I . . . every fu**ing class?!

Designing Beautiful APIs 13 / 55

Page 29: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Code APIs

I Libraries / frameworksI Layer boundaries / components / modulesI Interfaces (and abstract classes!)I . . . every fu**ing class?!

Designing Beautiful APIs 13 / 55

Page 30: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Levels of Beauty

I Code beauty (of the API)I OOP / OODI Consistent, good looking codeI Tests

I Syntactic / semantical beautyI Code structureI Naming & meaning

I DocumentationI API docsI Examples & TutorialsI User generated docs (comments)I Not further examined here

Designing Beautiful APIs 14 / 55

Page 31: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Levels of Beauty

I Code beauty (of the API)I OOP / OODI Consistent, good looking codeI Tests

I Syntactic / semantical beautyI Code structureI Naming & meaning

I DocumentationI API docsI Examples & TutorialsI User generated docs (comments)I Not further examined here

Designing Beautiful APIs 14 / 55

Page 32: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Levels of Beauty

I Code beauty (of the API)I OOP / OODI Consistent, good looking codeI Tests

I Syntactic / semantical beautyI Code structureI Naming & meaning

I DocumentationI API docsI Examples & TutorialsI User generated docs (comments)I Not further examined here

Designing Beautiful APIs 14 / 55

Page 33: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Outline

On the beauty of APIs

Detecting APIs

Getting into code

Summary

Designing Beautiful APIs 15 / 55

Page 34: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

The Example

I Search componentI Index / search for arbitrary objectsI Apply filters to results

Designing Beautiful APIs 16 / 55

Page 35: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 1: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $product = new Product ( ’ Glow Stone Driveway ’ ) ;8 Serv ice : : get ( )−>add ( $product ) ;9

10 / / Search11

12 $products = Search : : get ( )−> f e t ch (13 ’ type = ” Product ” AND name LIKE ”%stone%” ’14 ) ;

Designing Beautiful APIs 17 / 55

Page 36: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 1: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $product = new Product ( ’ Glow Stone Driveway ’ ) ;8 Serv ice : : get ( )−>add ( $product ) ;9

10 / / Search11

12 $products = Search : : get ( )−> f e t ch (13 ’ type = ” Product ” AND name LIKE ”%stone%” ’14 ) ;

Designing Beautiful APIs 17 / 55

Page 37: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 1: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $product = new Product ( ’ Glow Stone Driveway ’ ) ;8 Serv ice : : get ( )−>add ( $product ) ;9

10 / / Search11

12 $products = Search : : get ( )−> f e t ch (13 ’ type = ” Product ” AND name LIKE ”%stone%” ’14 ) ;

Designing Beautiful APIs 17 / 55

Page 38: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 1: API

1 namespace qafoo \Search ;2 class Serv ice3 {

4 public s t a t i c function get ( )5 { }

6 public function f e t ch ( $search , ar ray $conf ig = ar ray ( ) )7 { }

8 public function suggest ( $p re f i x , a r ray $ f i e l d s )9 { }

10 public function add ( $ob jec t )11 { }

12 }

Designing Beautiful APIs 18 / 55

Page 39: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 1: Issues

I IndescriptiveI Ambiguous

Designing Beautiful APIs 19 / 55

Page 40: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 1: Issues

I IndescriptiveI Ambiguous

Designing Beautiful APIs 19 / 55

Page 41: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Find descriptive namesI Know your audienceI Start with usage examples!

Designing Beautiful APIs 20 / 55

Page 42: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Find descriptive namesI Know your audienceI Start with usage examples!

Designing Beautiful APIs 20 / 55

Page 43: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Find descriptive namesI Know your audienceI Start with usage examples!

Designing Beautiful APIs 20 / 55

Page 44: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $product = new Product ( ’ Glow Stone Driveway ’ ) ;8 Search : : ge t Ins tance ( )−> index ( $product ) ;9

10 / / Search11

12 $products = Search : : ge t Ins tance ( )−> f i n d (13 ’ type = ” Product ” AND name LIKE ”%stone%” ’14 ) ;

Designing Beautiful APIs 21 / 55

Page 45: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $product = new Product ( ’ Glow Stone Driveway ’ ) ;8 Search : : ge t Ins tance ( )−> index ( $product ) ;9

10 / / Search11

12 $products = Search : : ge t Ins tance ( )−> f i n d (13 ’ type = ” Product ” AND name LIKE ”%stone%” ’14 ) ;

Designing Beautiful APIs 21 / 55

Page 46: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $product = new Product ( ’ Glow Stone Driveway ’ ) ;8 Search : : ge t Ins tance ( )−> index ( $product ) ;9

10 / / Search11

12 $products = Search : : ge t Ins tance ( )−> f i n d (13 ’ type = ” Product ” AND name LIKE ”%stone%” ’14 ) ;

Designing Beautiful APIs 21 / 55

Page 47: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: API

1 namespace qafoo \Search ;2 class Search3 {

4 public s t a t i c function get Ins tance ( )5 { }

6 public function f i n d ( $searchQuery , a r ray $conf ig = ar ray ( ) )7 { }

8 public function suggest ( $p re f i x , a r ray $ f i e l d s )9 { }

10 public function index ( $ob jec t )11 { }

12 }

Designing Beautiful APIs 22 / 55

Page 48: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: Issues

I Not configurableI Not easily mockableI STATIC DEPENDENCY

Designing Beautiful APIs 23 / 55

Page 49: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: Issues

I Not configurableI Not easily mockableI STATIC DEPENDENCY

Designing Beautiful APIs 23 / 55

Page 50: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 2: Issues

I Not configurableI Not easily mockableI STATIC DEPENDENCY

Designing Beautiful APIs 23 / 55

Page 51: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Don’t force to violate Law of Demeter (LoD)I Leave instancitation / configuration to the userI DON’T FORCE STATIC DEPENDENCIES

Designing Beautiful APIs 24 / 55

Page 52: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Don’t force to violate Law of Demeter (LoD)I Leave instancitation / configuration to the userI DON’T FORCE STATIC DEPENDENCIES

Designing Beautiful APIs 24 / 55

Page 53: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Don’t force to violate Law of Demeter (LoD)I Leave instancitation / configuration to the userI DON’T FORCE STATIC DEPENDENCIES

Designing Beautiful APIs 24 / 55

Page 54: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $search = new Search ( ) ;8 $product = new Product ( ’ Glow Stone Driveway ’ ) ;9 $search−> index ( $product ) ;

10

11 / / Search12

13 $search = new Search ( ) ;14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 25 / 55

Page 55: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $search = new Search ( ) ;8 $product = new Product ( ’ Glow Stone Driveway ’ ) ;9 $search−> index ( $product ) ;

10

11 / / Search12

13 $search = new Search ( ) ;14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 25 / 55

Page 56: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: Usage

1 <?php2

3 use qafoo \Search\Search ;4

5 / / Index6

7 $search = new Search ( ) ;8 $product = new Product ( ’ Glow Stone Driveway ’ ) ;9 $search−> index ( $product ) ;

10

11 / / Search12

13 $search = new Search ( ) ;14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 25 / 55

Page 57: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: API

1 namespace qafoo \Search ;2 class Search3 {

4 public function construct ( )5 { }

6 public function f i n d ( $searchQuery , a r ray $conf ig = ar ray ( ) )7 { }

8 public function suggest ( $p re f i x , a r ray $ f i e l d s )9 { }

10 public function index ( $ob jec t )11 { }

12 }

Designing Beautiful APIs 26 / 55

Page 58: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: Issues

I Search & index are different concernsI Not easily replaceableI Missing abstraction

Designing Beautiful APIs 27 / 55

Page 59: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: Issues

I Search & index are different concernsI Not easily replaceableI Missing abstraction

Designing Beautiful APIs 27 / 55

Page 60: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 3: Issues

I Search & index are different concernsI Not easily replaceableI Missing abstraction

Designing Beautiful APIs 27 / 55

Page 61: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Make implementations replaceableI Stay flexible for changing requirementsI See concerns on different levels

Designing Beautiful APIs 28 / 55

Page 62: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Make implementations replaceableI Stay flexible for changing requirementsI See concerns on different levels

Designing Beautiful APIs 28 / 55

Page 63: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Make implementations replaceableI Stay flexible for changing requirementsI See concerns on different levels

Designing Beautiful APIs 28 / 55

Page 64: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Usage

1 use qafoo \Search\Search ;2

3 / / Object cons t r uc t i on4 $search = new Search (5 new Backends\SolrSearchBackend ( /∗ . . . ∗ / ) ,6 new Backends\ZeroMqIndexBackend ( /∗ . . . ∗ / )7 ) ;8

9 / / Index10 $product = new Product ( ’ Glow Stone Driveway ’ ) ;11 $search−> index ( $product ) ;12

13 / / Search14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 29 / 55

Page 65: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Usage

1 use qafoo \Search\Search ;2

3 / / Object cons t r uc t i on4 $search = new Search (5 new Backends\SolrSearchBackend ( /∗ . . . ∗ / ) ,6 new Backends\ZeroMqIndexBackend ( /∗ . . . ∗ / )7 ) ;8

9 / / Index10 $product = new Product ( ’ Glow Stone Driveway ’ ) ;11 $search−> index ( $product ) ;12

13 / / Search14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 29 / 55

Page 66: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Usage

1 use qafoo \Search\Search ;2

3 / / Object cons t r uc t i on4 $search = new Search (5 new Backends\SolrSearchBackend ( /∗ . . . ∗ / ) ,6 new Backends\ZeroMqIndexBackend ( /∗ . . . ∗ / )7 ) ;8

9 / / Index10 $product = new Product ( ’ Glow Stone Driveway ’ ) ;11 $search−> index ( $product ) ;12

13 / / Search14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 29 / 55

Page 67: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Usage

1 use qafoo \Search\Search ;2

3 / / Object cons t r uc t i on4 $search = new Search (5 new Backends\SolrSearchBackend ( /∗ . . . ∗ / ) ,6 new Backends\ZeroMqIndexBackend ( /∗ . . . ∗ / )7 ) ;8

9 / / Index10 $product = new Product ( ’ Glow Stone Driveway ’ ) ;11 $search−> index ( $product ) ;12

13 / / Search14 $products = $search−> f i n d (15 ’ type = ” Product ” AND name LIKE ”%stone%” ’16 ) ;

Designing Beautiful APIs 29 / 55

Page 68: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: API

1 namespace qafoo \Search ;2 class Search3 {

4 public function construct ( SearchBackend $searchBackend ,IndexBackend $indexBackend )

5 { }

6 public function f i n d ( $searchQuery , a r ray $conf ig = ar ray ( ) )7 { }

8 public function suggest ( $p re f i x , a r ray $ f i e l d s )9 { }

10 public function index ( $ob jec t )11 { }

12 }

Designing Beautiful APIs 30 / 55

Page 69: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Issues

I Query parsing is a dedicated concernI Back ends use different syntaxI Writing strings in code sucks

Designing Beautiful APIs 31 / 55

Page 70: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Issues

I Query parsing is a dedicated concernI Back ends use different syntaxI Writing strings in code sucks

Designing Beautiful APIs 31 / 55

Page 71: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 4: Issues

I Query parsing is a dedicated concernI Back ends use different syntaxI Writing strings in code sucks

Designing Beautiful APIs 31 / 55

Page 72: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Separate concernsI Avoid code is strings

Designing Beautiful APIs 32 / 55

Page 73: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Separate concernsI Avoid code is strings

Designing Beautiful APIs 32 / 55

Page 74: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n ;3

4 / / Object cons t r uc t i on5 $search = new Search ( /∗ . . . ∗ / ) ;6

7 / / Search8 $products = $search−> f i n d (9 new C r i t e r i o n (

10 new C r i t e r i o n ( ’ type ’ , C r i t e r i o n : : EQUALS, ’ Product ’ ) ,11 C r i t e r i o n : : L AND ,12 new C r i t e r i o n ( ’name ’ , C r i t e r i o n : : LIKE , ’%stone%’ )13 )14 ) ;

Designing Beautiful APIs 33 / 55

Page 75: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n ;3

4 / / Object cons t r uc t i on5 $search = new Search ( /∗ . . . ∗ / ) ;6

7 / / Search8 $products = $search−> f i n d (9 new C r i t e r i o n (

10 new C r i t e r i o n ( ’ type ’ , C r i t e r i o n : : EQUALS, ’ Product ’ ) ,11 C r i t e r i o n : : L AND ,12 new C r i t e r i o n ( ’name ’ , C r i t e r i o n : : LIKE , ’%stone%’ )13 )14 ) ;

Designing Beautiful APIs 33 / 55

Page 76: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n ;3

4 / / Object cons t r uc t i on5 $search = new Search ( /∗ . . . ∗ / ) ;6

7 / / Search8 $products = $search−> f i n d (9 new C r i t e r i o n (

10 new C r i t e r i o n ( ’ type ’ , C r i t e r i o n : : EQUALS, ’ Product ’ ) ,11 C r i t e r i o n : : L AND ,12 new C r i t e r i o n ( ’name ’ , C r i t e r i o n : : LIKE , ’%stone%’ )13 )14 ) ;

Designing Beautiful APIs 33 / 55

Page 77: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n ;3

4 / / Object cons t r uc t i on5 $search = new Search ( /∗ . . . ∗ / ) ;6

7 / / Search8 $products = $search−> f i n d (9 new C r i t e r i o n (

10 new C r i t e r i o n ( ’ type ’ , C r i t e r i o n : : EQUALS, ’ Product ’ ) ,11 C r i t e r i o n : : L AND ,12 new C r i t e r i o n ( ’name ’ , C r i t e r i o n : : LIKE , ’%stone%’ )13 )14 ) ;

Designing Beautiful APIs 33 / 55

Page 78: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n ;3

4 / / Object cons t r uc t i on5 $search = new Search ( /∗ . . . ∗ / ) ;6

7 / / Search8 $products = $search−> f i n d (9 new C r i t e r i o n (

10 new C r i t e r i o n ( ’ type ’ , C r i t e r i o n : : EQUALS, ’ Product ’ ) ,11 C r i t e r i o n : : L AND ,12 new C r i t e r i o n ( ’name ’ , C r i t e r i o n : : LIKE , ’%stone%’ )13 )14 ) ;

Designing Beautiful APIs 33 / 55

Page 79: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n ;3

4 / / Object cons t r uc t i on5 $search = new Search ( /∗ . . . ∗ / ) ;6

7 / / Search8 $products = $search−> f i n d (9 new C r i t e r i o n (

10 new C r i t e r i o n ( ’ type ’ , C r i t e r i o n : : EQUALS, ’ Product ’ ) ,11 C r i t e r i o n : : L AND ,12 new C r i t e r i o n ( ’name ’ , C r i t e r i o n : : LIKE , ’%stone%’ )13 )14 ) ;

Designing Beautiful APIs 33 / 55

Page 80: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 class Search2 {

3 public function construct ( SearchBackend $searchBackend ,IndexBackend $indexBackend )

4 { }

5 public function f i n d ( C r i t e r i o n $ c r i t e r i o n , ar ray $conf ig =ar ray ( ) )

6 { }

7 public function suggest ( $p re f i x , a r ray $ f i e l d s )8 { }

9 public function index ( $ob jec t )10 { }

11 }

Designing Beautiful APIs 34 / 55

Page 81: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 namespace qafoo \Search ;2 class UserQueryParser3 {

4 public function parseQuery ( $querySt r ing )5 { }

6 }

Designing Beautiful APIs 35 / 55

Page 82: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 namespace qafoo \Search ;2 class UserQueryParser3 {

4 public function parseQuery ( $querySt r ing )5 { }

6 }

Designing Beautiful APIs 35 / 55

Page 83: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 namespace qafoo \Search ;2 class C r i t e r i o n3 {

4 const EQUALS = ’= ’ ;5 const LIKE = ’ LIKE ’ ;6 const L AND = ’&& ’ ;7 / / . . .8

9 public function construct ( $ f i r s t , $operator , $second )10 { }

11 }

Designing Beautiful APIs 36 / 55

Page 84: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 namespace qafoo \Search ;2 class C r i t e r i o n3 {

4 const EQUALS = ’= ’ ;5 const LIKE = ’ LIKE ’ ;6 const L AND = ’&& ’ ;7 / / . . .8

9 public function construct ( $ f i r s t , $operator , $second )10 { }

11 }

Designing Beautiful APIs 36 / 55

Page 85: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 namespace qafoo \Search ;2 class C r i t e r i o n3 {

4 const EQUALS = ’= ’ ;5 const LIKE = ’ LIKE ’ ;6 const L AND = ’&& ’ ;7 / / . . .8

9 public function construct ( $ f i r s t , $operator , $second )10 { }

11 }

Designing Beautiful APIs 36 / 55

Page 86: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: API

1 namespace qafoo \Search ;2 class C r i t e r i o n3 {

4 const EQUALS = ’= ’ ;5 const LIKE = ’ LIKE ’ ;6 const L AND = ’&& ’ ;7 / / . . .8

9 public function construct ( $ f i r s t , $operator , $second )10 { }

11 }

Designing Beautiful APIs 36 / 55

Page 87: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Issues

I Not extensible at allI Encourages to violate Liskov

Designing Beautiful APIs 37 / 55

Page 88: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 5: Issues

I Not extensible at allI Encourages to violate Liskov

Designing Beautiful APIs 37 / 55

Page 89: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Never use constants for processing instructionsI Put trees where trees belong . . .

Designing Beautiful APIs 38 / 55

Page 90: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Never use constants for processing instructionsI Put trees where trees belong . . .

Designing Beautiful APIs 38 / 55

Page 91: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n \LogicalAnd ;3 use qafoo \Search\ C r i t e r i o n \Equals ;4 use qafoo \Search\ C r i t e r i o n \ Like ;5

6 / / Object cons t r uc t i on7 $search = new Search ( /∗ . . . ∗ / ) ;8

9 / / Search10 $products = $search−> f i n d (11 new LogicalAnd (12 new Equals ( ’ type ’ , ’ Product ’ ) ,13 new Like ( ’name ’ , ’%stone%’ )14 )15 ) ;

Designing Beautiful APIs 39 / 55

Page 92: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n \LogicalAnd ;3 use qafoo \Search\ C r i t e r i o n \Equals ;4 use qafoo \Search\ C r i t e r i o n \ Like ;5

6 / / Object cons t r uc t i on7 $search = new Search ( /∗ . . . ∗ / ) ;8

9 / / Search10 $products = $search−> f i n d (11 new LogicalAnd (12 new Equals ( ’ type ’ , ’ Product ’ ) ,13 new Like ( ’name ’ , ’%stone%’ )14 )15 ) ;

Designing Beautiful APIs 39 / 55

Page 93: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n \LogicalAnd ;3 use qafoo \Search\ C r i t e r i o n \Equals ;4 use qafoo \Search\ C r i t e r i o n \ Like ;5

6 / / Object cons t r uc t i on7 $search = new Search ( /∗ . . . ∗ / ) ;8

9 / / Search10 $products = $search−> f i n d (11 new LogicalAnd (12 new Equals ( ’ type ’ , ’ Product ’ ) ,13 new Like ( ’name ’ , ’%stone%’ )14 )15 ) ;

Designing Beautiful APIs 39 / 55

Page 94: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\ C r i t e r i o n \LogicalAnd ;3 use qafoo \Search\ C r i t e r i o n \Equals ;4 use qafoo \Search\ C r i t e r i o n \ Like ;5

6 / / Object cons t r uc t i on7 $search = new Search ( /∗ . . . ∗ / ) ;8

9 / / Search10 $products = $search−> f i n d (11 new LogicalAnd (12 new Equals ( ’ type ’ , ’ Product ’ ) ,13 new Like ( ’name ’ , ’%stone%’ )14 )15 ) ;

Designing Beautiful APIs 39 / 55

Page 95: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: API

1 namespace qafoo \Search ;2 abstract class C r i t e r i o n3 {

4 }

1 namespace qafoo \Search\ C r i t e r i o n ;2 use qafoo \Search\ C r i t e r i o n ;3

4 class Equals extends C r i t e r i o n5 {

6 public function construct ( $fieldName , $value )7 { }

8 }

Designing Beautiful APIs 40 / 55

Page 96: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: API

1 namespace qafoo \Search ;2 abstract class C r i t e r i o n3 {

4 }

1 namespace qafoo \Search\ C r i t e r i o n ;2 use qafoo \Search\ C r i t e r i o n ;3

4 class Equals extends C r i t e r i o n5 {

6 public function construct ( $fieldName , $value )7 { }

8 }

Designing Beautiful APIs 40 / 55

Page 97: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: API

1 namespace qafoo \Search ;2 abstract class C r i t e r i o n3 {

4 }

1 namespace qafoo \Search\ C r i t e r i o n ;2 use qafoo \Search\ C r i t e r i o n ;3

4 class Equals extends C r i t e r i o n5 {

6 public function construct ( $fieldName , $value )7 { }

8 }

Designing Beautiful APIs 40 / 55

Page 98: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: API

1 namespace qafoo \Search ;2 abstract class C r i t e r i o n3 {

4 }

1 namespace qafoo \Search\ C r i t e r i o n ;2 use qafoo \Search\ C r i t e r i o n ;3

4 class LogicalAnd extends C r i t e r i o n5 {

6 public function construct ( C r i t e r i o n $ l e f t C r i t e r i o n ,C r i t e r i o n $ r i g h t C r i t e r i o n )

7 { }

8 }

Designing Beautiful APIs 41 / 55

Page 99: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: API

1 namespace qafoo \Search ;2 abstract class C r i t e r i o n3 {

4 }

1 namespace qafoo \Search\ C r i t e r i o n ;2 use qafoo \Search\ C r i t e r i o n ;3

4 class LogicalAnd extends C r i t e r i o n5 {

6 public function construct ( C r i t e r i o n $ l e f t C r i t e r i o n ,C r i t e r i o n $ r i g h t C r i t e r i o n )

7 { }

8 }

Designing Beautiful APIs 41 / 55

Page 100: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: API

1 namespace qafoo \Search ;2 abstract class C r i t e r i o n3 {

4 }

1 namespace qafoo \Search\ C r i t e r i o n ;2 use qafoo \Search\ C r i t e r i o n ;3

4 class LogicalAnd extends C r i t e r i o n5 {

6 public function construct ( C r i t e r i o n $ l e f t C r i t e r i o n ,C r i t e r i o n $ r i g h t C r i t e r i o n )

7 { }

8 }

Designing Beautiful APIs 41 / 55

Page 101: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast
Page 102: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Issues

1 use qafoo \Search\Search ;2

3 / / Object cons t r u c t i on4 $search = new Search ( /∗ . . . ∗ / ) ;5

6 / / Search7 $products = $search−> f i n d (8 new LogicalAnd ( /∗ . . . ∗ / ) ,9 ar ray (

10 ’ f i l t e r s ’ => ar ray (11 ’ user ’ => ’ john doe ’12 )13 )14 ) ;

Designing Beautiful APIs 43 / 55

Page 103: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Issues

I No auto completion for arrays / stringsI Almost impossible to extend

Designing Beautiful APIs 44 / 55

Page 104: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 6: Issues

I No auto completion for arrays / stringsI Almost impossible to extend

Designing Beautiful APIs 44 / 55

Page 105: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Remember inversion of controlI Provide hook-in mechanism where sensible

Designing Beautiful APIs 45 / 55

Page 106: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Remember inversion of controlI Provide hook-in mechanism where sensible

Designing Beautiful APIs 45 / 55

Page 107: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: Usage

1 use qafoo \Search\Search ;2

3 / / Object cons t r uc t i on4 $search = new Search ( /∗ . . . ∗ / ) ;5

6 / / Search7 $products = $search−> f i n d (8 new LogicalAnd ( /∗ . . . ∗ / ) ,9 new UserPerm iss ionF i l t e r ( ’ john doe ’ )

10 ) ;

Designing Beautiful APIs 46 / 55

Page 108: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: Usage

1 use qafoo \Search\Search ;2

3 / / Object cons t r uc t i on4 $search = new Search ( /∗ . . . ∗ / ) ;5

6 / / Search7 $products = $search−> f i n d (8 new LogicalAnd ( /∗ . . . ∗ / ) ,9 new UserPerm iss ionF i l t e r ( ’ john doe ’ )

10 ) ;

Designing Beautiful APIs 46 / 55

Page 109: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: API

1 namespace qafoo \Search ;2 abstract class F i l t e r3 {

4 abstract public function accepts ( Resul t $ r e s u l t ) ;5 }

Designing Beautiful APIs 47 / 55

Page 110: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: API

1 namespace qafoo \Search ;2 abstract class F i l t e r3 {

4 abstract public function accepts ( Resul t $ r e s u l t ) ;5 }

Designing Beautiful APIs 47 / 55

Page 111: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: Issues

I Results are created out of thin air?I Smells like . . .

I hidden dependenciesI black magic

Designing Beautiful APIs 48 / 55

Page 112: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: Issues

I Results are created out of thin air?I Smells like . . .

I hidden dependenciesI black magic

Designing Beautiful APIs 48 / 55

Page 113: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in Mind

I Object live cycle control

Designing Beautiful APIs 49 / 55

Page 114: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 8: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\Resul tFactory ;3

4 / / Object cons t r uc t i on5 $search = new Search (6 new Backends\SolrSearchBackend ( /∗ . . . ∗ / ) ,7 new Backends\ZeroMqIndexBackend ( /∗ . . . ∗ / ) ,8 new Resul tFactoy \Dispatch ingResu l tFac tory (9 /∗ . . . ∗ /

10 )11 ) ;12

13 / / Search14 $products = $search−> f i n d (15 new LogicalAnd ( /∗ . . . ∗ / ) ,16 new UserPerm iss ionF i l t e r ( ’ john doe ’ )17 ) ;Designing Beautiful APIs 50 / 55

Page 115: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 8: Usage

1 use qafoo \Search\Search ;2 use qafoo \Search\Resul tFactory ;3

4 / / Object cons t r uc t i on5 $search = new Search (6 new Backends\SolrSearchBackend ( /∗ . . . ∗ / ) ,7 new Backends\ZeroMqIndexBackend ( /∗ . . . ∗ / ) ,8 new Resul tFactoy \Dispatch ingResu l tFac tory (9 /∗ . . . ∗ /

10 )11 ) ;12

13 / / Search14 $products = $search−> f i n d (15 new LogicalAnd ( /∗ . . . ∗ / ) ,16 new UserPerm iss ionF i l t e r ( ’ john doe ’ )17 ) ;Designing Beautiful APIs 50 / 55

Page 116: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: API

1 namespace qafoo \Search ;2 abstract class Resul tFactory3 {

4 abstract public function canHandle ( $className ) ;5 abstract public function crea teResu l tOb jec t ( $className , ar ray

$data ) ;6 }

Designing Beautiful APIs 51 / 55

Page 117: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: API

1 namespace qafoo \Search ;2 abstract class Resul tFactory3 {

4 abstract public function canHandle ( $className ) ;5 abstract public function crea teResu l tOb jec t ( $className , ar ray

$data ) ;6 }

Designing Beautiful APIs 51 / 55

Page 118: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Step 7: API

1 namespace qafoo \Search ;2 abstract class Resul tFactory3 {

4 abstract public function canHandle ( $className ) ;5 abstract public function crea teResu l tOb jec t ( $className , ar ray

$data ) ;6 }

Designing Beautiful APIs 51 / 55

Page 119: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Outline

On the beauty of APIs

Detecting APIs

Getting into code

Summary

Designing Beautiful APIs 52 / 55

Page 120: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in mind (randomly)

I Descriptive namingI Know your audienceI Let the user create objectsI Never force static dependenciesI Separate concerns on different levelsI Make implementations replaceableI Remember inversion of controlI Avoid code in strings

Designing Beautiful APIs 53 / 55

Page 121: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in mind (randomly)

I Descriptive namingI Know your audienceI Let the user create objectsI Never force static dependenciesI Separate concerns on different levelsI Make implementations replaceableI Remember inversion of controlI Avoid code in strings

Designing Beautiful APIs 53 / 55

Page 122: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in mind (randomly)

I Descriptive namingI Know your audienceI Let the user create objectsI Never force static dependenciesI Separate concerns on different levelsI Make implementations replaceableI Remember inversion of controlI Avoid code in strings

Designing Beautiful APIs 53 / 55

Page 123: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in mind (randomly)

I Descriptive namingI Know your audienceI Let the user create objectsI Never force static dependenciesI Separate concerns on different levelsI Make implementations replaceableI Remember inversion of controlI Avoid code in strings

Designing Beautiful APIs 53 / 55

Page 124: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in mind (randomly)

I Descriptive namingI Know your audienceI Let the user create objectsI Never force static dependenciesI Separate concerns on different levelsI Make implementations replaceableI Remember inversion of controlI Avoid code in strings

Designing Beautiful APIs 53 / 55

Page 125: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Keep in mind (randomly)

I Descriptive namingI Know your audienceI Let the user create objectsI Never force static dependenciesI Separate concerns on different levelsI Make implementations replaceableI Remember inversion of controlI Avoid code in strings

Designing Beautiful APIs 53 / 55

Page 126: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Thanks for Listening!

Stay in touch

I TobyI @tobySenI [email protected] http://qafoo.com

Rate the talk!

https://joind.in/7312

Designing Beautiful APIs 54 / 55

Page 127: Designing Beautiful APIs...Designing Beautiful APIs 2 / 55 About Me I Tobias Schlitt (Toby) I Degree in computer science I More than 10 years of professional PHP I Open source enthusiast

Attribution

I Cat photo CC-NC-BY-SA buttersweet,https://secure.flickr.com/photos/buttersweet/433993378/

I Cat photo CC-NC-BY-SA rodolphoreis,https://secure.flickr.com/photos/rodolphoreis/5252680113/

Designing Beautiful APIs 55 / 55