diff --git a/site/content/blog/2018-12-26-svelte-css-in-js.md b/site/content/blog/2018-12-26-svelte-css-in-js.md index dccfcb4a17..5e8e6ed71b 100644 --- a/site/content/blog/2018-12-26-svelte-css-in-js.md +++ b/site/content/blog/2018-12-26-svelte-css-in-js.md @@ -16,11 +16,13 @@ But Svelte's style handling does have some limitations. It's too difficult to sh Here, we're using [Emotion](https://emotion.sh) to generate scoped class names that can be used across multiple components: - +
+ +
It's important to note that most CSS-in-JS libraries have a runtime library, and many don't support statically extracting styles out into a separate .css file at build time (which is essential for the best performance). You should therefore only use CSS-in-JS if it's necessary for your application! diff --git a/site/content/blog/2019-04-20-write-less-code.md b/site/content/blog/2019-04-20-write-less-code.md new file mode 100644 index 0000000000..226909259c --- /dev/null +++ b/site/content/blog/2019-04-20-write-less-code.md @@ -0,0 +1,165 @@ +--- +title: Write less code +description: The most important metric you're not paying attention to +author: Rich Harris +authorURL: https://twitter.com/Rich_Harris +draft: true +--- + +All code is buggy. It stands to reason, therefore, that the more code you have to write the buggier your apps will be. + +Writing more code also takes more time, leaving less time for other things like optimisation, nice-to-have features, or being outdoors instead of hunched over a laptop. + +In fact it's widely acknowledged that [project development time](https://blog.codinghorror.com/diseconomies-of-scale-and-lines-of-code/) and [bug count](https://www.mayerdan.com/ruby/2012/11/11/bugs-per-line-of-code-ratio) grow *quadratically*, not linearly, with the size of a codebase. That tracks with our intuitions: a ten-line pull request will get a level of scrutiny rarely applied to a 100-line one. And once a given module becomes too big to fit on a single screen, the cognitive effort required to understand it increases significantly. We compensate by refactoring and adding comments — activities that almost always result in *more* code. It's a vicious cycle. + +Yet while we obsess — rightly! — over performance numbers, bundle size and anything else we can measure, we rarely pay attention to the amount of code we're writing. + + +## Readability is important + +I'm certainly not claiming that we should use clever tricks to scrunch our code into the most compact form possible at the expense of readability. Nor am I claiming that reducing *lines* of code is necessarily a worthwhile goal, since it encourages turning readable code like this... + +```js +for (let i = 0; i <= 100; i += 1) { + if (i % 2 === 0) { + console.log(`${i} is even`); + } +} +``` + +...into something much harder to parse: + +```js +for (let i = 0; i <= 100; i += 1) if (i % 2 === 0) console.log(`${i} is even`); +``` + +Instead, I'm claiming that we should favour languages and patterns that allow us to naturally write less code. + + +## Yes, I'm talking about Svelte + +Reducing the amount of code you have to write is an explicit goal of Svelte. To illustrate, let's look at a very simple component implemented in React, Vue and Svelte. First, the Svelte version: + +
+ +
+ +How would we build this in React? It would probably look something like this: + +```js +import React, { useState } from 'react'; + +export default () => { + const [a, setA] = useState(1); + const [b, setB] = useState(2); + + function handleInputA(event) { + setA(+event.target.value); + } + + function handleInputB(event) { + setB(+event.target.value); + } + + return ( +
+ + + +

{a} + {b} = {a + b}

+
+ ); +}; +``` + +Here's an equivalent component in Vue: + +```html + + + +``` + + + +In other words, it takes 442 characters in React, and 263 characters in Vue, to achieve something that takes 145 characters in Svelte. The React version is literally three times larger! + +It's unusual for the difference to be *quite* so obvious — in my experience, a React component is typically around 40% larger than its Svelte equivalent. Let's look at the features of Svelte's design that enable you to express ideas more concisely: + + +### Top-level elements + +In Svelte, a component can have as many top-level elements as you like. In React and Vue, a component must have a single top-level element — in React's case, trying to return two top-level elements from a component function would result in syntactically invalid code. (You can use a fragment — `<>` — instead of a `
`, but it's the same basic idea). + +In Vue, your markup must be wrapped in a `