A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.html 23KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!doctype html><!-- This is a valid HTML5 document. -->
  2. <!-- Screen readers, SEO, extensions and so on. -->
  3. <html lang="fr">
  4. <!-- Has to be within the first 1024 bytes, hence before the `title` element
  5. See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
  6. <meta charset="utf-8">
  7. <!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
  8. <!-- The viewport meta is quite crowded and we are responsible for that.
  9. See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
  10. <meta name="viewport" content="width=device-width,initial-scale=1">
  11. <!-- Required to make a valid HTML5 document. -->
  12. <title>Maintaining JavaScript applications in the long term (archive) — David Larlet</title>
  13. <meta name="description" content="Publication mise en cache pour en conserver une trace.">
  14. <!-- That good ol' feed, subscribe :). -->
  15. <link rel="alternate" type="application/atom+xml" title="Feed" href="/david/log/">
  16. <!-- Generated from https://realfavicongenerator.net/ such a mess. -->
  17. <link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons2/apple-touch-icon.png">
  18. <link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons2/favicon-32x32.png">
  19. <link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons2/favicon-16x16.png">
  20. <link rel="manifest" href="/static/david/icons2/site.webmanifest">
  21. <link rel="mask-icon" href="/static/david/icons2/safari-pinned-tab.svg" color="#07486c">
  22. <link rel="shortcut icon" href="/static/david/icons2/favicon.ico">
  23. <meta name="msapplication-TileColor" content="#f7f7f7">
  24. <meta name="msapplication-config" content="/static/david/icons2/browserconfig.xml">
  25. <meta name="theme-color" content="#f7f7f7" media="(prefers-color-scheme: light)">
  26. <meta name="theme-color" content="#272727" media="(prefers-color-scheme: dark)">
  27. <!-- Documented, feel free to shoot an email. -->
  28. <link rel="stylesheet" href="/static/david/css/style_2021-01-20.css">
  29. <!-- See https://www.zachleat.com/web/comprehensive-webfonts/ for the trade-off. -->
  30. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  31. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  32. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  33. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  34. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  35. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  36. <script>
  37. function toggleTheme(themeName) {
  38. document.documentElement.classList.toggle(
  39. 'forced-dark',
  40. themeName === 'dark'
  41. )
  42. document.documentElement.classList.toggle(
  43. 'forced-light',
  44. themeName === 'light'
  45. )
  46. }
  47. const selectedTheme = localStorage.getItem('theme')
  48. if (selectedTheme !== 'undefined') {
  49. toggleTheme(selectedTheme)
  50. }
  51. </script>
  52. <meta name="robots" content="noindex, nofollow">
  53. <meta content="origin-when-cross-origin" name="referrer">
  54. <!-- Canonical URL for SEO purposes -->
  55. <link rel="canonical" href="https://9elements.com/blog/maintaining-javascript-applications-in-the-long-term/">
  56. <body class="remarkdown h1-underline h2-underline h3-underline em-underscore hr-center ul-star pre-tick" data-instant-intensity="viewport-all">
  57. <article>
  58. <header>
  59. <h1>Maintaining JavaScript applications in the long term</h1>
  60. </header>
  61. <nav>
  62. <p class="center">
  63. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  64. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  65. </svg> Accueil</a> •
  66. <a href="https://9elements.com/blog/maintaining-javascript-applications-in-the-long-term/" title="Lien vers le contenu original">Source originale</a>
  67. </p>
  68. </nav>
  69. <hr>
  70. <p>In 2019, I wrote an article on <a href="https://9elements.com/blog/maintaining-large-javascript-projects/">maintaining large JavaScript applications</a>. As a follow-up, I’d like to describe a client project we are maintaining since 2014.</p>
  71. <h2 id="the-oecd-data-portal">The OECD Data Portal</h2>
  72. <figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://9elements.com/blog/content/images/2021/01/oecd-data-portal1.jpg" class="kg-image"><figcaption>Start page of the Data Portal</figcaption></figure>
  73. <p>The Organisation for Economic Co-operation and Development (OECD) is an intergovernmental body that collects data and publishes studies on behalf of its member states. The fields of work, amongst others, include economy, environmental issues, well-being or education.</p>
  74. <p>The <a href="https://data.oecd.org"><strong>OECD Data Portal</strong></a> is the central hub for statistical data. It helps researchers, journalists and policymakers to find meaningful data and to visualize it quickly with different charts. It connects with the <a href="https://www.oecd-ilibrary.org/">OECD iLibrary</a>, hosting the publications, and <a href="https://stats.oecd.org/">OECD.Stat</a>, storing the full data.</p>
  75. <p>The OECD is funded by its member states, so eventually by taxpayers like you and me. Using cost-effective, sustainable technologies was one of the requirements so the code can be maintained in the long term.</p>
  76. <p>The Data Portal is a joint work by OECD staff as well as external developers and designers. The initial design and prototyping came from <a href="https://truth-and-beauty.net">Moritz Stefaner</a> and <a href="https://raureif.net/">Raureif</a>. 9elements developed the production front-end code and still maintains it.</p>
  77. <h2 id="complex-javascript-codebase">Complex JavaScript codebase</h2>
  78. <p>The most complex part of the front-end is the JavaScript charting engine. It features ten main chart types with numerous configuration options. Using powerful interfaces, users can query the database and create charts for embedding or sharing.</p>
  79. <p>We started working on the Data Portal in 2014. Since then, there was no “big rewrite”, only new features, gradual improvements and refactoring. Recently, for the <a href="https://www.oecd.org/economic-outlook/">OECD Economic Outlook in December 2020</a>, we added several features, including four new chart types. Also we refactored the codebase significantly.</p>
  80. <p>In this article I’m going to describe how we maintained the code for so long and how we improved the code step by step. Also I will point out things that did not work well.</p>
  81. <h2 id="boring-mainstream-technology">Boring mainstream technology</h2>
  82. <p>When the project started in 2014, we chose plain HTML, XSLT templates, Sass for stylesheets and CoffeeScript as compile-to-JavaScript language. As JavaScript libraries, we chose jQuery, D3, D3.chart as well as Backbone.</p>
  83. <p>Back in 2014, these technologies were the safest, most compatible available. Only CoffeeScript was kind of a venture. We chose CoffeeScript because it made us more productive and helped us to write reliable code. But we were aware that it poses a liability.</p>
  84. <p>From 2015 on, 9elements has been using React for most JavaScript web applications. We considered to migrate the Data Portal chart engine to React, but we could not find the right moment. And in the end, it was probably a good decision to stick with the original stack.</p>
  85. <p>From today, the described JavaScript stack might seem outdated. But in fact the codebase stood the test of time. One reason is that the technologies we chose have aged well.</p>
  86. <h2 id="the-ravages-of-time">The ravages of time</h2>
  87. <p>While plenty of JavaScript libraries appeared and vanished, jQuery remains the most popular JavaScript library. It is robust, well-maintained and widely deployed. According to the <a href="https://almanac.httparchive.org/en/2020/javascript#libraries">Web Almanac 2020</a>, jQuery is used on 83% of all web sites. (For comparison, React was detected on 4% of all web sites.)</p>
  88. <p>Without doubt, jQuery has lost its dominance for non-trivial DOM scripting tasks. As mentioned, we would choose React or Preact for a project like the Data Portal today.</p>
  89. <p>The second library, <a href="https://d3js.org/">D3</a>, remains the industry standard when it comes to data visualization in the browser. It exists since 2010 and is still leading. While several major releases changed the structure and the API significantly, it is still an outstanding work of engineering.</p>
  90. <p>The <a href="https://backbonejs.org/">Backbone</a> library is not as popular, but has other qualities. Backbone is a relatively simple library. You can read the source code in one morning and could re-implement the core parts yourself in one afternoon. Backbone is still maintained, but more importantly it is feature-complete.</p>
  91. <p>From today’s perspective, only CoffeeScript poses a significant technical debt. CoffeeScript was developed because of blatant deficits in ECMAScript 5. Later, many ideas from CoffeeScript were incorporated into the ECMAScript 6 (2015) and ECMAScript 7 (2016) standards. Since then, there is no compelling reason to use CoffeeScript any longer.</p>
  92. <p>We picked CoffeeScript in 2014 because its philosophy is “it’s just JavaScript”. In contrast to other languages that compile to JavaScript, CoffeeScript is a straight-forward abstraction. CoffeeScript code compiles to clean JavaScript code without surprises.</p>
  93. <p>Today, most companies have migrated their CoffeeScript codebases to modern JavaScript. That’s what we did as well.</p>
  94. <h2 id="from-coffeescript-to-typescript">From CoffeeScript to TypeScript</h2>
  95. <p>Using the <a href="https://github.com/decaffeinate/decaffeinate">decaffeinate</a> tool, we converted the CoffeeScript code to ECMAScript 6 (2015). We still wanted to support the same browsers, so we now use the Babel compiler to produce backwards-compatible ECMAScript 5.</p>
  96. <p>All in all, this migration went smoothly. But we did not want to stop there.</p>
  97. <p>In new projects, 9elements is using TypeScript. In my opinion, TypeScript is best thing that happened in the JavaScript world in the last couple of years. As I mentioned in my previous article, <a href="https://9elements.com/blog/maintaining-large-javascript-projects/#avoid-creating-untyped-objects">TypeScript forces you to think about your types</a> and name them properly.</p>
  98. <p>For the Data Portal, we wanted to have the development benefits of TypeScript without converting the codebase to fully typed TypeScript.</p>
  99. <p>TypeScript is a superset of JavaScript. The compiler understands .js files pretty well. So we gradually added type annotations with a 20-year-old technology: <a href="https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html">JSDOC</a>. In addition, we wrote few typings in .ts files in order to reference them in the JSDOC annotations.</p>
  100. <p>This way, the developing experience in Visual Studio Code improved greatly with little effort. While there is no strict type checking, code editing feels as good as in an average TypeScript project.</p>
  101. <p>By combining a rather boring but rock-solid technology with the latest TypeScript compiler, we could add new features and refactor the code safely and easily.</p>
  102. <p>On the surface, coding is a conversation between you and the computer: You tell the computer what it should do.</p>
  103. <p>But more importantly, coding is a conversation between you and the reader of the code. It is a well-known fact that code is written once but read again and again. First and foremost, the reader is your future self.</p>
  104. <p>The Data Portal codebase contains many comments and almost all proved valuable during the last six years. Obviously, code should be structured to help human readers understanding it. But I do not believe in “self-descriptive” or “self-documenting” code.</p>
  105. <p>Before we switched to JSDOC, we had human-readable type annotations, documented function parameters and return values. Also we documented the main data types, complex nested object structures.</p>
  106. <p>These human-readable comments proved to be really helpful six years later. We translated them into machine-readable JSDOC and type declarations for the TypeScript compiler.</p>
  107. <h2 id="things-will-break-have-a-test-suite">Things will break – have a test suite</h2>
  108. <p>The project has only a few automated unit tests, but more than 50 test pages that demonstrate all Data Portal pages, components, data query interfaces, chart types and chart configuration options. They test against live or staging data, but also against fabricated data.</p>
  109. <p>These test pages serve the same purpose as automated tests: If we fix a bug, we add the scenario to the corresponding test page first. If we develop a new feature, we create a comprehensive test page simultaneously.</p>
  110. <figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://9elements.com/blog/content/images/2021/01/oecd-data-portal4.jpg" class="kg-image"><figcaption>Test page for the line chart responsiveness</figcaption></figure>
  111. <p>Before a release, we check all test pages manually and compare them to the last release – both visually and functionally. This is time-consuming, but it lets us find regressions quickly.</p>
  112. <p>I don’t think an automated test suite would serve us better. It is almost impossible to test interactive data visualizations in the browser in an automated way. Visual regression testing is a valuable tool in general, but would produce too many false positives in our case.</p>
  113. <h2 id="backward-and-forward-compatibility">Backward and forward compatibility</h2>
  114. <p>In 2014, the Data Portal had to work with Internet Explorer 9. Today, Internet Explorer has no importance when you develop a dynamic, in-browser charting engine.</p>
  115. <p>We decided to keep the compatibility with old browsers. The Data Portal is an international platform, so users visit from all over the world. They do not have the latest hardware and newest browsers.</p>
  116. <figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://9elements.com/blog/content/images/2021/01/oecd-data-portal5-ie9.jpg" class="kg-image"><figcaption>The Data Portal in Internet Explorer 9</figcaption></figure>
  117. <p>We accomplished to maintain the browser support baseline by using boring standard technologies. We do use several modern web features. But we apply <a href="https://en.wikipedia.org/wiki/Progressive_enhancement">Progressive Enhancement</a> to activate them only if the browser supports them. Also we use Babel and polyfills to make the modern JavaScript features work in old browsers.</p>
  118. <h2 id="your-abstractions-will-bite-you">Your abstractions will bite you</h2>
  119. <p>The technology stack was not the limit we faced in this project over the years. It was rather the abstractions we created our own that got in our way.</p>
  120. <p>We divided the user interface into views and created a base class similar to Backbone.View. (Today, all big JavaScript libraries use the term “component” instead of “view” for parts of the UI.) For holding the data and the state, we used Backbone.Model. This worked quite well, but we should have stuck to our own best practices.</p>
  121. <p>The idea of Backbone’s model-view separation is to have the model as the single source of truth. The DOM should merely reflect the model data. All changes should originate from the model. Modern frameworks like React, Vue and Angular enforce the convention that the UI is “a function of the state”, meaning the UI is derived from the state deterministically.</p>
  122. <p>We violated this principle and sometimes made the DOM the source of truth. This led to confusion with code that treated the model as authoritative.</p>
  123. <h2 id="object-oriented-charts">Object-oriented charts</h2>
  124. <p>For the charts, we chose yet another approach. We created chart classes not based on the view class described above.</p>
  125. <p>D3 itself is functional. A chart is typically created and updated with a huge <code>render</code> function that calls other functions. The chart data is the input for this large function. More state is held in specific objects.</p>
  126. <p>This makes D3 tremendously expressive and flexible. But D3 code is hard to read since there are little conventions on the structure of a chart.</p>
  127. <p>Folks at Bocoup, Irene Ros and Mike Pennisi, invented <a>d3.chart</a>, a small library on top of D3 that introduced class-based OOP. Its main goal was to structure and reuse charting code. These charts are made of layers. A layer renders and updates a specific part of the DOM using D3. Charts can have other charts attached.</p>
  128. <p>A general rule of OOP is “favor composition over inheritance”. Unfortunately, we used a weird mix of composition <em>and</em> inheritance to mix chart behavior.</p>
  129. <p>We should have used functions or simple classes instead of complex class hierarchies. People still wrap D3 in class-based OOP today, but no class-based solution has prevailed against D3’s functional structure. </p>
  130. <h2 id="summary">Summary</h2>
  131. <p>Since we designed the Data Portal front-end architecture in 2014, powerful patterns for JavaScript-driven web interfaces have emerged.</p>
  132. <p>Instead of rendering string-based HTML templates and updating the DOM manually, UI components are declarative nowadays. You simply update the state and the framework updates the DOM accordingly. This unidirectional data flow eliminates a whole class of bugs.</p>
  133. <p>The technologies we picked in 2014 either stood the test of time or offered a clear migration path. You could say we were lucky, but together with the client, we also chose long-lasting technologies deliberately.</p>
  134. <p>At 9elements, we take pride in using cutting-edge technologies. This includes assessing experimental front-end technologies that probably are not maintained any longer in three to four years from now. Unfortunately, many open source JavaScript projects are technically groundbreaking yet prove to be unsustainable.</p>
  135. <p>For each client project, we seek the right balance between well-established, zero-risk technologies as well as innovative technologies that help us to deliver an outstanding product in time.</p>
  136. <p><a href="https://9elements.com/contact"><strong>We're open for business so feel free to contact us for your next project.</strong></a></p>
  137. <h2 id="acknowledgments">Acknowledgments</h2>
  138. <figure class="kg-card kg-image-card"><img src="https://9elements.com/blog/content/images/2020/12/maintaining-large-javascript-projects.svg" class="kg-image"></figure>
  139. <p>Thanks to <a href="https://dribbble.com/LittleSue">Susanne Nähler</a>, designer at 9elements, for creating the teaser illustration.</p>
  140. <p>Thanks to the kind folks at OECD for the great collaboration over the course of the last six years.</p>
  141. </article>
  142. <hr>
  143. <footer>
  144. <p>
  145. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  146. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  147. </svg> Accueil</a> •
  148. <a href="/david/log/" title="Accès au flux RSS"><svg class="icon icon-rss2">
  149. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-rss2"></use>
  150. </svg> Suivre</a> •
  151. <a href="http://larlet.com" title="Go to my English profile" data-instant><svg class="icon icon-user-tie">
  152. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-user-tie"></use>
  153. </svg> Pro</a> •
  154. <a href="mailto:david%40larlet.fr" title="Envoyer un courriel"><svg class="icon icon-mail">
  155. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-mail"></use>
  156. </svg> Email</a> •
  157. <abbr class="nowrap" title="Hébergeur : Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33184162340"><svg class="icon icon-hammer2">
  158. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-hammer2"></use>
  159. </svg> Légal</abbr>
  160. </p>
  161. <template id="theme-selector">
  162. <form>
  163. <fieldset>
  164. <legend><svg class="icon icon-brightness-contrast">
  165. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-brightness-contrast"></use>
  166. </svg> Thème</legend>
  167. <label>
  168. <input type="radio" value="auto" name="chosen-color-scheme" checked> Auto
  169. </label>
  170. <label>
  171. <input type="radio" value="dark" name="chosen-color-scheme"> Foncé
  172. </label>
  173. <label>
  174. <input type="radio" value="light" name="chosen-color-scheme"> Clair
  175. </label>
  176. </fieldset>
  177. </form>
  178. </template>
  179. </footer>
  180. <script src="/static/david/js/instantpage-5.1.0.min.js" type="module"></script>
  181. <script>
  182. function loadThemeForm(templateName) {
  183. const themeSelectorTemplate = document.querySelector(templateName)
  184. const form = themeSelectorTemplate.content.firstElementChild
  185. themeSelectorTemplate.replaceWith(form)
  186. form.addEventListener('change', (e) => {
  187. const chosenColorScheme = e.target.value
  188. localStorage.setItem('theme', chosenColorScheme)
  189. toggleTheme(chosenColorScheme)
  190. })
  191. const selectedTheme = localStorage.getItem('theme')
  192. if (selectedTheme && selectedTheme !== 'undefined') {
  193. form.querySelector(`[value="${selectedTheme}"]`).checked = true
  194. }
  195. }
  196. const prefersColorSchemeDark = '(prefers-color-scheme: dark)'
  197. window.addEventListener('load', () => {
  198. let hasDarkRules = false
  199. for (const styleSheet of Array.from(document.styleSheets)) {
  200. let mediaRules = []
  201. for (const cssRule of styleSheet.cssRules) {
  202. if (cssRule.type !== CSSRule.MEDIA_RULE) {
  203. continue
  204. }
  205. // WARNING: Safari does not have/supports `conditionText`.
  206. if (cssRule.conditionText) {
  207. if (cssRule.conditionText !== prefersColorSchemeDark) {
  208. continue
  209. }
  210. } else {
  211. if (cssRule.cssText.startsWith(prefersColorSchemeDark)) {
  212. continue
  213. }
  214. }
  215. mediaRules = mediaRules.concat(Array.from(cssRule.cssRules))
  216. }
  217. // WARNING: do not try to insert a Rule to a styleSheet you are
  218. // currently iterating on, otherwise the browser will be stuck
  219. // in a infinite loop…
  220. for (const mediaRule of mediaRules) {
  221. styleSheet.insertRule(mediaRule.cssText)
  222. hasDarkRules = true
  223. }
  224. }
  225. if (hasDarkRules) {
  226. loadThemeForm('#theme-selector')
  227. }
  228. })
  229. </script>
  230. </body>
  231. </html>