Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

From programming languages to network protocols: lessons on API design

44 views

Published on

Looking at well-known projects and what they can teach us about API design.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

From programming languages to network protocols: lessons on API design

  1. 1. Lessons on API design Andrey Salomatin @flpvsk
  2. 2. API affects —What you can and can not do —How does it perform —How easy it is to make a mistake 2018-12-02 @flpvsk @growitconf 3
  3. 3. Why look at well-known projects? —To understand how the systems we use work —To learn techniques we can apply in our projects —To improve our thinking processes 2018-12-02 @flpvsk @growitconf 4
  4. 4. 1: A pixel is not a pixel2018-12-02 @flpvsk @growitconf 5
  5. 5. Poor abstractions limit what we can do using the API. 2018-12-02 @flpvsk @growitconf 6
  6. 6. 2018-12-02 @flpvsk @growitconf 7
  7. 7. x = ? 1. h1 / 2 2. (h1 - h2) / 2 3. (h1 + h2) / 2 2018-12-02 @flpvsk @growitconf 8
  8. 8. x = ? 2. (h1 - h2) / 2 2018-12-02 @flpvsk @growitconf 9
  9. 9. CSS .container { display: flex; align-items: center; } 2018-12-02 @flpvsk @growitconf 10
  10. 10. 2018-12-02 @flpvsk @growitconf 11
  11. 11. x = ? 1. h3 * round( (h1 - h2) / (2 * h3)) 2. h3 * (h1 - h2) / 2 3. h3 * floor( (h1 + h2) / 2) 2018-12-02 @flpvsk @growitconf 12
  12. 12. x = ? 1. h3 * round( (h1 - h2) / (2 * h3)) 2018-12-02 @flpvsk @growitconf 13
  13. 13. Center element within a grid —CSS? —iOS Auto Layout? —Android Layout? 2018-12-02 @flpvsk @growitconf 14
  14. 14. Vertical rhythm 2018-12-02 @flpvsk @growitconf 15
  15. 15. Testing layout & paint requires spinning off a simulator or a browser 2018-12-02 @flpvsk @growitconf 16
  16. 16. A pixel is not a pixel2018-12-02 @flpvsk @growitconf 17
  17. 17. Browser vendors' in the early 90s: —WWW is for documents —Each browser will decide how to best display pages to their users 2018-12-02 @flpvsk @growitconf 18
  18. 18. 19
  19. 19. Browser vendors' in the early 90s: —WWW is for documents —Each browser will decide how to display pages —Seems like publishers want styling 2018-12-02 @flpvsk @growitconf 20
  20. 20. …the style of a document couldn't be designed by either the author or the reader on their own… 2018-12-02 @flpvsk @growitconf 21
  21. 21. …their wishes have to be combined, or cascaded, in some way. 2018-12-02 @flpvsk @growitconf 22
  22. 22. 23
  23. 23. 2018-12-02 @flpvsk @growitconf 24
  24. 24. Flutter design principles —Layered API —Access to Layout and Paint phases —Full control over what is drawn on the screen 2018-12-02 @flpvsk @growitconf 25
  25. 25. Vertical rhythm in Flutter Offset getPositionForChild( Size containerSize, Size childSize ) 2018-12-02 @flpvsk @growitconf 26
  26. 26. Vertical rhythm in Flutter Offset getPositionForChild( Size containerSize, Size childSize ) { final h1 = containerSize.height; final h2 = childSize.height; final h3 = rowHeight; final rowsCount = ((h1 - h2) / (2 * h3)).round(); return Offset(0, rowsCount * h3); } 2018-12-02 @flpvsk @growitconf 27
  27. 27. Vertical rhythm in Flutter Offset getPositionForChild( Size containerSize, Size childSize ) { final h1 = containerSize.height; final h2 = childSize.height; final h3 = rowHeight; final rowsCount = ((h1 - h2) / (2 * h3)).round(); return Offset(0, rowsCount * h3); } 2018-12-02 @flpvsk @growitconf 27
  28. 28. Geometry x = h3 * round( (h1 - h2) / (2 * h3) ) 2018-12-02 @flpvsk @growitconf 28
  29. 29. 2018-12-02 @flpvsk @growitconf 29
  30. 30. Properly layered API gives freedom to build for custom usecases. 2018-12-02 @flpvsk @growitconf 30
  31. 31. 31
  32. 32. 32
  33. 33. 33
  34. 34. 34
  35. 35. 35
  36. 36. 36
  37. 37. 37
  38. 38. 38
  39. 39. 39
  40. 40. 40
  41. 41. 2: Simple or easy2018-12-02 @flpvsk @growitconf 41
  42. 42. Programming language designers balance cognitive load and capabilities. 2018-12-02 @flpvsk @growitconf 42
  43. 43. 43
  44. 44. Good abstractions help API users avoid mistakes. 2018-12-02 @flpvsk @growitconf 44
  45. 45. Working with concurrency 2018-12-02 @flpvsk @growitconf 45
  46. 46. 46
  47. 47. 2018-12-02 @flpvsk @growitconf 47
  48. 48. Concurrency basics in Java synchronized method() {} synchronized (object) {} Object.wait(); Object.notify(); Object.notifyAll(); 2018-12-02 @flpvsk @growitconf 48
  49. 49. Actual concurrency in Java —432 pages —18cm x 3cm x 23cm —635g 49
  50. 50. 50
  51. 51. Correctness is impossible to enforce. 2018-12-02 @flpvsk @growitconf 51
  52. 52. 2018-12-02 @flpvsk @growitconf 52
  53. 53. Concurrency basics in Rust 2018-12-02 @flpvsk @growitconf 53
  54. 54. Concurrency basics in Rust —New concept of Borrow Checker 2018-12-02 @flpvsk @growitconf 53
  55. 55. Concurrency basics in Rust —New concept of Borrow Checker —Thread safety enforced by the compiler 2018-12-02 @flpvsk @growitconf 53
  56. 56. Concurrency basics in Rust —New concept of Borrow Checker —Thread safety enforced by the compiler —Custom primitives with Send and Sync traits 2018-12-02 @flpvsk @growitconf 53
  57. 57. Expressive API requires more effort from the person adopting it. 2018-12-02 @flpvsk @growitconf 54
  58. 58. Choose what's important and make it hard to get wrong. 2018-12-02 @flpvsk @growitconf 55
  59. 59. Other examples —RPC (free for all) vs REST (unification) vs GraphQL (flexibility on the client) —Ethereum Solidity (ffa) vs Bamboo (understanding state & transitions) 2018-12-02 @flpvsk @growitconf 56
  60. 60. 3: Tree that scales2018-12-02 @flpvsk @growitconf 57
  61. 61. API is a performance bottleneck. 2018-12-02 @flpvsk @growitconf 58
  62. 62. 59
  63. 63. 60
  64. 64. 61
  65. 65. 62
  66. 66. 63
  67. 67. 64
  68. 68. 65
  69. 69. 66
  70. 70. 1h talk in Full-HD: 3.6GB Chunk size Chunks count Metadata size 2MB 1800 >36KB 1MB 3516 >70KB 512KB 7032 >140KB 2018-12-02 @flpvsk @growitconf 67
  71. 71. 10h conference in Full-HD: 36GB Chunk size Chunks count Metadata size 2MB 18000 >360KB 1MB 35160 >700KB 512KB 70320 >1.4MB 2018-12-02 @flpvsk @growitconf 68
  72. 72. 69
  73. 73. bittorent scaling issues —Size of metadata —Size of chunks 2018-12-02 @flpvsk @growitconf 70
  74. 74. Dat protocol Peer-to-peer versioned data sharing 2018-12-02 @flpvsk @growitconf 72
  75. 75. Dat protocol Decentralized open-source analog of Dropbox 2018-12-02 @flpvsk @growitconf 73
  76. 76. Dat protocol —Started as a way to exchange scientific data sets —Growing as the protocol for p2p web —Can be used to share TBs of dynamic data 2018-12-02 @flpvsk @growitconf 74
  77. 77. 75
  78. 78. 76
  79. 79. 77
  80. 80. 78
  81. 81. 79
  82. 82. 80
  83. 83. 81
  84. 84. 82
  85. 85. 83
  86. 86. 84
  87. 87. 85
  88. 88. 86
  89. 89. 87
  90. 90. Metadata size —Bittorent: O(n) —Dat: O(log(n)) n – number of chunks 2018-12-02 @flpvsk @growitconf 88
  91. 91. API is the ultimate performance bottleneck 2018-12-02 @flpvsk @growitconf 89
  92. 92. Other examples —HTTP/1 and HTTP/2 —Bitcoin core and Lightning network 2018-12-02 @flpvsk @growitconf 90
  93. 93. 4: The only const is change2018-12-02 @flpvsk @growitconf 91
  94. 94. Time and change are API-designers' worst nightmares. 2018-12-02 @flpvsk @growitconf 92
  95. 95. 93
  96. 96. REST API POST '/:personId/posts' PUT '/:personId/posts/:postId' GET '/:personId/posts' 2018-12-02 @flpvsk @growitconf 94
  97. 97. 95
  98. 98. 96
  99. 99. Creating new post ! --> POST { text: 'All frameworks have tradeoffs!' } '/bob/posts' " <-- { id: 'post-1' } 2018-12-02 @flpvsk @growitconf 97
  100. 100. 98
  101. 101. 99
  102. 102. Sharing post ! --> POST { share: 'post-1', text: 'YES!!' } '/alice/posts' " <-- { id: 'post-2' } 2018-12-02 @flpvsk @growitconf 100
  103. 103. 101
  104. 104. Updating post ! --> PUT { text: 'Serverless is a lie!' } '/bob/posts/post-1' " <-- { ok: true } 2018-12-02 @flpvsk @growitconf 102
  105. 105. 103
  106. 106. 105
  107. 107. REST assumptions 2018-12-02 @flpvsk @growitconf 106
  108. 108. REST assumptions —Everything is a resource 2018-12-02 @flpvsk @growitconf 106
  109. 109. REST assumptions —Everything is a resource —Resources have unique identifiers 2018-12-02 @flpvsk @growitconf 106
  110. 110. REST assumptions —Everything is a resource —Resources have unique identifiers —Resources can change over time* 2018-12-02 @flpvsk @growitconf 106
  111. 111. Alternative assumptions 2018-12-02 @flpvsk @growitconf 107
  112. 112. Alternative assumptions 2018-12-02 @flpvsk @growitconf 108
  113. 113. Alternative assumptions —Objects (facts) do not change over time 2018-12-02 @flpvsk @growitconf 108
  114. 114. Alternative assumptions —Objects (facts) do not change over time —References (labels) can change over time 2018-12-02 @flpvsk @growitconf 108
  115. 115. 109
  116. 116. 110
  117. 117. 111
  118. 118. 2018-12-02 @flpvsk @growitconf 112
  119. 119. 113
  120. 120. The concepts of time and change are hard to get right. 2018-12-02 @flpvsk @growitconf 114
  121. 121. Separate facts from labels, snapshots from references. 2018-12-02 @flpvsk @growitconf 115
  122. 122. Systems that get it right —CouchDB, Datomic —bittorent, Dat, IPFS —Blockchain-based projects —Event-sourcing-based projects 2018-12-02 @flpvsk @growitconf 116
  123. 123. Summary 2018-12-02 @flpvsk @growitconf 117
  124. 124. API design is hard. 2018-12-02 @flpvsk @growitconf 118
  125. 125. API affects —What you can and can not do —How does it perform —How easy it is to make a mistake 2018-12-02 @flpvsk @growitconf 119
  126. 126. Learn good desgin from systems you use. 2018-12-02 @flpvsk @growitconf 120
  127. 127. Listen to code podcast. 2018-12-02 @flpvsk @growitconf 121
  128. 128. THE END 2018-12-02 @flpvsk @growitconf 122

×