<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>James Shakespeare - Posts</title><link>https://jshakespeare.com/posts/</link><description>Software engineer and consultant with >15 years experience. Specializing in product engineering, design systems, and full-stack web development with React and TypeScript.</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 14 May 2025 05:00:52 +0000</lastBuildDate><atom:link href="https://jshakespeare.com/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>Fix missing ‘Skip to Content’ link in WordPress block themes</title><link>https://jshakespeare.com/fix-missing-skip-to-content-link-wordpress-block-themes/</link><pubDate>Wed, 14 May 2025 05:00:52 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/fix-missing-skip-to-content-link-wordpress-block-themes/</guid><description>&lt;p>WordPress sites are steadily becoming more accessible out of the box with the increasing adoption of &lt;a href="https://fullsiteediting.com/lessons/what-is-full-site-editing/">Full Site Editing (FSE)&lt;/a>. This is great news for the web, as it reduces the responsibility of making sites accessible on individual developers and site owners, shifting more of that burden onto the platform itself.&lt;/p>
&lt;p>One a11y feature that is available by default in WordPress 5.8 and later is the automatic addition of ’Skip to content’ links, known as skip-links, to block theme templates. Skip-links help keyboard and screen reader users quickly navigate to the main content area of a page and avoid repetitive tabbing through header and navigation areas above it.&lt;/p>
&lt;p>You can test whether skip-links are working on your WordPress FSE site by loading a page and pressing &lt;code>Tab&lt;/code>. You should see a link appear in the top-left corner of the page saying &lt;strong>“Skip to content.”&lt;/strong> Pressing &lt;code>Enter&lt;/code> while this link is in focus should shift focus past the navigation and into the main content area.&lt;/p>
&lt;p>If you don’t see the skip-link, it’s likely that your template is missing a &lt;code>&amp;lt;main&amp;gt;&lt;/code> element.&lt;/p>
&lt;h2 id="how-wordpress-determines-whether-and-where-to-add-a-skip-link">How WordPress determines whether and where to add a skip-link&lt;/h2>
&lt;p>If you view the source of your page, you will see the following snippet, which is added by &lt;a href="https://github.com/WordPress/WordPress/blob/6.4/wp-includes/theme-templates.php#L109C10-L109C45">&lt;code>wp_enqueue_block_template_skip_link&lt;/code>&lt;/a>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">script&lt;/span> &lt;span class="na">id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;wp-block-template-skip-link-js-after&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">(&lt;/span> &lt;span class="kd">function&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kd">var&lt;/span> &lt;span class="nx">skipLinkTarget&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">querySelector&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="s1">&amp;#39;main&amp;#39;&lt;/span> &lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">sibling&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLinkTargetID&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLink&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Early exit if a skip-link target can&amp;#39;t be located.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="o">!&lt;/span> &lt;span class="nx">skipLinkTarget&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cm">/*
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> * Get the site wrapper.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> * The skip-link will be injected at the beginning of it.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> */&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">sibling&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">querySelector&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="s1">&amp;#39;.wp-site-blocks&amp;#39;&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Early exit if the root element was not found.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="o">!&lt;/span> &lt;span class="nx">sibling&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Get the skip-link target&amp;#39;s ID, and generate one if it doesn&amp;#39;t exist.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="nx">skipLinkTargetID&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">skipLinkTarget&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">id&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="o">!&lt;/span> &lt;span class="nx">skipLinkTargetID&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLinkTargetID&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;wp--skip-link--target&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLinkTarget&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">id&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">skipLinkTargetID&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Create the skip link.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="nx">skipLink&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">createElement&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="s1">&amp;#39;a&amp;#39;&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLink&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">classList&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">add&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="s1">&amp;#39;skip-link&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;screen-reader-text&amp;#39;&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLink&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">id&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;wp-skip-link&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLink&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;#&amp;#39;&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="nx">skipLinkTargetID&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">skipLink&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">innerText&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;Skip to content&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Inject the skip link.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="nx">sibling&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">parentElement&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">insertBefore&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">skipLink&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">sibling&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}()&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">script&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notice that if no main element is found (&lt;code>skipLinkTarget&lt;/code>), the script exits early, meaning no skip-link will be added.&lt;/p>
&lt;h2 id="how-to-add-a-main-element-in-the-site-editor">How to add a &lt;code>&amp;lt;main&amp;gt;&lt;/code> element in the Site Editor&lt;/h2>
&lt;ol>
&lt;li>Open the WordPress Editor and navigate to the relevant template (e.g. “Page” or “Single”).&lt;/li>
&lt;li>Locate or create a &lt;strong>Group&lt;/strong> block that wraps all of your primary content. Ideally, this Group should contain your page’s &lt;!-- raw HTML omitted --> heading as its first child.&lt;/li>
&lt;li>If no suitable Group exists, select all the relevant blocks and press Cmd + G (or Ctrl + G on Windows) to group them.&lt;/li>
&lt;li>With the Group block selected, open the &lt;strong>Block settings&lt;/strong> sidebar.&lt;/li>
&lt;li>Under the &lt;strong>Advanced&lt;/strong> section, find the &lt;strong>HTML element&lt;/strong> dropdown and select &lt;code>&amp;lt;main&amp;gt;&lt;/code>.&lt;/li>
&lt;li>Save the template.&lt;/li>
&lt;li>Refresh the page. You should now see the “Skip to content” link appear as the first item to receive focus when pressing Tab.&lt;/li>
&lt;li>Repeat this process for any other templates that are missing a skip-link.&lt;/li>
&lt;/ol></description></item><item><title>Making route changes accessible in React with an autofocusing h1</title><link>https://jshakespeare.com/accessible-route-change-react-router-autofocus-heading/</link><pubDate>Sat, 04 Nov 2023 09:20:52 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/accessible-route-change-react-router-autofocus-heading/</guid><description>&lt;p>Many routing packages for React don’t include any sort of focus management out of the box (including the most popular, React Router). This means that after a route change the browser&amp;rsquo;s focus will linger on the most recently focused element, even if that element is no longer in the DOM. This can be confusing for users who are navigating using a screen reader and/or their keyboard.&lt;/p>
&lt;p>If you’re starting a new React project that requires routing, you may want to consider using &lt;a href="https://reach.tech/router/">Reach Router&lt;/a>, which gives you focus management for free. From their &lt;a href="https://reach.tech/router/accessibility">docs&lt;/a>:&lt;/p>
&lt;blockquote>
&lt;p>Reach Router provides out-of-the-box focus management so your apps are significantly more accessible without you breaking a sweat.&lt;/p>
&lt;p>When the location changes, the top-most part of your application that changed is identified and focus is moved to it.&lt;/p>
&lt;/blockquote>
&lt;p>However, if you’re stuck with some other routing package then you will have to roll your own focus management. There are many ways to do this, but there is some &lt;a href="https://www.gatsbyjs.com/blog/2019-07-11-user-testing-accessible-client-routing/">consensus&lt;/a> that the best way to handle SPA route changes is to move focus to the new page’s heading, in other words the &lt;code>h1&lt;/code> tag.&lt;/p>
&lt;p>When we move focus to the h1 we do two things to help users:&lt;/p>
&lt;ol>
&lt;li>We move focus to the top of the new page&amp;rsquo;s content, ideally after any navigation or page chrome that may be redundant. We ensure that focus is not left lingering on some possibly irrelevant or likely non-existent DOM element from the previous page.&lt;/li>
&lt;li>We prompt screen readers to announce the new page&amp;rsquo;s title.&lt;/li>
&lt;/ol>
&lt;p>By default, &lt;code>h1&lt;/code> is not a focusable element, but we can make any HTML element focusable using the &lt;code>tabindex&lt;/code> attribute (in React, we must write this as &lt;code>tabIndex&lt;/code>).&lt;/p>
&lt;p>Then, to focus the element, we just need to add a one-line &lt;code>useEffect&lt;/code> hook that focuses the &lt;code>h1&lt;/code> on mount using a ref.&lt;/p>
&lt;p>Altogether, the component looks like this in TypeScript:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-tsx" data-lang="tsx">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="nx">React&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">useEffect&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">useRef&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="kr">from&lt;/span> &lt;span class="s1">&amp;#39;react&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm">/**
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> * A wrapper for h1 that focuses on mount. Useful as a page heading to
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> * reset focus after a route change. See
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> * https://jshakespeare.com/accessible-route-change-react-router-autofocus-heading/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cm"> */&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">export&lt;/span> &lt;span class="k">default&lt;/span> &lt;span class="kd">function&lt;/span> &lt;span class="nx">FocusablePageHeading&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">props&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">children&lt;/span>: &lt;span class="kt">React.ReactNode&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">})&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">el&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">useRef&lt;/span>&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">HTMLHeadingElement&lt;/span>&lt;span class="p">&amp;gt;(&lt;/span>&lt;span class="kc">null&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">useEffect&lt;/span>&lt;span class="p">(()&lt;/span> &lt;span class="o">=&amp;gt;&lt;/span> &lt;span class="nx">el&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">current&lt;/span>&lt;span class="o">?&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">focus&lt;/span>&lt;span class="p">(),&lt;/span> &lt;span class="p">[]);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">h1&lt;/span> &lt;span class="na">tabIndex&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">}&lt;/span> &lt;span class="na">ref&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="nx">el&lt;/span>&lt;span class="p">}&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">{&lt;/span>&lt;span class="nx">props&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">children&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">h1&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then replace your existing usages of &lt;code>h1&lt;/code> with the component, e.g. &lt;code>&amp;lt;FocusablePageHeading&amp;gt;My great page&amp;lt;/FocusablePageHeading&amp;gt;&lt;/code>.&lt;/p>
&lt;p>And that’s it! Note that we don’t need to do anything fancy to make the &lt;code>h1&lt;/code> unfocusable after it is blurred. That’s because although we give the element a &lt;code>tabindex&lt;/code> attribute to make it focusable programmatically, because its value is &lt;code>-1&lt;/code> the user will be unable to re-focus it with the Tab key once it is blurred.&lt;/p>
&lt;h2 id="further-reading">Further reading&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://www.gatsbyjs.com/blog/2019-07-11-user-testing-accessible-client-routing/">What we learned from user testing of accessible client-side routing techniques with Fable Tech Labs&lt;/a> — gatsbyjs.org&lt;/li>
&lt;li>&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_accessibility">Accessibility in React&lt;/a> — developer.mozilla.org&lt;/li>
&lt;/ul></description></item><item><title>How to test an error boundary component with React Testing Library</title><link>https://jshakespeare.com/react-error-boundary-testing-rtl/</link><pubDate>Mon, 16 Oct 2023 12:20:52 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/react-error-boundary-testing-rtl/</guid><description>&lt;p>[&lt;a href="#full-working-example">Skip straight to the code&lt;/a>]&lt;/p>
&lt;p>In a React application, an error boundary catches JavaScript errors thrown during the rendering of other components and optionally displays some fallback UI, typically an apologetic message and next steps to try. It may have other functional responsibilities like passing details of the error along to a monitoring service such as Sentry or New Relic. Without an error boundary, uncaught errors in a React application result in the ‘white screen of death’ whereby the application removes itself from the DOM entirely and the user is left in an empty void of nothingness.&lt;/p>
&lt;p>If your application already includes an error boundary component: congratulations on accepting your innate human fallibility! Now let’s finish the job and make sure it actually works by writing a unit test.&lt;/p>
&lt;p>In this post I won’t include the code for the error boundary component itself. The &lt;a href="https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary">React docs include a decent working example&lt;/a> and there are plenty of third party packages like &lt;a href="https://github.com/bvaughn/react-error-boundary">react-error-boundary&lt;/a> or &lt;a href="https://www.npmjs.com/package/@sentry/react">@sentry/react&lt;/a>’s &lt;code>ErrorBoundary&lt;/code> component. Good unit tests are agnostic to implementation details. All I’m assuming below is that your error boundary component can be passed children via &lt;code>props.children&lt;/code> and that when an error is caught, it will display some fallback copy instead of those children. I’m British so have chosen the word ‘sorry’. (Other apologies are available but please don’t use &lt;a href="https://alexwlchan.net/2022/no-cute/">annoying cutesy language&lt;/a>.)&lt;/p>
&lt;h2 id="setup">Setup&lt;/h2>
&lt;p>We need to do a couple of things before we can test the error boundary itself:&lt;/p>
&lt;ul>
&lt;li>Temporarily suppress &lt;code>console.log&lt;/code> errors so that our test runner process doesn’t get its output spammed with error messages or worse, aborted&lt;/li>
&lt;li>Define a React component that will throw an error when rendered&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-jsx" data-lang="jsx">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="nx">React&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;react&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="nx">MyErrorBoundary&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;./MyErrorBoundary&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">render&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">screen&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;@testing-library/react&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">describe&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;MyErrorBoundary&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// A very buggy component
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">ThrowError&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">throw&lt;/span> &lt;span class="k">new&lt;/span> &lt;span class="nb">Error&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;Test&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Temporarily suppress console errors so we don&amp;#39;t clog the logs
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">realError&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">beforeEach&lt;/span>&lt;span class="p">(()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">jest&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">fn&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">afterEach&lt;/span>&lt;span class="p">(()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">realError&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>(Yep, that really is a valid React component!)&lt;/p>
&lt;h2 id="assertions">Assertions&lt;/h2>
&lt;p>At its core an error boundary component only has two jobs: display its children when everything is fine, and display a fallback when those children throw an error.&lt;/p>
&lt;p>In RTL we can test this behavior with the following assertions:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-jsx" data-lang="jsx">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">it&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;renders children when everything is fine&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kr">async&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">render&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>&lt;span class="nx">Everything&lt;/span> &lt;span class="nx">is&lt;/span> &lt;span class="nx">fine&lt;/span>&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">screen&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">getByText&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/Everything is fine/i&lt;/span>&lt;span class="p">)).&lt;/span>&lt;span class="nx">toBeInTheDocument&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">it&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;shows an apologetic error message when an unhandled exception is thrown&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">render&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">ThrowError&lt;/span> &lt;span class="p">/&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>&lt;span class="nx">Everything&lt;/span> &lt;span class="nx">is&lt;/span> &lt;span class="nx">fine&lt;/span>&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">screen&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">queryByText&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/Everything is fine/i&lt;/span>&lt;span class="p">)).&lt;/span>&lt;span class="nx">not&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">toBeInTheDocument&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">screen&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">getByText&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/sorry/i&lt;/span>&lt;span class="p">)).&lt;/span>&lt;span class="nx">toBeInTheDocument&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If your error boundary has other functionality you want to test, for example displaying a contact form, you could add assertions for that too.&lt;/p>
&lt;h2 id="full-working-example">Full working example&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-jsx" data-lang="jsx">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="nx">React&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;react&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="nx">MyErrorBoundary&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;./MyErrorBoundary&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">render&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">screen&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;@testing-library/react&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">describe&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;MyErrorBoundary&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// A very buggy component
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">ThrowError&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">throw&lt;/span> &lt;span class="k">new&lt;/span> &lt;span class="nb">Error&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;Test&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Temporarily suppress console errors so we don&amp;#39;t clog the logs
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">realError&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">beforeEach&lt;/span>&lt;span class="p">(()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">jest&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">fn&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">afterEach&lt;/span>&lt;span class="p">(()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">realError&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">it&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;renders children when everything is fine&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kr">async&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">render&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>&lt;span class="nx">Everything&lt;/span> &lt;span class="nx">is&lt;/span> &lt;span class="nx">fine&lt;/span>&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">screen&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">getByText&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/Everything is fine/i&lt;/span>&lt;span class="p">)).&lt;/span>&lt;span class="nx">toBeInTheDocument&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">it&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;shows an apologetic error message when an unhandled exception is thrown&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">render&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">ThrowError&lt;/span> &lt;span class="p">/&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>&lt;span class="nx">Everything&lt;/span> &lt;span class="nx">is&lt;/span> &lt;span class="nx">fine&lt;/span>&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">p&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">MyErrorBoundary&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">screen&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">queryByText&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/Everything is fine/i&lt;/span>&lt;span class="p">)).&lt;/span>&lt;span class="nx">not&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">toBeInTheDocument&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">screen&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">getByText&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/sorry/i&lt;/span>&lt;span class="p">)).&lt;/span>&lt;span class="nx">toBeInTheDocument&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>How did JavaScript’s console.log get its name?</title><link>https://jshakespeare.com/javascript-console-log-etymology/</link><pubDate>Mon, 07 Dec 2020 12:20:52 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/javascript-console-log-etymology/</guid><description>&lt;p>If you’re a JavaScript developer you will be familiar with &lt;code>console.log&lt;/code>, a function for printing a value to the console. &lt;code>console.log&lt;/code> is often used for debugging, and if you’re a great JavaScript developer like me, you mostly use it to determine whether your code is working by printing things like &lt;code>'asdasdsds'&lt;/code> or &lt;code>'HELLO IS THIS RUNNING?!?!'&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">msg&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;JavaScript is fun&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">msg&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">&amp;gt; &amp;#39;JavaScript is fun&amp;#39;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Even if you write &lt;code>console.log&lt;/code> many times a day, you probably haven’t stopped to think about how the words ‘console’ and ‘log’ ended up in a programming language. Isn’t ‘console’ something you do when someone is upset? Isn’t a ‘log’ something you put on a fire?&lt;/p>
&lt;p>Let’s take an etymological stroll through history and see what we can find. Our journey will take us from the early days of computing to medieval churches to the high seas, and we’ll learn that the seemingly unrelated definitions of these words have more in common than you might think.&lt;/p>
&lt;h2 id="console">Console&lt;/h2>
&lt;p>This is a JavaScript debugging console. It displays messages that help a programmer understand what’s happening in the code that’s being executed.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/js-console.gif"
alt="A screenshot of the JavaScript debugging console in Google Chrome showing various logged messages"/>&lt;figcaption>
&lt;p>The JavaScript console in Google Chrome’s developer tools. Messages from &lt;code>console.log&lt;/code> (or in the case of the red and yellow messages, &lt;code>console.error&lt;/code> and &lt;code>console.warn&lt;/code>) appear here&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>The concept of a debugging console is not unique to JavaScript. It’s a basic software interface common to many programming languages and applications. It provides a way for a program to output low-level textual information and, optionally, receive inputs for controlling the program.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/osx-console.gif"
alt="A screenshot of the OSX Console application"/>&lt;figcaption>
&lt;p>The OSX Console application&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>The definition of console as a software feature comes from the early days of computing, when a user interacted with a computer mainframe (which was often very large and possibly located in another room or building) via an array of controls set in a desk.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/ibm7090.jpg"
alt="A man in a white shirt pressing a button on a 1960&amp;#39;s IBM computer console"/>&lt;figcaption>
&lt;p>A meteorologist at the console of the IBM 7090 electronic computer in the Joint Numerical Weather Prediction Unit in 1965. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:IBM_7090_console_used_by_a_meteorologist,_1965.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Early computer consoles were controlled by switches and buttons, while output was relayed to the operator via lights, dials, and paper printouts. Over time, consoles evolved to incorporate a command-line interface on a screen, with which the user would enter instructions using a keyboard.&lt;/p>
&lt;p>We can find analogs of the computer console by looking to the world of music. Sound mixing consoles have existed in one form or another since the advent of electrical audio recording and broadcast almost a century ago.&lt;/p>
&lt;p>But the use of the word console to describe a panel of electrical controls can be traced even further back, beyond the discovery of electricity itself, to the pipe organ.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/pipe-organ-ebrach-kirche.jpg"
alt="Interior photograph of a church showing the pipe organ facade below a large stained glass window"/>&lt;figcaption>
&lt;p>The pipe organ facade in the former abbey church in Ebrach, Germany. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Ebrach_Kirche_rose_window_Orgel_P4252411efs.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>The pipe organ is the largest musical instrument, and is played by an organist who sits at a complex array of controls called a console. Organ consoles consist of keyboards (typically three to five), pedals, and stops - binary valves used for controlling wind flow to the pipes.&lt;/p>
&lt;figure class="post__figure--small">&lt;img src="https://jshakespeare.com/post_images/organ-console.jpg"
alt="A pipe organ console with console brackets"/>&lt;figcaption>
&lt;p>Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Usnaconsole.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Organ consoles give us another everyday term: the idiom ‘pulling out all the stops’, meaning to apply maximum effort to a task. When every stop on the console is open an organ will produce sound using every available pipe, and therefore at maximum volume.&lt;/p>
&lt;figure class="post__figure--small">&lt;img src="https://jshakespeare.com/post_images/cesar-franck.jpg"
alt="Painting of composer and organist César Franck pulling out an organ stop"/>&lt;figcaption>
&lt;p>Composer César Franck pulling out a stop at the console of the organ at St. Clotilde Basilica, Paris, 1885. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Cesar_Franck_At_Organ.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>But where did this type of console get its name? One clue lies in the fact that many organ consoles share a design feature with the console table. Modern console tables are typically four-legged, free-standing structures designed to abut a wall. But if we go back a few centuries, we find that console tables were more commonly wall-mounted with brackets beneath the table surface to bear the load.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/console-table.jpg"
alt="Marble and gilt oak console table with decoratively carved supporting brackets"/>&lt;figcaption>
&lt;p>French Regency console table from the Metropolitan Museum of Art collection in New York. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Console_table_MET_DP268329.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>These supporting brackets are where the console table, (and perhaps the organ console) gets its name. In architecture, a console refers to an ornamental bracket that supports a protruding structure, such as a ledge, cornice, or balcony.&lt;/p>
&lt;figure class="post__figure--small">&lt;img src="https://jshakespeare.com/post_images/console-architecture.jpg"
alt="A neoclassical stone console supporting a balcony"/>&lt;figcaption>
&lt;p>Neoclassical stone console supporting a balcony at the Maison médicale de l&amp;rsquo;Œuvre du calvaire in Brussels. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Fa%C3%A7adebalconconsolechausseewavreneoclassique.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>At this point, our etymological path forks. An architectural console may get its name from the verb ‘to console’: literally &lt;em>con solaris&lt;/em> or ‘with soothing’; a relieving of pain or, perhaps, of weight. You could consider the job of a console to ‘relieve’ the weight of the structure above it.&lt;/p>
&lt;p>An alternative explanation takes us back to the church, where in medieval times choir stalls were decorated with carved male figures called &lt;em>consolateurs&lt;/em> or ‘consolers’. This in itself may have been a pun on the fact that consolers were often carved into armrests, thus supporting the weight of a seated chorist’s arms.&lt;/p>
&lt;figure class="post__figure--small">&lt;img src="https://jshakespeare.com/post_images/choir-stall.jpg"
alt="Illustration of wooden choir stalls showing carved consoles in the arm rests"/>&lt;figcaption>
&lt;p>Illustration showing the carved wooden &lt;em>consolateurs&lt;/em> in the armrests of the choir stalls at Andlau Abbey in Alsace, France. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Stalles.eglise.Anellau.png">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>So, far from being coincidental homonyms, it turns out that the many definitions of ‘console’ share a common etymological root, even when it’s used as a verb. Does the same go for ‘log’?&lt;/p>
&lt;h2 id="log">Log&lt;/h2>
&lt;p>In computing, the term ‘log’ derives from a ship’s log, which is short for ‘logbook’. A logbook is used for recording information about any event taking place aboard a vessel, from navigation to maintenance to changes in crew. While there are typically no restrictions about what can be recorded in a logbook, the name comes from a very specific use: recording the ship’s speed.&lt;/p>
&lt;p>Long before the invention of modern instruments, such as GPS, recording speed was an essential part of navigating the seas. Sailors would measure their speed and record it in the logbook every half hour, and by knowing how fast they were going, combined with the direction in which they were traveling (measured using a compass), they could work out where they were on the map.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/ships-logbook.jpg"
alt="Ships logbook showing handwritten entries"/>&lt;figcaption>
&lt;p>Logbook from the USS Savannah with entries from September 3-15, 1943. Image credit: &lt;a href="https://commons.wikimedia.org/wiki/File:19430903-19430915_USS_Savannah_CL-42_Turret_Two_log_book_entries.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>The logbook got its name from the chip log, a device that sailors used to measure the ship’s speed. This consisted of a quarter-circle piece of wood (the ‘chip’) with a rope tied to it, in which knots were tied at regular intervals.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/chip-log.jpg"
alt="Chip log"/>&lt;figcaption>
&lt;p>19th century chip log. Image credit: &lt;a href="https://en.wikipedia.org/wiki/File:Loch_%C3%A0_plateau.jpg">Wikimedia Commons&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>A sailor would throw the chip overboard and count how many knots spooled out while another sailor timed him using a sand timer. When the timer was up, the number of knots that had passed over the stern could be used to calculate the ship’s speed, which was then recorded in the logbook. This is where we get the term ‘knot’ as shorthand for 1 nautical mile per hour.&lt;/p>
&lt;figure class="post__figure--medium">&lt;img src="https://jshakespeare.com/post_images/chip-log-engraving.jpg"
alt="An engraving of three mates measuring a ship’s speed using a chip log"/>
&lt;/figure>
&lt;p>Before the invention of the chip log, an even more rudimentary technique for gauging speed was the Dutchman’s log. One sailor would throw any floating object (probably an actual log) over the bow of the ship while another sailor on the stern timed how long it took for the object to pass him. With this measurement (and knowing the length of the ship), the sailors could make a rough estimate of how fast they were travelling.&lt;/p>
&lt;p>The days of using a log to measure a ship’s speed lie centuries behind us, but ‘log’ is a ubiquitous term in modern life thanks to its importance in computing (just think how many times you ‘log in’ to various pieces of software every day). And it’s just one of many modern definitions whose origins lie in our civilization’s long maritime history. We don’t need to look far to find more: the word ‘cyber’ derives from the ancient Greek word &lt;em>kubernētēs&lt;/em>, meaning steersman or rudder, and the word ‘gadget’ was a slang term used by sailors for any part of a ship that they couldn’t remember the correct name of.&lt;/p>
&lt;p>So next time you write &lt;code>console.log&lt;/code> in your editor, spare a thought for the computer pioneers, pipe organists, medieval carvers, and old-word mariners who played a small part in giving us the very modern definitions of these two words.&lt;/p>
&lt;hr>
&lt;p>&lt;em>If you enjoyed this, you may want to read &lt;a href="https://jshakespeare.com/your-creations-will-not-outlast-you/"> Your creations will not outlast you&lt;/a>.&lt;/em>&lt;/p></description></item><item><title>Use git hooks and husky to tell your teammates when to run npm install</title><link>https://jshakespeare.com/use-git-hooks-and-husky-to-tell-your-teammates-when-to-run-npm-install/</link><pubDate>Sun, 02 Aug 2020 12:20:52 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/use-git-hooks-and-husky-to-tell-your-teammates-when-to-run-npm-install/</guid><description>&lt;p>A former coworker and I had a system for letting each other know about dependency changes in shared codebases. We wrote &lt;code>npm install&lt;/code> on a post-it note and would stick it to the other person’s monitor. Once the post-it note eventually lost its stickiness, we just screwed up and threw it at each other. By the end of the project, the universal sign for ‘update your dependencies’ was to throw a random object across the room.&lt;/p>
&lt;p>The only downside of this otherwise perfect system is that it doesn’t scale well, especially on distributed teams. A much better solution is to use git hooks.&lt;/p>
&lt;p>&lt;a href="https://git-scm.com/docs/githooks">Git hooks&lt;/a> execute a command when git performs an action. They are useful for doing things like running a linter before you commit (&lt;code>pre-commit&lt;/code>) or running a build script when a server receives a push (&lt;code>post-receive&lt;/code>).&lt;/p>
&lt;p>We want to check whether &lt;code>package-lock.json&lt;/code> has changed when we run &lt;code>git pull&lt;/code>, so we can use the &lt;code>post-merge&lt;/code> hook (&lt;code>git pull&lt;/code> being equivalent to &lt;code>git fetch&lt;/code> + &lt;code>git merge&lt;/code>). If the lockfile has changed, we can &lt;code>echo&lt;/code> a notification to the user that they should run &lt;code>npm install&lt;/code> to bring their &lt;code>node_modules&lt;/code> directory up to date.&lt;/p>
&lt;h2 id="create-a-git-hook-file">Create a git hook file&lt;/h2>
&lt;p>In your repo, create a file called &lt;code>post-merge&lt;/code> in &lt;code>.git/hooks&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">touch .git/hooks/post-merge
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In that file we’re going to define a function that checks whether a file has changed between commits, and run that function with &lt;code>package-lock.json&lt;/code> as the argument:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">&lt;span class="k">function&lt;/span> changed &lt;span class="o">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> git diff --name-only HEAD@&lt;span class="o">{&lt;/span>1&lt;span class="o">}&lt;/span> HEAD &lt;span class="p">|&lt;/span> grep &lt;span class="s2">&amp;#34;^&lt;/span>&lt;span class="nv">$1&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &amp;gt; /dev/null 2&amp;gt;&lt;span class="p">&amp;amp;&lt;/span>&lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="o">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> changed &lt;span class="s1">&amp;#39;package-lock.json&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;📦 package-lock.json changed. Run npm install to bring your dependencies up to date.&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You could add notifications for other file changes in here, like &lt;code>Gemfile.lock&lt;/code> or &lt;code>Podfile.lock&lt;/code>.&lt;/p>
&lt;p>Credit to &lt;a href="https://gist.github.com/8bitDesigner/5869846">8bitDesigner&lt;/a> for this approach.&lt;/p>
&lt;h2 id="test-the-hook">Test the hook&lt;/h2>
&lt;p>Try out the hook by rewinding to a commit where you know &lt;code>package-lock.json&lt;/code> has changed, let’s say 20 commits ago, then running &lt;code>git pull&lt;/code> to fast-forward back to now:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">git reset --hard HEAD~20 &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> git pull
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If package-lock.json changed between now and then, you will see the notification.&lt;/p>
&lt;h2 id="use-husky-to-share-your-hook-with-others">Use husky to share your hook with others&lt;/h2>
&lt;p>Changes in the &lt;code>.git&lt;/code> directory are not version controlled, so we need to move our hook to somewhere that is. We then need to provide a way for other developers to easily install the hook for themselves. Thankfully &lt;a href="https://github.com/typicode/husky">husky&lt;/a> exists to do exactly that.&lt;/p>
&lt;p>Create a directory called &lt;code>.githooks&lt;/code> and move your hook there:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">mkdir .githooks &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> mv .git/hooks/post-merge .githooks
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Now we can set up husky. The npm post-install script for husky checks &lt;code>package.json&lt;/code> for any declared hooks and automatically installs them in &lt;code>.git/hooks&lt;/code>.&lt;/p>
&lt;p>In &lt;code>package.json&lt;/code> add:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="cl">&lt;span class="s2">&amp;#34;husky&amp;#34;&lt;/span>&lt;span class="err">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;hooks&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;post-merge&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;./.githooks/post-merge&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then install husky:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">npm install --save-dev husky
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Next time someone runs &lt;code>npm install&lt;/code> with your changes, the hook will be installed in their local &lt;code>.git&lt;/code> directory.&lt;/p></description></item><item><title>A simple count-up number animation with JavaScript</title><link>https://jshakespeare.com/simple-count-up-number-animation-javascript-react/</link><pubDate>Sun, 05 Apr 2020 21:00:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/simple-count-up-number-animation-javascript-react/</guid><description>&lt;p>Say you want to display a numeric value on a webpage with a count-up animation, like this:&lt;/p>
&lt;script
data-slug-hash="KKpjdYv"
data-user="jshakes"
data-height="300"
data-default-tab="result"
data-theme-id="8862"
class='codepen'
async
src="//codepen.io/assets/embed/ei.js"
>&lt;/script>
&lt;!-- raw HTML omitted -->
&lt;p>There are a few popular libraries that will do this for you, often with lots of configurable bells and whistles. I’m always reluctant to add a dependency to my application that I could write myself in under an hour (&lt;a href="https://www.theregister.co.uk/2016/03/23/npm_left_pad_chaos/">and you should be too&lt;/a>), so I did just that.&lt;/p>
&lt;p>Here’s the code in vanilla JavaScript using the DOM API (&lt;a href="https://codepen.io/jshakes/pen/KKpjdYv">CodePen here&lt;/a>):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// How long you want the animation to take, in ms
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="kr">const&lt;/span> &lt;span class="nx">animationDuration&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">2000&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Calculate how long each ‘frame’ should last if we want to update the animation 60 times per second
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="kr">const&lt;/span> &lt;span class="nx">frameDuration&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">1000&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="mi">60&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Use that to calculate how many frames we need to complete the animation
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="kr">const&lt;/span> &lt;span class="nx">totalFrames&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">Math&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">round&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">animationDuration&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="nx">frameDuration&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// An ease-out function that slows the count as it progresses
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="kr">const&lt;/span> &lt;span class="nx">easeOutQuad&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">t&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="nx">t&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="mi">2&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="nx">t&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// The animation function, which takes an Element
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="kr">const&lt;/span> &lt;span class="nx">animateCountUp&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">el&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kd">let&lt;/span> &lt;span class="nx">frame&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">countTo&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">parseInt&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">el&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">innerHTML&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">10&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Start the animation running 60 times per second
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">counter&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">setInterval&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">frame&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Calculate our progress as a value between 0 and 1
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="c1">// Pass that value to our easing function to get our
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="c1">// progress on a curve
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">progress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">easeOutQuad&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">frame&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="nx">totalFrames&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Use the progress value to calculate the current count
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kr">const&lt;/span> &lt;span class="nx">currentCount&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">Math&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">round&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">countTo&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="nx">progress&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// If the current count has changed, update the element
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="nb">parseInt&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">el&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">innerHTML&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">10&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="o">!==&lt;/span> &lt;span class="nx">currentCount&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">el&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">innerHTML&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">currentCount&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// If we’ve reached our last frame, stop the animation
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="nx">frame&lt;/span> &lt;span class="o">===&lt;/span> &lt;span class="nx">totalFrames&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">clearInterval&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">counter&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span> &lt;span class="nx">frameDuration&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Run the animation on all elements with a class of ‘countup’
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="kr">const&lt;/span> &lt;span class="nx">runAnimations&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">countupEls&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">querySelectorAll&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="s1">&amp;#39;.countup&amp;#39;&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">countupEls&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">forEach&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">animateCountUp&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And as a React component (using the useEffect and useState hooks):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="nx">React&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">useEffect&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">useState&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;react&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">easeOutQuad&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">t&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="nx">t&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="mi">2&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="nx">t&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">frameDuration&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">1000&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="mi">60&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">CountUpAnimation&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">children&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">duration&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">2000&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">countTo&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">parseInt&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">children&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">10&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="p">[&lt;/span> &lt;span class="nx">count&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">setCount&lt;/span> &lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">useState&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="mi">0&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">useEffect&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kd">let&lt;/span> &lt;span class="nx">frame&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">totalFrames&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">Math&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">round&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">duration&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="nx">frameDuration&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">counter&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">setInterval&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">frame&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">progress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">easeOutQuad&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">frame&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="nx">totalFrames&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">setCount&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">countTo&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="nx">progress&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span> &lt;span class="nx">frame&lt;/span> &lt;span class="o">===&lt;/span> &lt;span class="nx">totalFrames&lt;/span> &lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">clearInterval&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">counter&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span> &lt;span class="nx">frameDuration&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span> &lt;span class="p">[]&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="nb">Math&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">floor&lt;/span>&lt;span class="p">(&lt;/span> &lt;span class="nx">count&lt;/span> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Use the component to count to 500
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="o">&amp;lt;&lt;/span>&lt;span class="nx">CountUpAnimation&lt;/span>&lt;span class="o">&amp;gt;&lt;/span>&lt;span class="mi">500&lt;/span>&lt;span class="o">&amp;lt;&lt;/span>&lt;span class="err">/CountUpAnimation&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// …optionally using the duration prop
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="o">&amp;lt;&lt;/span>&lt;span class="nx">CountUpAnimation&lt;/span> &lt;span class="nx">duration&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="mi">1000&lt;/span>&lt;span class="p">}&lt;/span>&lt;span class="o">&amp;gt;&lt;/span>&lt;span class="mi">500&lt;/span>&lt;span class="o">&amp;lt;&lt;/span>&lt;span class="err">/CountUpAnimation&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There are a couple of things you might want to change to suit your needs here. The first is that the examples above use an ease-out function, so that the count slows as it nears the final number. You can use any easing function (or none at all) that you like by replacing &lt;code>easeOutQuad&lt;/code>. Find a list of JavaScript implementations of &lt;a href="https://gist.github.com/gre/1650294">easing functions in this Gist&lt;/a>, and handy visualizations for each function on &lt;a href="https://easings.net">easings.net&lt;/a>.&lt;/p>
&lt;p>Secondly, you may want to format the final value, e.g. by adding commas to break up large numbers, or rounding to a certain number of decimal points (&lt;a href="https://blog.abelotech.com/posts/number-currency-formatting-javascript/">find both of these examples here&lt;/a>). You can do this immediately before you return the current count value, e.g. replacing &lt;code>return Math.floor(count)&lt;/code> with &lt;code>return myNumberFormattingFunc(count)&lt;/code>.&lt;/p>
&lt;p>With thanks to &lt;a href="https://robertheaton.com/">Rob Heaton&lt;/a> for helping me with this solution.&lt;/p></description></item><item><title>Don’t mislabel mailto links</title><link>https://jshakespeare.com/dont-mislabel-mailto-links/</link><pubDate>Sat, 22 Feb 2020 10:47:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/dont-mislabel-mailto-links/</guid><description>&lt;p>Do you have a link on your website that looks like this?&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">a&lt;/span> &lt;span class="na">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;mailto:yourname@yoursite.com&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>Contact&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">a&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If so, you are probably frustrating a lot of your users.&lt;/p>
&lt;p>Mailto links instruct the operating system to open the default email client and create a new draft email with the ‘to’ field already populated. That’s fairly presumptuous behavior at the best of times, but it’s especially jarring to a user who was expecting to see a web page with, say, a contact form on it.&lt;/p>
&lt;p>An improvement would be to use the word ‘email’ in the label text:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">a&lt;/span> &lt;span class="na">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;mailto:yourname@yoursite.com&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>Email us&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">a&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This is better because it creates a clearer expectation of what will happen when the user clicks the link, but it still isn’t much help to people who don’t want mailto behavior in the first place. Specifically, this is anybody who:&lt;/p>
&lt;ul>
&lt;li>Doesn’t use a native email client, for example Gmail users who use mail.google.com. (Some browsers will open mailto links using a web address, but this often requires some non-trivial tweaking &lt;a href="https://blog.hubspot.com/marketing/set-gmail-as-browser-default-email-client-ht">deep in the application settings&lt;/a>.)&lt;/li>
&lt;li>Uses a third-party native email client but is unable to make that their default email client, for example an iPhone user who &lt;a href="https://www.lifewire.com/choose-default-apps-on-iphone-4044162">uses anything other than Apple’s Mail app&lt;/a>.&lt;/li>
&lt;/ul>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/ios-mail.jpg"
alt="Screenshot of the Apple iOS Mail app setup screen"/>&lt;figcaption>
&lt;p>As a self-hating iPhone user who uses the Microsoft Outlook app for email, this is what I see when I tap a mailto link in my web browser. It’s not very useful.&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>What’s more, labeling an email address with ‘email’ is redundant. Everyone knows that &lt;code>yourname@yoursite.com&lt;/code> is an email address, so why not use that as the label?&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">a&lt;/span> &lt;span class="na">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;mailto:yourname@yoursite.com&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>yourname@yoursite.com&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">a&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Not only does this establish a crystal clear expectation of behavior, it also means the user can actually &lt;em>read&lt;/em> the email address. Best of all, it’s easy to copy and paste the address, which is important for users who don’t want mailto behavior or want to do something else with the email address like save it to their contacts.&lt;/p>
&lt;p>As a final note, I would encourage anyone who publishes content on the web to think carefully about how they label any link, not just mailto links. Many users rely on label text as their only context for where a link will take them, for example touchscreen users who can’t hover on a link to see its URL, or screen-reader users who may see a link isolated from its surrounding text (try using a screen-reader to navigate a web page full of links labelled ‘click here’ or ‘more’ and you will quickly realize how valuable meaningful link labels are). As always, less guesswork makes for a more usable website.&lt;/p></description></item><item><title>The Meg Ryan effect</title><link>https://jshakespeare.com/the-meg-ryan-effect/</link><pubDate>Tue, 04 Feb 2020 10:47:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/the-meg-ryan-effect/</guid><description>&lt;p>If you had been watching BBC One at 11pm on 25 October 2003, you would have witnessed one of the most talked about moments in British television history. This was the time-slot of &lt;em>Parkinson&lt;/em>, then the BBC’s flagship chat show, hosted by veteran interviewer and national treasure Michael ‘Parky’ Parkinson. That night’s top-billed guest was Meg Ryan, fresh from a messy divorce and in the midst of a media tour to promote her latest film &lt;em>In the Cut&lt;/em>.&lt;/p>
&lt;p>The format of &lt;em>Parkinson&lt;/em> was straightforward: three celebrity guests would chat with Parky, whose grandfatherly demeanor and warm Yorkshire accent gave the show the air of a gentle fireside chat. There was some middle-of-the-road musical entertainment, the audience clapped and everyone went home. It was a tried and tested format that had changed little since the 1970’s, and although Parky was in the twilight of his career by 2003, millions still tuned in every Saturday night to enjoy his cozy brand of entertainment.&lt;/p>
&lt;p>But tonight something felt different. Far from a convivial chat, Parkinson and Ryan were butting heads. Ryan sat with her arms and legs crossed, leaning away from Parkinson and replying in monosyllables to a barrage of increasingly needling questions from the host.&lt;/p>
&lt;p>Parkinson pursued line after line of awkward and sometimes overly personal questioning, even suggesting at one point that her decision to star in &lt;em>In the Cut&lt;/em>, a darker movie than the breezy rom-coms that had made her famous, was because she was ‘bruised’ from her recent divorce.&lt;/p>
&lt;p>Just as the tension started to boil, Parkinson changed the topic to Ryan’s early ambitions to be a journalist. Things seemed to cool as Ryan described her time studying at New York University and how she ended up becoming an actor instead. But Parkinson’s choice of topic was a trap. “Now that you’re wary of journalists, does it give you an insight into what they’re after?” He asked. Ryan paused “Now that I’m wary of them?” She echoed. Parkinson pressed on. “Yes, you are wary of journalists. You’re wary of me. You’re wary of being interviewed, you don’t like being interviewed. You can see it in the way you sit, the way you are” Ryan looked around the studio incredulously as Parkinson continued. “In other words if you were me, what would you do now?” Ryan thought for a second and delivered the perfect riposte, “I’d just wrap it up”. The audience laughed nervously.&lt;/p>
&lt;p>The interview caused a national sensation. Water-coolers around the country were abuzz. The media firmly sided with Parkinson and subsequently dragged Ryan through the mud, labeling her arrogant and sullen. In a &lt;em>Guardian&lt;/em> article (published almost two months after the episode aired, to give you some idea of the fallout), Peter Bradshaw wrote:&lt;/p>
&lt;blockquote>
&lt;p>“Tetchy, defensive, and sometimes apparently just not interested, Ryan gave the British public an unscripted glimpse into the life of the pampered A-lister ungraciously submitting to the publicity process and looking with uncomprehending distaste, as if through a thick glass screen, at the non-American media and public she was forced to court.”&lt;/p>
&lt;/blockquote>
&lt;p>Despite being just 15 at the time and largely disinterested in celebrity tattle, I accepted this narrative as the canonical version of events. In fact if you’d asked me what I thought about Meg Ryan even a couple of weeks ago, I would probably have given you a pretty similar account to the one above.&lt;/p>
&lt;p>So when I recently pulled up a &lt;a href="https://www.youtube.com/watch?v=blpq-Iwu25s">clip of the exchange on YouTube&lt;/a> I was confused. Meg Ryan wasn’t being difficult at all, Parkinson was being rude and condescending to her. Was this definitely the right clip? I scrolled down to read the comments:&lt;/p>
&lt;blockquote>
&lt;p>“Watching this back after many years believing Meg Ryan was a lunatic in this interview. How wrong I was, Meg answers his question with honesty and sincerity whilst completely playing ball - Parky goads and continually tried to catch her out then turns aggressive and intimidating - disgusting”&lt;/p>
&lt;/blockquote>
&lt;p>&amp;ndash;Will Bryan&lt;/p>
&lt;blockquote>
&lt;p>“I remember really slagging Meg off about this interview years ago. I’m sorry Meg. There was ‘something in the water’ and I thought you were really unresponsive - and I even remember you walking out of the studio [she didn’t]. Damn! I’m so sorry I remembered this so negatively. You were sweet and natural and still so lovely.”&lt;/p>
&lt;/blockquote>
&lt;p>&amp;ndash;McPrfctday&lt;/p>
&lt;blockquote>
&lt;p>“I totally agree with the majority of other comments about Parky. I’m not sure why Meg Ryan got the bad press following this interview, she didn’t deserve to in my book.”&lt;/p>
&lt;/blockquote>
&lt;p>&amp;ndash;Brenda Anglim&lt;/p>
&lt;p>Normally I would never encourage anyone to read YouTube comments, but the ones beneath this clip are worth a look; they are almost unanimously in Ryan’s favor. I couldn’t find a single comment that came even close to the vitriol that was printed about her at the time. What was going on? Had everyone secretly agreed that we all got it wrong?&lt;/p>
&lt;p>This disparity between memory and reality made me think of the Mandela Effect, a largely unexplained phenomenon whereby large groups of strangers seemingly share false memories, sometimes in great detail. It is named after a popular false memory, reportedly shared by millions of people, that Nelson Mandela died in prison in the 1980’s, even though he was released from prison and lived for another 30 years. Was the Meg Ryan incident a case of misremembering en masse?&lt;/p>
&lt;p>There are a number of explanations we could look at. An improvement in Anglo-US relations after the invasion of Iraq, perhaps. Or the #metoo movement. Or the &lt;a href="https://en.wikipedia.org/wiki/Operation_Yewtree">shocking revelations&lt;/a> about the behavior of other so-called national-treasures from 1970’s Britain that tarnished a generation of entertainers. But I’d like to favor Occam’s razor and offer a more simple explanation: we have, as a society, got better at listening to women. Parkinson vs. Ryan is not a case of collective misremembering but a touchstone against which we can measure our progress. In 2003 we saw a difficult, ‘tetchy’ celebrity daring to insult an institutional, avuncular figure. Today we see a man bullying a woman.&lt;/p>
&lt;p>It’s an encouraging sign that things are getting better at a time when it often feels like they aren’t. No doubt there are similar exchanges taking place today that we will look back on 16 years from now and wonder how we got it so wrong. By way of an apology, let’s call it the Meg Ryan effect: not a collective misremembering, but the steady march of progress.&lt;/p></description></item><item><title>Finding a voice</title><link>https://jshakespeare.com/finding-a-voice/</link><pubDate>Sun, 12 Jan 2020 10:47:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/finding-a-voice/</guid><description>&lt;p>Writers sometimes talk about finding their ‘voice’, a writing style that allows them to tell a story effectively. Every writer’s voice is different; you could probably identify your favorite author given just a few paragraphs of their writing.&lt;/p>
&lt;p>I’ve been keeping a blog for about seven years, which is to say that I wrote my first blog post seven years ago and have published about thirty more posts at irregular intervals since then. I’ve tried out several different voices during this time but none of them have felt quite right. This has meant I’ve found the writing process arduous and the habit has never stuck.&lt;/p>
&lt;p>When I was redesigning my website recently I gave this problem a lot of thought. I like the idea of having a blog, but if I don’t actually enjoy writing, is it the right hobby for me? I suspected the problem was that I just hadn’t found the right voice yet, and that if I did I would enjoy writing more.&lt;/p>
&lt;p>It seems logical to me that if you’re going to write for fun, you should write things that you would want to read. With this in mind I started by thinking about the non-fiction authors whose work I enjoy reading. (Some of those authors are listed at the bottom of this post). I realized that most of these authors have several things in common:&lt;/p>
&lt;ol>
&lt;li>They write in simple, conversational language&lt;/li>
&lt;li>They use the first person a lot&lt;/li>
&lt;li>They often use a narrative structure with lots of anecdotes&lt;/li>
&lt;li>Their work usually contains at least one surprising fact that sticks in my head after I finish reading&lt;/li>
&lt;/ol>
&lt;p>These qualities mean that the author’s personality is allowed to shine through their writing, so reading it feels human and intimate. My favorite non-fiction pieces read as if they had been emailed to me by a knowledgeable friend.&lt;/p>
&lt;p>It struck me that this type of voice has more in common with spoken language than it does with most written language. When people talk they do so from their own perspective, they use anecdotes to engage the listener, and they use their knowledge and personal experience to make what they’re saying relevant and informative. In other words, they naturally use a lot of the same techniques that go into good writing.&lt;/p>
&lt;p>I’m not the first person to have noticed this. In &lt;a href="http://www.paulgraham.com/talk.html">an essay from 2015&lt;/a>, Paul Graham wrote:&lt;/p>
&lt;blockquote>
&lt;p>“If you simply manage to write in spoken language, you’ll be ahead of 95% of writers. And it’s so easy to do: just don’t let a sentence through unless it’s the way you’d say it to a friend.”&lt;/p>
&lt;/blockquote>
&lt;p>Suddenly, the idea of writing seems a lot less daunting: write like you speak. Not only does this make the process of writing easier, it makes it easier to think of things to write about because all you have to do is think back to the last good conversation you had.&lt;/p>
&lt;p>So perhaps my writer’s voice is just that: my voice. The same voice I use when I explain a technical concept to a colleague or tell a friend &lt;a href="https://jshakespeare.com/disney-earth/">what I learned about cruise ships while on vacation in Iceland&lt;/a>. It feels light and effortless, dare I say it, even enjoyable.&lt;/p>
&lt;p>I’m excited to hone my voice in my writing this year and beyond. Since you’ve read this far, I hope you’ll join me. Thank you for listening.&lt;/p>
&lt;hr>
&lt;p>&lt;em>Some authors whose non-fiction work I enjoy:&lt;/em>&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://kneelingbus.substack.com/p/108-grids-ladders-and-malls">Drew Austin, aka Kneeling Bus&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.google.com/books/edition/One_Summer/kI-n6fWpq98C?hl=en&amp;amp;gbpv=1&amp;amp;printsec=frontcover">Bill Bryson&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.bbc.co.uk/blogs/adamcurtis/entries/2989a78a-ee94-385e-808f-c9c7c38d1cb7">Adam Curtis&lt;/a>&lt;/li>
&lt;li>&lt;a href="http://www.paulgraham.com/work.html">Paul Graham&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://robertheaton.com/2019/06/17/childbirth-a-fathers-eye-view/">Robert Heaton&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.newyorker.com/magazine/2003/07/07/a-sudden-illness">Laura Hillenbrand&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.shatnerchatner.com/p/american-on-american-action-abroad-sorry-for-travel-writing-at-you">Daniel Lavery&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.newyorker.com/magazine/2019/12/02/hurricane-season">David Sedaris&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://waitbutwhy.com/2015/01/artificial-intelligence-revolution-1.html">Tim Urban, aka Wait But Why&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>Disney Earth</title><link>https://jshakespeare.com/disney-earth/</link><pubDate>Sat, 04 Jan 2020 10:47:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/disney-earth/</guid><description>&lt;p>Last summer my girlfriend and I spent a week in Iceland. We flew from Reykjavik to Akureyri and drove back via the &lt;em>Hringvegur&lt;/em> - the ring road that encircles the island’s volcanic, inhospitable inner territory. Iceland is sparsely habited to a degree that makes it feel otherworldly, but is nonetheless a highly popular tourist destination. We were two of over 2 million tourists that visit Iceland each year - roughly six times the country’s population.&lt;/p>
&lt;p>A small but highly visible form of tourism in Iceland is visiting by cruise ship. Enormous liners are a ubiquitous sight in ports around the country, where they dwarf the fishing vessels that until recent decades were the main engine of the Icelandic economy.&lt;/p>
&lt;p>The numbers involved with modern cruise ships are almost meaninglessly big. The heaviest cruisers rival aircraft carriers and oil tankers in size, with construction costs often exceeding one billion dollars. Royal Caribbean’s &lt;em>Oasis&lt;/em> class cruisers, currently the largest passenger ships in the world, are 360m long, weigh over 225,000 tons and can house over 5,500 passengers and 2,200 crew for weeks at a time. Each of the four active &lt;em>Oasis&lt;/em> class ships is bigger, heavier and more capacious than the Chrysler Building.&lt;/p>
&lt;p>Despite this heft, many of these vessels are remarkably maneuverable. Most large cruise ships have a draft of less than 9m and are equipped with 360 degree azimuthing propellor pods which afford them a narrow turning circle. Like the Viking longboat, they can sail far up rivers and fjords, offloading their passengers right into the hearts of cities and towns in the farthest corners of the world.&lt;/p>
&lt;p>For most of our Icelandic visit we stayed in Akureyri, the country’s second ‘city’, a relative term in Iceland - with a population of just 20,000 people it would be classified as a medium-sized town in the US. Akureyri is built on a hillside at the inland-most point of a fjord called the Eyjafjörður, and most mornings during the summer months a cruise ship sails the 60km from the mouth of the fjord in the north down to the port in Akureyri, where it sits and waits for its passengers, anywhere from a few hundred to several thousand, to look around the town before sailing back out to sea that evening. Armed with a pair of binoculars and the MarineTraffic app on my phone, I would identify each day’s visiting ship and compare it to the last, boring my girlfriend with statistics and itineraries while we ate our morning skyr and berries.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/akureyri-map.jpg"
alt="Map showing the location of Akureyri and the Eyjafjörður fjord"/>
&lt;/figure>
&lt;p>One morning I looked out of our vacation apartment window to see the &lt;em>Disney Magic&lt;/em> in port, owned and operated by Disney Cruise Lines (DCL). &lt;em>Magic&lt;/em> is relatively small for a modern cruise ship at 86,000 tons (the following day’s visitor would be the &lt;em>MSC Preziosa&lt;/em> at 139,000 tons), but it had an outsized presence in the port thanks to the exhaustive efforts of the Disney marketing department in the ship’s livery, which included red and blue Mickey-mouse themed paintwork and giant Disney logo emblazoned on each funnel.&lt;/p>
&lt;p>Maritime etiquette dictates that visiting ships fly the flag of the host port’s country as a sign of respect, but the Icelandic cross was absent from the bow of the &lt;em>Magic&lt;/em>, which instead flew its own flag bearing the Mickey Mouse head silhouette. Even the ship’s twenty lifeboats matched the bright yellow of Mickey’s bow-tie and shoes, DCL having successfully petitioned the UN International Maritime Organization in the nineties to permit a lifeboat color that deviated from the regulation dayglo orange.&lt;/p>
&lt;p>The ship’s passengers were not exempt from carrying the Disney brand, their matching t-shirts a recurrent visual theme as they were bussed around the town on chartered coaches during their brief on-shore visit. Despite being tourists ourselves we felt a greater kinship with the local residents, the subjects of a sightseeing excursion by visitors curious of their surroundings but reluctant to integrate with them.&lt;/p>
&lt;p>In the evening the &lt;em>Magic&lt;/em> blasted its horn, a seven-tone ditty to the tune of &lt;em>When you wish upon a star&lt;/em>, which echoed around the hills surrounding the Eyjafjörður. Gradually the Disney cruisers headed back to eat their dinner aboard the ship as it prepared to return to sea.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/eyjafjordur.jpg"
alt="The Eyjafjörður fjord with Disney Magic docked in Akureyri port"/>&lt;figcaption>
&lt;p>The Eyjafjörður fjord with Disney Magic docked in Akureyri port&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Cruising has always struck me as an odd choice of vacation, a bit like staying in a hotel for a week and only venturing outside for a few hours every other day. It’s a vacation that bears closer resemblance to a theme park than the vacation we were on. On a DCL cruise, Akureyri and other port stops are not so much destinations as backdrops to the Disney experience, artificial locations like Disneyland itself. See gothic German castles like the one in &lt;em>Tangled&lt;/em>, or snow-swept Scandinavian vistas like the ones in &lt;em>Frozen&lt;/em>, while Elsa and Anna sing to your kids. Why invite the world to Disneyland when you can take Disneyland to the world?&lt;/p>
&lt;p>Hosting these ships is the cost of doing business in a country whose economy relies heavily on tourism, but it must be a tough sell. The cruise industry as a whole has an appalling track record on environmental impact (see: Venice) and its massive vessels dominate small coastal towns while keeping passengers on the ship as much as possible. Accommodation, travel and food are already catered for onboard, leaving scant profit to be made by the local economy. They are the new longboats, sailing inland not to rape and pillage but to give their passengers nice photo opportunities and to leave the local residents inadvertently humming &lt;em>When you wish upon a star&lt;/em> for days afterwards. It’s a more profitable enterprise than their Viking ancestors could have dreamt of.&lt;/p>
&lt;p>That night, unable to sleep as the midnight sun peeped through the blinds of our room, I unlocked my phone and opened MarineTraffic to see where the &lt;em>Magic&lt;/em> was on the map. It had reached the Greenland Sea and was en route to its next port call in Kirkwall, Scotland, another former Viking settlement in the Orkney Islands at the northernmost tip of the UK. Its population: 9,293.&lt;/p></description></item><item><title>Design smells</title><link>https://jshakespeare.com/design-smells/</link><pubDate>Sat, 03 Aug 2019 10:47:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/design-smells/</guid><description>&lt;p>In digital product development, the process of turning visual mockups into code is sometimes called ‘translating’ a design. This choice of words highlights the fact that the design-to-code stage is not a rote formula that takes in pixels and spits out code, but an interpretation of meaning from one language to another. A translator must be proficient in both languages if she is to effectively convey meaning from one to the other. Therefore, it is the job of an interface developer to be fluent in the languages of both design and code.&lt;/p>
&lt;p>If you’ve ever learned a foreign language then you will know that words cannot simply be exchanged like-for-like in order to translate a phrase. (You will also know this if you’ve ever heard a joke that begins with &lt;a href="https://en.wikipedia.org/wiki/Russian_reversal">“in Soviet Russia”&lt;/a>). Translation requires knowing not just vocabulary but word order, idioms, pronouns, conjugations, etiquette, punctuation and more.&lt;/p>
&lt;p>The middle ground between any two languages is meaning, and that meaning exists in the mind of the translator. Translation requires unearthing meaning so it can be reconstructed in another language.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/translation-fail.jpg"
alt="A Filipino sign that reads &amp;#39;Manok ng ina mo&amp;#39;, with an English translation below it which reads &amp;#39;Chicken of your mother&amp;#39;"/>&lt;figcaption>
&lt;p>Translating words but not their meaning: the bread and butter of clickbait sites since 2007&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>To find the meaning within an interface design, a developer must look beneath the surface of the mockups and identify the rules and patterns that govern how the interface should behave in different contexts. Does the layout follow a grid system? Which values are fixed and which are fluid? What is the typographic hierarchy? The better she is at identifying these rules, the better the code will describe the design system.&lt;/p>
&lt;p>Describing a design system in code is important because mockups do not paint a complete picture of how an interface will behave. Mockups are frozen in time, but a user interface must be capable of responding to an infinite combination of browsers, devices, screen resolutions, zoom-levels, and content. Code that effectively describes a design system can fill in these gaps and makes the interface more robust.&lt;/p>
&lt;p>Building an interface is a translation from a simple language to a much more complex one. But no matter how skilled a developer is at building an interface, the first step must be always be to understand it. The only way to do that is to become bilingual.&lt;/p>
&lt;hr>
&lt;p>Programmers sometimes talk about “code smells”: surface-level indications that something about a program isn’t right. Sometimes a code smell is just a hunch; not something that can be identified straight away, but a loose thread that reveals a structural flaw when pulled. Much like the nasal assault that any parent of a small child is familiar with, a good programmer knows a foul smell indicates something dirty has happened and must be cleaned up sooner rather than later.&lt;/p>
&lt;p>In the same vein, an interface developer should have a nose for smells in the design she is working with. Just as a good linguistic translator would not translate mistakes between languages, a good interface developer should not recreate design flaws in her code.&lt;/p>
&lt;p>Design smells often manifest themselves through the need to write one-off rules that only apply to a fixed set of circumstances, instead of code that is more universal and flexible. If a mockup specifies page margins of 80 pixels instead of the usual 60, does that mean that the default page margins should be updated everywhere? Is there something special about this type of page that means others like it should also have an 80 pixel margin? Or is it simply a mistake that should not be translated into code at all?&lt;/p>
&lt;p>Design smells are often the result of mistakes made by designers, or at least indications that a design is unclear and needs further thought. But having a nose for these smells is not about picking holes in other people’s work, it’s about fixing small problems early so they don’t become bigger problems later on. Contrary to the ancient wisdom of the playground, it doesn’t matter who dealt it, as long as someone smelled it.&lt;/p></description></item><item><title>A primer on BAT, the cryptocurrency aiming to fix online advertising</title><link>https://jshakespeare.com/a-primer-on-bat-the-cryptocurrency-aiming-to-fix-online-advertising/</link><pubDate>Wed, 03 Jan 2018 13:20:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/a-primer-on-bat-the-cryptocurrency-aiming-to-fix-online-advertising/</guid><description>&lt;p>Online ads account for a staggering amount of the data that our browsers consume. They increase load times by an average of five seconds per page viewed on a mobile phone. By &lt;a href="https://medium.com/@robleathern/carriers-are-making-more-from-mobile-ads-than-publishers-are-d5d3c0827b39">some accounts&lt;/a>, downloading ads and trackers costs the average user up to $23 per month in data fees alone.&lt;/p>
&lt;p>Behind the scenes, ad exchanges like AppNexus and Google DoubleClick suck up and analyze personal information from users’ online behavior, such as browsing history, device information and location data. These data can often be combined with identifying personal information like email addresses and Facebook account IDs to build a terrifyingly complex profile of a user. User data are often sold and resold, sometimes to companies not even in the business of advertising online.&lt;/p>
&lt;p>Unsurprisingly, users are increasingly turning to ad and tracker-blocking software like AdBlock and Privacy Badger. This hurts publishers, who don&amp;rsquo;t receive payment from ad exchanges if their page loads without ads. Publishers retaliate by setting up ad-blocker-blockers, forcing users to whitelist their site in order to access its content. Worse yet, &lt;a href="https://www.engadget.com/2016/02/12/rip-adblock-plus/">some providers&lt;/a> of ad-blocker software will whitelist sites for users in exchange for money, making them de facto gatekeepers of what ads a user does and doesn’t see.&lt;/p>
&lt;p>The situation isn’t much better for advertisers, either. Digital advertising fraud costs marketers and advertisers billions of dollars a year, as scammers use botnets and click farms to inflate traffic figures and charge for ad impressions that are never seen by real customers. There’s little impetus to combat this fraud from the ad exchanges, who make just as much money from fraudulent impressions as genuine ones.&lt;/p>
&lt;p>The world of online advertising is a mess. It’s a bad deal for users, advertisers and publishers. And the only reason this broken system still exists is because historically it’s the only way that publishers have been able to remain solvent.&lt;/p>
&lt;p>All this is not to say that alternatives have not been tried. Paywalls, usually broached out of desperation by beleaguered newspapers, are seen as the primary means by which to bolster or replace revenue from ads. While paywalling does at least ensure that readers pay for what they read, ad-blocker or no, it can discourage new readers and frustrate existing ones who browse the web on an ever growing number of devices. Frustratingly, paywalled content is often served with ads anyway.&lt;/p>
&lt;p>Recently, a third revenue stream has proved an unlikely success for some online publishers: voluntary donations from users. In 2014, The Guardian, faced with falling ad revenue, made a plea to its readers. Instead of putting up a paywall, which would go against the paper’s idea of a free and open internet, they asked for grassroots support in the form of voluntary memberships. It was the carrot to the paywall&amp;rsquo;s stick, and it worked. Since then, The Guardian has grown its paying membership to 300,000. Despite comprising less than 0.01% of the site’s &lt;a href="https://advertising.theguardian.com/audience">34.7 million monthly visitors&lt;/a>, The Guardian&amp;rsquo;s revenue from members now &lt;a href="https://www.theguardian.com/membership/2017/oct/26/together-we-are-safeguarding-the-guardians-independent-journalism">exceeds that of advertising&lt;/a>.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/guardian-donations-plea.png"
alt="A screenshot of The Guardian homepage"/>&lt;figcaption>
&lt;p>The Guardian&amp;rsquo;s plea for support. The company now earns more from reader donations than from advertising revenue&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Grassroots support like this is becoming increasingly popular online. Fans routinely tip Youtubers and Twitchers using Patreon, and sites like Kickstarter and GoFundme allow users to crowdfund everything from new board games and watches to medical bills and political campaigns. Former employees of Gawker, bankrupt since losing a high-profile court case last year, are asking people to chip in for the media organization&amp;rsquo;s revival in a $500,000 &lt;a href="https://www.kickstarter.com/projects/2136064924/save-gawkercom?ref=discovery">Kickstarter campaign&lt;/a>. If it succeeds, they claim, it will return as &amp;ldquo;an ownerless, advertiser-less, non-profit media organization”.&lt;/p>
&lt;p>So we have reached a point where: a) an ad-blocker is a necessity for browsing the web securely, and b) a major news site can rely more on revenue from its readers than from advertising. Does this mean online advertising face a natural demise? Yes and no. At the very least, it is ripe for disruption. Advertising isn’t bad per se, it’s just been very badly implemented on the web so far. Rather than scrap online advertising entirely, it is the technology behind it that needs to be rethought. If the ad exchanges and data merchants are cut out of the deal, then perhaps a more equitable balance of privacy and profitability can be restored amongst users, publishers and marketers.&lt;/p>
&lt;p>It might seem ironic then the man leading this effort to rethink adtech would be Brendan Eich, the inventor of Javascript. Without Javascript, online advertising would not exist. It is the only programming language that can run in your browser, so it’s the technology responsible for injecting ads and sending data back to ad exchanges.&lt;/p>
&lt;p>Eich is spearheading the creation of Brave, a web browser with a focus on privacy and security. Brave’s major selling point to users is that it blocks ads and trackers by default. The project is a response to what &lt;a href="https://www.brave.com/how-to-fix-the-web/">Eich calls&lt;/a> a “primal threat” to the web:&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;I contend that the threat we face is ancient and, at bottom, human. Some call it advertising, others privacy. I view it as the Principal-Agent conflict of interest woven into the fabric of the Web.” - Brendan Eich&lt;/p>
&lt;/blockquote>
&lt;p>But instead of just being another piece of ad-blocking tech, Brave aims to reinvent the advertising model altogether to make a fairer deal for publishers and advertisers as well as users. The browser ships with Brave Payments, a wallet that lets users pay publishers as they browse, based on a voluntary monthly budget.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/brave-payments-wallet.png"
alt="Brave Payments wallet screenshot showing BAT balance and attention breakdown by percentage"/>&lt;figcaption>
&lt;p>The Brave Payments wallet. Sites eligible to receive donations are listed in order of how much time the user has spent visiting them. A green checkmark indicates that site has signed up to receive payment&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Brave users fund their wallet with a cryptocurrency called Basic Attention Token (BAT). The premise of BAT is that user attention has an intrinsic value that drives the digital economy. BAT aims to be a monetary representation of this attention, whether it is to editorial content, video, advertising, games, and so on. At the time of writing, 1 BAT is worth roughly 0.50 USD.&lt;/p>
&lt;p>Once a Brave user has funded their wallet they decide how much money they are willing to donate to publishers every thirty days, from 25 to 100 BAT. Each payment cycle, BAT micro-donations are sent directly to publishers’ wallets, either at a fixed amount per month or based on the proportion of time the user has spent on that site relative to others.&lt;/p>
&lt;p>The best thing about Brave’s payment model is that it doesn’t require publishers to make any binary decisions about how they want to be funded. Sites can continue to serve regular ads to non-Brave users and receive BAT from Brave users at the same time. In fact, BAT that has already been paid to publishers is currently held in escrow, meaning a site need only &lt;a href="https://publishers.basicattentiontoken.org/">verify ownership&lt;/a> of their website and set up a BAT wallet to receive all back-dated payments (unclaimed BAT will eventually be returned to the user growth pool to be distributed into the ecosystem by other means). If the Brave project is successful, ad revenue for publishers will gradually shift from dollar payments in traditional adtech to BAT from users and advertisers.&lt;/p>
&lt;p>In phase two of Brave’s development roadmap, Brave users will be able to opt-in to seeing safe ads as they browse. The Brave wallet will then go from being unidirectional (currently you can only fund your wallet and pay publishers from it) to bi-directional, allowing advertisers to pay BAT to publishers for placing ads on their site and to users for viewing those ads. Using hypothetical numbers as an example, an advertiser would pay 1 BAT per impression of a banner ad, of which 0.15 BAT would be paid to the user and 0.7 BAT paid to the site it was loaded on (Brave will take the remaining cut as payment for their role in delivering the ad, so not &lt;em>all&lt;/em> the middle men are cut out). Once the user has loaded ten pages containing this ad, they will have accrued 1.5 BAT, or around $0.75 at today’s conversion rates, in their Brave wallet. This credit would be automatically donated to publishers or content creators of the user’s choice via Brave Payments.&lt;/p>
&lt;p>Whether or not a user chooses to see ads could be a useful toggle depending on context. The user could opt-in to seeing ads while she is browsing the web at home on a wifi connection and be paid BAT for each ad impression. Later, she could redeem those BAT in ad-free mobile browsing when she is out of the home and has limited battery power or data available.&lt;/p>
&lt;p>The ads that Brave users can opt in to seeing will be different to the kind of ads we’re used to. Brave will use its own ad exchange, which will serve only carefully vetted, lightweight, UX-compliant, non-snooping ads. The ads will be served from a local ad cache, so there is no need for them to send any data remotely, nor will they rely on third party information about the user in order to serve ads in a relevant way. Since the user already trusts their browser, it is not a violation of privacy for the Brave ad exchange to use local data such as browsing history. Even analytics data about an ad’s impressions and click-through rates, metrics that are vital for advertisers in order to know the effectiveness of a campaign, will maintain the user’s anonymity through the user of zero-knowledge proofs.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/bat-diagram.png"
alt="The user/publisher/advertiser payment model on which BAT is based. From https://basicattentiontoken.org/"/>&lt;figcaption>
&lt;p>The user/publisher/advertiser payment model on which BAT is based. From &lt;a href="https://basicattentiontoken.org/">https://basicattentiontoken.org/&lt;/a>&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Because BAT is not tethered to Brave, it could have many applications outside of web browsing. If successful, we could see its use in on-demand streaming services or in video games, where BAT accrued by watching interstitial or in-game ads could be redeemed against subscription fees. It is this open-endedness that makes the project such an exciting prospect.&lt;/p>
&lt;p>In many ways, BAT is the perfect use-case for a cryptocurrency. It is decentralized, allowing users, publishers and advertisers to pay each other directly without middle men. It is not tied to any particular country, so international payments aren’t an issue if you&amp;rsquo;re browsing content from another country. And as a publicly traded asset, its value in dollar-terms will be decided by a free market over time. This is important because in the current system, the dollar value of user attention is hard to calculate due to the sheer number of parties profiting from it. The value of the BAT will absorb these parities shares as they are gradually removed from the equation.&lt;/p>
&lt;p>Although the BAT is traded on &lt;a href="https://coinmarketcap.com/currencies/basic-attention-token/">cryptocurrency exchanges&lt;/a>, much of its current price is speculative&amp;ndash; we won’t begin to understand the true dollar-value of a BAT until Brave’s ad-exchange is launched and the three-way user/advertiser/publisher triangle is complete. This value will be derived from myriad factors such as online publishing overheads, the monetary value that users ascribe to their time spent online, the cost of advertising and how it converts to revenue, and how highly users value privacy and reduced computing strain and battery use.&lt;/p>
&lt;p>BAT sits at the convergence of a number of trends. For publishers, revenue from online advertising will continue to decline, in large part due to Google and Facebook’s dominance over worldwide ad spend (more than two-thirds of global ad spend growth from 2012 to 2016 came from those two companies). At the same time, revenue from member-supported payment models and voluntary donations will increase. Finally, the increasing invasiveness of ads will result in more and more browser and OS vendors protecting their users with increasingly strict blocking technology. In short, the online advertising world is changing fast, and Eich’s team is well placed to take advantage of that.&lt;/p>
&lt;p>The goal of making BAT a store of worldwide user attention is certainly a lofty goal, but it&amp;rsquo;s plausible that it could at least capture a significant chunk of the digital advertising market. Here’s how I think that will happen:&lt;/p>
&lt;ol>
&lt;li>Brave’s user base will continue to grow organically as they release more features and users become better educated about the privacy concerns of current adtech.&lt;/li>
&lt;li>Brave will launch its ad exchange (some time in 2018), causing user interest to spike significantly, especially amongst cryptocurrency circles. The release of Brave ad-replacement browser extensions for Chrome and Firefox will also contribute to its popularity.&lt;/li>
&lt;li>As the number of users blocking ads via Brave or its extensions grows, so will the number of users making voluntary donations via Brave Payments. At the same time, BAT will increase in value against the dollar, giving publishers and content creators (like Youtubers and Twitch gamers) greater incentive to accept BAT payments.&lt;/li>
&lt;li>Once Brave owns a sufficient share of browsers and ad-blockers, advertisers will be forced to use Brave’s ad exchange or miss out on a highly valuable demographic of tech-savvy cryptocurrency nerds and early adopters.&lt;/li>
&lt;li>Eventually, Brave will own a large enough market share that more mainstream publishers and advertising brands will join in, although it’s likely that the biggest of them will hold out or even &lt;a href="https://www.wired.com/2016/04/brave-software-publishers-respond/">fight Brave&lt;/a> for some time to come.&lt;/li>
&lt;/ol>
&lt;p>It’s going to be an interesting ride for Brave. There is certainly a need for better protecting users as they browse the web, and the rampant privacy violations of current adtech mean that something has to give. They will almost certainly face resistance from the giants of online advertising, and probably a fair amount from publishers who currently rely on them to keep the lights on.&lt;/p>
&lt;p>Whether or not BAT is successful depends on two factors: the popularity of Brave and the success of cryptocurrencies as a concept. But Brave’s mission of increasing user privacy is a noble one, and it deserves to succeed.&lt;/p></description></item><item><title>Why you should keep a diary</title><link>https://jshakespeare.com/why-you-should-keep-a-diary/</link><pubDate>Sat, 16 Dec 2017 15:56:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/why-you-should-keep-a-diary/</guid><description>&lt;p>I&amp;rsquo;ve had varying opinions about keeping a diary throughout my life, ranging from &amp;lsquo;it&amp;rsquo;s for girls&amp;rsquo; at ages c.7-10 years old through to &amp;lsquo;it&amp;rsquo;s very difficult and I&amp;rsquo;m much too busy playing Final Fantasy&amp;rsquo; for most of my life since then. The latter of these sentiments is probably shared by most people, wherein Final Fantasy is replaced by sleeping, working or ingesting recreational drugs.&lt;/p>
&lt;p>But while an adolescence spent playing RPGs has left me with little more than an appreciation for well-distributed XP and comically large swords, five years of semi-regular diary-keeping means I now own an archive of my life that I prize among my greatest possessions. What&amp;rsquo;s more, the act of writing a diary is a low-effort, zero-cost form of therapy that anyone can benefit from.&lt;/p>
&lt;h2 id="why-keep-a-diary">Why keep a diary?&lt;/h2>
&lt;h3 id="improved-memory-retention">Improved memory retention&lt;/h3>
&lt;p>I started keeping a diary after becoming frustrated with how easily I would forget recent events. Every Monday morning, people at work would ask me what I did at the weekend while we stood around in the kitchen. I would always stand there looking vacant while I desperately sorted through the murky depths of my brain before exclaiming “I took a shower on Saturday!”.&lt;/p>
&lt;p>A diary is a more reliable and less degradable record of events than memory will allow for. But more importantly, the &lt;em>act&lt;/em> of recording events can help you better retain them mentally. The process of recalling and organizing events after they have happened aids with a process called memory consolidation, the transfer from short-term to long-term memory. If we don&amp;rsquo;t deliberately recall the otherwise unremarkable events of the day, we forget them.&lt;/p>
&lt;h3 id="routine-mindfulness">Routine mindfulness&lt;/h3>
&lt;p>Taking any time out of your day to be mindful is good for you, and keeping a diary is one easy way to do this. It forces you to stop and recap, even if only at a superficial level, what happened to you in the past 24 hours. One of the pleasant side effects of documenting your day is that it is often done in an environment more likely to promote mindfulness than that in which the event took place. You may look back on an experience shared with other people and have an entirely different perspective when you&amp;rsquo;re alone and able to reflect more deeply.&lt;/p>
&lt;p>This becomes especially useful in avoiding destructive habits and promoting positive ones. If you notice that every few weekends for the past six months you have written &amp;lsquo;woke up late and felt miserable that I had wasted the day&amp;rsquo;, you are able to identify a cause and effect relationship that you can begin to counteract.&lt;/p>
&lt;h3 id="a-record-of-past-selves">A record of past selves&lt;/h3>
&lt;p>One of the most delightfully simple reasons to keep a diary is that you have a unique set of experiences that no one else can write about. Each day you can record what kind of a person you are at this exact point in time, and to be able to revisit that person when you&amp;rsquo;re older is a truly valuable asset. In an essay entitled “On Keeping a Notebook”, Joan Didion said:&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Remember what it was to be me: that is always the point… We forget all too soon the things we thought we could never forget. We forget the loves and the betrayals alike, forget what we whispered and what we screamed, forget who we were.”&lt;/p>
&lt;/blockquote>
&lt;p>Leaving your future perception of your current self to the haziness of memory is unreliable. The seemingly insignificant thoughts and events that fall through the cracks are often the ones that paint the most accurate picture of who we were, not the extreme highs and lows that stick in our mind precisely because they were out of the ordinary.&lt;/p>
&lt;p>&amp;ldquo;How we spend our days is, of course, how we spend our lives&amp;rdquo;, wrote Anne Dillard, and if you believe that then you must record the dramatic and the mundane events alike.&lt;/p>
&lt;h3 id="good-practice">Good practice&lt;/h3>
&lt;p>To quote a third literary titan, Virginia Woolf wrote of diary-keeping that &amp;ldquo;the habit of writing thus for my own eye only is good practice. It loosens the ligaments”.&lt;/p>
&lt;p>It is incredibly liberating to write something that will never be read by anyone else. It offers a chance to play with language, to experiment with new words we aren&amp;rsquo;t quite sure how to use yet, to try a different voice or just write sloppily. No editing is required and there&amp;rsquo;s no need to think about audience or worry about pleasing or offending anyone. There&amp;rsquo;s no deadline and no external pressures whatsoever. Thoughts can pour, honest and unfiltered, straight from the mind to the page. How often do we get to write like that?&lt;/p>
&lt;h2 id="how-to-keep-a-diary">How to keep a diary&lt;/h2>
&lt;p>There are many ways you can keep a diary. You can keep notes on your computer, write in a book, or create audio memos on your phone. The only criteria are that you record them privately and habitually.&lt;/p>
&lt;p>I use a service called &lt;a href="https://dabble.me/">DabbleMe&lt;/a>, which sends me an email every day to which I reply with a diary entry. As someone who is slightly bothered about having emails lingering in my inbox, this is a helpful nudge for me to write on a regular basis. I can browse past entries on the DabbleMe website, and export my diary into a text file if I ever decide to switch to another format. Every DabbleMe email contains a past extract, which provides a small dose of either nostalgia or helpful reflection depending on the content.&lt;/p>
&lt;p>For me, a good diary-keeping habit means replying to one of these emails, with anything from one sentence to a couple of paragraphs, at least four days a week. Obviously there&amp;rsquo;s room for improvement there in frequency and quantity, but I have found that forcing myself to a higher standard of diary-writing consistently results in my abandoning the whole thing. I recommend starting small so as to increase the likelihood of building a routine.&lt;/p>
&lt;p>I find diary-keeping to be most helpful when I set aside time for it every day. This helps build a routine that makes writing feel almost effortless. I also try to write soon before I go to bed, as it provides an opportunity to collate and analyze the thoughts swirling around my brain before going to sleep. What&amp;rsquo;s more, I find that nightly diary entries are more detailed and therefore more helpful to me when I revisit them later. After 24 hours I have usually forgotten the small details and fleeting emotions that paint the best picture of my day. If I miss several days and try to catch up on my diary, my memories are filtered through my current mood and the priorities of the moment, and aren&amp;rsquo;t a true record.&lt;/p>
&lt;p>You don&amp;rsquo;t need to be a good writer and you don&amp;rsquo;t need to be doing anything particularly remarkable with your life to keep a diary. It&amp;rsquo;s a form of therapy that only costs you ten minutes per day and pays dividends in the future. Fire up a text editor and write your first entry today.&lt;/p></description></item><item><title>Encoding browser-friendly video files with ffmpeg</title><link>https://jshakespeare.com/encoding-browser-friendly-video-files-with-ffmpeg/</link><pubDate>Mon, 04 Dec 2017 22:17:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/encoding-browser-friendly-video-files-with-ffmpeg/</guid><description>&lt;p>There are only two &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats">file types&lt;/a> required to natively play video in any modern browser with HTML5: WebM and MP4. Currently &lt;a href="https://caniuse.com/#search=mp4">all major browsers support MP4&lt;/a>, but WebM allows for a much better quality-to-filesize ratio. Using &lt;a href="https://www.ffmpeg.org/">ffmpeg&lt;/a> you can transcode any multimedia file into WebM and MP4 from your command line.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/webm-comparison.jpg"
alt="Screenshot from OSX showing the relative file sizes of two files encoded with ffmpeg"/>&lt;figcaption>
&lt;p>The same file transcoded to H.264-MP4 and VP9-WebM with the same bitrate. The WebM filesize is 40% that of the MP4.&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Ffmpeg is a hugely powerful tool. It supports just about every codec going and has a relatively simple interface. With that said, it does require you have at least a basic knowledge of multimedia encoding, so it can be a little daunting if you’re just looking to create the necessary files to use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video">&lt;code>&amp;lt;video&amp;gt;&lt;/code>&lt;/a> tag on your site. Here’s a step by step guide to getting the files you need for any browser to natively play your video.&lt;/p>
&lt;h2 id="install-ffmpeg">Install ffmpeg&lt;/h2>
&lt;p>On OSX, use &lt;a href="https://brew.sh/">homebrew&lt;/a> to build ffmpeg on your machine. Compiling ffmpeg with every available codec takes a long time, so we’ll just build with the three codecs we need:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">brew install ffmpeg --with-libvpx --with-libx264 --with-libvorbis
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This can still take a fair bit of time (it took 15 minutes on my computer), so go and have a stretch or call your Grandma.&lt;/p>
&lt;p>If you&amp;rsquo;re wondering what the &lt;code>--with&lt;/code> flags mean:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://www.webmproject.org/code/">LibVPX&lt;/a> is a library that contains implementations of the &lt;a href="https://en.wikipedia.org/wiki/VP8">VP8&lt;/a> and &lt;a href="https://en.wikipedia.org/wiki/VP9">VP9&lt;/a> encoders (hence VP&lt;em>X&lt;/em>) that create WebM video files. (We will only be using VP9).&lt;/li>
&lt;li>libvorbis contains an implementation of the &lt;a href="https://xiph.org/vorbis/">Ogg Vorbis&lt;/a> encoder that is used for encoding audio tracks in the WebM container.&lt;/li>
&lt;li>&lt;a href="https://www.videolan.org/developers/x264.html">libx264&lt;/a> is a codec from VideoLAN (who make VLC) that lets us encode H.264 video and audio into the MP4 container.&lt;/li>
&lt;/ul>
&lt;h2 id="create-the-webm-file">Create the WebM file&lt;/h2>
&lt;p>&lt;code>cd&lt;/code> into the directory containing your source/input video file and run the following command, replacing the filenames appropriately:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">ffmpeg -i my-original-video.wmv -f webm -vcodec libvpx-vp9 -vb 1024k my-new-video.webm
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There’s a bit going on here, so let’s break it down while you wait for your video to transcode.&lt;/p>
&lt;ul>
&lt;li>&lt;code>my-original-video.wmv&lt;/code> is the filename of your source file. You can use any type of video file (AVI, WMV, MP4, etc.)&lt;/li>
&lt;li>&lt;code>webm&lt;/code> is the container format we want to use for the output file (do not change this)&lt;/li>
&lt;li>&lt;code>libvpx-vp9&lt;/code> is the video codec library we will be using for the transcoding. VP9 is the specific encoder.&lt;/li>
&lt;li>&lt;code>1024k&lt;/code> is the bitrate that the output video will have. You may want to tweak this value depending on the desired quality of the video. Higher bitrates mean bigger files, which mean longer buffer times.&lt;/li>
&lt;li>&lt;code>my-new-video.webm&lt;/code> is the name of the output file.&lt;/li>
&lt;/ul>
&lt;p>By default, the audio track will be transcoded to the output file using the libvorbis encoder. If you want to change the bitrate of the audio you can do so with the &lt;code>-ab&lt;/code> flag, e.g. &lt;code>-ab 128k&lt;/code>.&lt;/p>
&lt;p>If you wish to output a silent video, then you should pass the flag &lt;code>-an&lt;/code> to transcode the video without an audio track. This must go immediately before the output file name, e.g. &lt;code>[...] -an my-new-video.webm&lt;/code>. This is useful for adhering to &lt;a href="https://webkit.org/blog/6784/new-video-policies-for-ios">iOS Webkit&amp;rsquo;s&lt;/a> requirements for supporting the HTML5 video &lt;code>inline&lt;/code> and &lt;code>autoplay&lt;/code> attributes.&lt;/p>
&lt;h2 id="create-the-mp4-file">Create the MP4 file&lt;/h2>
&lt;p>Now run:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">ffmpeg -i my-original-video.wmv -vcodec libx264 -f mp4 -vb 1024k -preset slow my-new-video.mp4
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The flags here are similar, except we’re using the libx264 codec to create a video with an MP4 container. The &lt;code>-preset slow&lt;/code> flag increases the encoding time but results in a better quality-to-filesize ratio. &lt;a href="https://trac.ffmpeg.org/wiki/Encode/H.264#Preset">You should use the slowest preset that you have the patience for&lt;/a>.&lt;/p>
&lt;p>Again, you can tweak the bitrate flag for video (&lt;code>-vb&lt;/code>) and audio (&lt;code>-ab&lt;/code>), and pass the &lt;code>-an&lt;/code> flag before the output file name if you want to encode the video without an audio channel.&lt;/p>
&lt;h2 id="embed-the-files-with-video">Embed the files with &lt;code>&amp;lt;video&amp;gt;&lt;/code>&lt;/h2>
&lt;p>Now you can reference the files you’ve created within your HTML:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">video&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">source&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">src&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;my-new-video.webm&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">type&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;video/webm&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">source&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">src&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;my-new-video.mp4&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">type&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;video/mp4&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">video&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And that’s it! ffmpeg supports a huge number of options when encoding, transcoding or streaming multimedia, so be sure to refer to &lt;a href="https://ffmpeg.org/ffmpeg.html#Video-Options">the docs&lt;/a> if you need to achieve something that isn’t mentioned here.&lt;/p></description></item><item><title>How Slack hooks users through artificial urgency</title><link>https://jshakespeare.com/how-slack-hooks-users-through-artificial-urgency/</link><pubDate>Fri, 01 Dec 2017 15:30:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/how-slack-hooks-users-through-artificial-urgency/</guid><description>&lt;p>You may have heard someone describe a piece of software as being addictive. As in, &amp;ldquo;I&amp;rsquo;m so addicted to this balloon-popping game on my phone that I got fired from my job as an air traffic controller&amp;rdquo;. As the amount of software vying for our attention has exploded in recent years, companies are increasingly hot-wiring our brain’s reward center to make their product successful. It is no longer enough for an application to be enjoyable or useful, it must also provide us with dopamine-inducing moments that reduce us to cocaine-addicted lab rats mashing a button marked &amp;lsquo;Press here for cocaine’. The software in the air-traffic control tower may be good for landing planes safely, but it’s missing the habit-forming hooks that would otherwise send it to number one in the App Store charts.&lt;/p>
&lt;p>Slack is a good example of habit-forming software. The longer you use it, the harder it becomes to imagine work life without it. This is not because it necessarily improves the way we communicate with our colleagues, but because it fundamentally changes it. Slack attempts to replace the fusty conventions of emails and in-person meetings with a tool that encourages the discussion of Everything Right Now.&lt;/p>
&lt;p>Obviously Slack are onto something here. The average user logs 140 minutes of active usage per weekday&lt;!-- raw HTML omitted -->&lt;a href="https://expandedramblings.com/index.php/slack-statistics/">[1]&lt;/a>&lt;!-- raw HTML omitted --> (four times more than Facebook’s 35 minutes per day&lt;!-- raw HTML omitted -->&lt;a href="https://sproutsocial.com/insights/facebook-stats-for-marketers/">[2]&lt;/a>&lt;!-- raw HTML omitted -->). I have probably spent thousands of hours of my life using it. And yet I have absolutely nothing tangible to show for this. In fact, my feelings of productivity and fulfillment on any given day are often inversely proportional to how much time I have spent using Slack.&lt;/p>
&lt;p>This relationship is worth examining because Slack claims to be &amp;lsquo;Where Work Happens’, an all-in-one tool for workplace collaboration without which we are cursed to spend our days trudging between meetings or exchanging emails. Their website also claims to make you &amp;ldquo;more productive, less stressed, and just a little bit happier”&lt;!-- raw HTML omitted -->&lt;a href="https://slack.com/features">[3]&lt;/a>&lt;!-- raw HTML omitted -->. As with cocaine, this is partly true.&lt;/p>
&lt;p>The simplest explanation for Slack’s popularity is that all your colleagues are using Slack. It’s a bit like the way everyone gravitates towards the kitchen at a house party. It starts off as a suitable place for newcomers to gather while the host makes them a drink, but before long everyone else has joined them and the rest of the house is empty. Individually, everyone in the kitchen knows the living room is a much more comfortable place to sit, but would feel a bit weird and standoffish if they left the social throng that has mysteriously spawned around the bread bin.&lt;/p>
&lt;p>What lures people to Slack in the first place is its ability to manufacture an artificial sense of urgency. There’s no option to send a message on Slack in such a way that says ‘no need to stop what you’re doing, but could you please deal with this at some point?’. Even if you explicitly preface every message with that statement, by the time the recipient has cmd + tabbed away from whatever they were doing to read it they might as well just deal with it now anyway. Sometimes this is good; some jobs require people to juggle lots of time sensitive things and the ability to get an instant response from teammates is valuable to them. But most intra-office communication is not urgent. In fact, a decent chunk of it isn’t even work-related.&lt;/p>
&lt;p>Even if such disruptions frustrate us as recipients, when it’s our turn to communicate something we’re unlikely to switch to email or schedule a meeting. Why would we want our message to go to the bottom of the pile, or have to wait for a vacant slot in both our calendars? Urgency has become the status quo. Like leading someone out of the kitchen at a house party, it changes the tone of the conversation and feels slightly like you’re about to try and make out with them.&lt;/p>
&lt;p>Slack uses two devices to manufacture urgency. The first is the kind of eyeball-grabbing, shouty notifications that you’re used to seeing on social media. But while ignoring shouty Facebook notifications for a few days might mean you miss out on an invitation to a party, the consequences of ignoring Slack notifications could mean you remain blissfully unaware that the website has been hacked and everything is on fire. Unfortunately, the Slack message from your boss saying ‘the website has been hacked and everything is on fire’ looks and sounds identical to the Slack message from the person sitting next to you asking where you want to go for lunch. This makes it hard to filter signal from noise and forces you to be on triage duty all day. The same attention-leaching forces are at play whether you&amp;rsquo;re using Slack for work or socializing.&lt;/p>
&lt;p>The second urgency-generating device is the mostly inconsequential chatter that occurs throughout the workday. These are not conversations that demand your attention by belching notifications and scarlet circles with numbers in them, but the ones you feel you should keep an eye on just in case they affect you or require your input in some way. Slack chatter can be ephemeral and hard to revisit, so you feel compelled to continually check in throughout the day in case you miss out on something important.&lt;/p>
&lt;p>These devices make you feel itchy. It&amp;rsquo;s hard to look away from Slack for very long because there are just so many time-sensitive and essential things that you’re missing out on, even when there aren&amp;rsquo;t.&lt;/p>
&lt;p>Other forms of communication like emails and meetings are not suitable for everything, but they are good at enabling focused communication based on existing schedules. By contrast, Slack encourages us to juggle hundreds of separate conversations, barraging our colleagues with stream-of-consciousness messages and giving little regard to what the recipient is currently doing. Because every message is presented as time-sensitive, and because sometimes those messages are genuinely important, Slack can be extremely tiring to use for everyone involved. When your only tool is a hammer, everyone goes home with severe head trauma.&lt;/p>
&lt;p>If the main reason to use Slack is because it shouts the loudest, should we really be using it? If a competitor were to release something tomorrow that set off an airhorn in the user&amp;rsquo;s face every time someone sent them a message, would that become the new choice of office communication? I’d bet ten bucks and a pair of ear defenders that it would. Slack is undoubtedly useful for some things, and for some people more than others, but it is not the holy grail of workplace communication that it claims to be.&lt;/p>
&lt;p>Slack as a company is not hell-bent on ruining our productivity or stressing us out with feelings of bombardment or FOMO, but they are in the business of making as many people dependent on their product as possible. Sometimes it’s best to step away from Slack, consider what we want to say, and schedule a time to say it. Perhaps then we can actully be more productive, less stressed, and just a little bit happier.&lt;/p>
&lt;h2 id="references">References&lt;/h2>
&lt;ol>
&lt;li>&lt;a href="https://expandedramblings.com/index.php/slack-statistics/">https://expandedramblings.com/index.php/slack-statistics/&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://sproutsocial.com/insights/facebook-stats-for-marketers/">https://sproutsocial.com/insights/facebook-stats-for-marketers/&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://slack.com/features">https://slack.com/features&lt;/a>&lt;/li>
&lt;/ol></description></item><item><title>Learning curves and user interfaces</title><link>https://jshakespeare.com/learning-curves-and-user-interfaces/</link><pubDate>Mon, 14 Mar 2016 23:07:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/learning-curves-and-user-interfaces/</guid><description>&lt;p>When we design an interface for a product there are two questions we should ask before doing anything else:&lt;/p>
&lt;ol>
&lt;li>How much time does the user have to comprehend the product?&lt;/li>
&lt;li>How much potential proficiency should the product offer the user?&lt;/li>
&lt;/ol>
&lt;p>These factors are important because they can be used to plot learning curves: experience on the x axis and proficiency on the y axis. Once we know the shape of the learning curve best suited to the user, we can design an interface that is either more intuitive (easier to learn) or more powerful (offers greater proficiency).&lt;/p>
&lt;p>An interface with a logarithmic learning curve (one that levels out over time) is more intuitive. It enables the user to quickly familiarize themselves with the features of the product. Intuitive learning curves mean less time must be invested to become competent with the product, but in exchange there is a reduction in how proficient the user can become over time. Take, for example, a French press.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/french-press.jpg"
alt="A French press full of coffee"/>&lt;figcaption>
&lt;p>The interface of a French press is intutive, but not powerful (photo by Don LaVange, &lt;a href="https://flic.kr/p/cmthoq">https://flic.kr/p/cmthoq&lt;/a>)&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>The simplicity of a French press&amp;rsquo; interface means that a user can quickly understand the mechanism of the product, how to operate it and can predict what the outcome will be. Pour water and ground coffee into the pot, wait a few minutes, push the top down, pour coffee. The learning curve for a French press looks something like this:&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/logarithmic.png"
alt="A line graph with &amp;#39;experience&amp;#39; on the x axis and &amp;#39;proficiency&amp;#39; on the y axis. The line shows an logarithmic increase in proficiency with experience"/>
&lt;/figure>
&lt;p>The time required to reach competence (making a decent cup of coffee) is fairly short, but the curve quickly flattens off as we reach the maximum potential of the product. There is a little more proficiency available after reaching competence, such as learning to adjust the amount of coffee used or to heat the pot first to reduce cooling time. After this the curve levels off as the user reaches mastery of the product.&lt;/p>
&lt;p>An interface with an exponential learning curve (one that curves upward over time) is more powerful. A powerful interface offers the user more control over a product. It requires a greater time investment to reach competence but offers the user an increased maximum proficiency. In contrast to a French press, consider a café-grade espresso machine.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/espresso-machine.jpg"
alt="A cafe-grade espresso machine"/>&lt;figcaption>
&lt;p>An espresso machine interface offers more power, but requires a greater initial investment of time (photo by Felipe Neves, &lt;a href="https://flic.kr/p/aWEoia">https://flic.kr/p/aWEoia&lt;/a>)&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>To an inexperienced user there’s more to process and this means an increase in the time and brainpower required to make an acceptable cup of coffee. Grind the beans into the portafilter, tamp the coffee, attach and run water through the portafilter, add hot water or milk as desired. The learning curve here would look more like this:&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/exponential.png"
alt="A line graph with &amp;#39;experience&amp;#39; on the x axis and &amp;#39;proficiency&amp;#39; on the y axis. The line shows an exponential increase in proficiency with experience"/>
&lt;/figure>
&lt;p>The added complexity of the interface means there is more potential for inexperienced users to fail than when operating a French press. But with practice the user will be able to pull a good espresso, and from there they can become a more skilled barista than the French press owner as they learn to make cappuccinos, flat whites and other caffeinated delights.&lt;/p>
&lt;p>All user interfaces have learning curves. Manual vs. automatic transmission in cars, elevators vs. escalators, command line vs. GUI. In every case, there is a trade-off between intuitiveness and power that a designer has had to make.&lt;/p>
&lt;p>This trade-off is not necessarily based around the available features of the product itself or even the complexity of the actions a proficient user might make. An interface for a complex piece of machinery might be intuitive but fail to offer an experienced user the ability to perform actions better or more effectively than a less experienced user. Take for example this interface for a cherry picker crane:&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/cherry-picker-interface.jpg"
alt="The control panel of a cherry picker crane"/>&lt;figcaption>
&lt;p>The control panel of a cherry picker crane&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>This crane is obviously a complex piece of machinery, but the interface does an excellent job of making clear how each switch will affect its position. However, the interface is not powerful (in the sense that we are defining it) because an experienced user must still navigate a relatively cumbersome set of switches over a large area, even once they know exactly what each one does.&lt;/p>
&lt;p>But if the complexity of the product is not the deciding factor in an interface’s learning curve, what is?&lt;/p>
&lt;p>The decision of whether to make an interface intuitive or powerful is most heavily influenced by the intended audience of the product. In the example above, the cherry picker will likely be hired out for a single job by a work crew who may have never used the device before. This crew may not have a long time over which to hone their skills, but more often will be learning on the job in an environment that is noisy and full of potential hazards. The designer of the crane recognized this and decided a logarithmic learning curve would be most appropriate.&lt;/p>
&lt;p>We must consider the same factors when building interfaces for digital products. The intended audience may be tech-savvy and time-rich with many months available to master the product. They may relish the opportunity to learn new things and overcome challenges in exchange for expertise and proficiency. But they could also be less technically-minded, time-poor or not sufficiently sold on a product to invest lots of time in using it. They may have complex needs that require lots of time to meet, or they may have simple needs that don’t.&lt;/p>
&lt;p>An obvious example of this in the digital world is the difference between mobile and desktop applications. Not only must we consider differences in device hardware such as screen size and processing power, but also the differences in the context that users will operate those devices. A mobile device user is likely to have less time available and face more distractions from the world around them, but will probably also have fewer needs. For these users, intuitiveness is more important than power. Conversely, a desktop user is likely to have more time to comprehend an interface in the comfort of their home or office and will therefore expect to achieve increasingly complex tasks as their experience with the product increases. In these cases, we can trade in some intuitiveness in the short term for added proficiency in the long term.&lt;/p>
&lt;p>As with all humanistic design, it is important to put the user and their needs first and not simply to crowbar the same solutions into everything we design. Abby Covert writes in How to Make Sense of Any Mess:&lt;/p>
&lt;blockquote>
&lt;p>“What works for kids might annoy older people. What worked five years ago may not work today. We have to think about the effects of adopting an existing structure or language before doing so.”&lt;/p>
&lt;/blockquote>
&lt;p>The cost of misjudging learning curves is frustration for the user. If an interface is intuitive but does not reward experience, the user will move on to another product that lets them be more proficient. If an interface is too hard to learn, users will simply ignore it in the first place.&lt;/p>
&lt;p>Once a learning curve has been established we can more easily make decisions about a digital interface. Should we nest items in this menu so it’s easier to navigate for power users, or will new users struggle to find things? Should we add tooltips to educate new users, or will that be patronizing for existing ones?&lt;/p>
&lt;p>Good interface design means recognizing what solutions are appropriate for which demographics and in which contexts. It means setting aside our assumptions and talking to users about their priorities rather than imposing ours upon them. It means watching people use a product - not just for five minutes but over the course of days, weeks and months. Being aware of learning curves and how they differ across different groups of people means we can build interfaces that make users happy. What’s more important than that?&lt;/p></description></item><item><title>Why good agencies write bad code</title><link>https://jshakespeare.com/why-good-agencies-write-bad-code/</link><pubDate>Sun, 04 Oct 2015 22:46:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/why-good-agencies-write-bad-code/</guid><description>&lt;p>Any seasoned programmer will have some experience of technical debt, the metaphor used to analogize how corner-cutting and skimping on process early in a project’s lifecycle results in a debt that must be paid off later. It is not necessarily a bad thing, but anyone who has inherited legacy code will know the frustrations of working with an indebted project. Sometimes a development team is able to effectively leverage technical debt to their advantage, sacrificing stability and process early on to achieve fast turn-arounds. Think of this a bit like a shopkeeper who must take out a business loan to buy inventory. They know that if they are able to sell that inventory at a profit they will have the capital to pay off that debt later on, along with the interest it accrues along the way.&lt;/p>
&lt;p>I’ve spent most of my career working at small web development agencies. In that time I’ve noticed that code is frequently handed off with a level of technical debt ranging from potentially troublesome to irresponsibly dangerous. Some of those projects have been relatively pain-free to work on, while others have resulted in such horrible messes of code that developers have literally quit their job over them.&lt;/p>
&lt;p>I wanted to understand what it, if anything, makes agencies more susceptible to technical debt than other organizations. To do that, it’s necessary to look at how agencies make money.&lt;/p>
&lt;h2 id="how-agencies-operate">How agencies operate&lt;/h2>
&lt;p>For those unfamiliar with how an agency does business, the lifecycle of almost every project I have worked on at an agency has looked like this:&lt;/p>
&lt;ol>
&lt;li>A client approaches the agency with a set of requirements for the product (ie. an app or website) he wants built&lt;/li>
&lt;li>The designers at the agency design something that meets those requirements&lt;/li>
&lt;li>After n rounds of client feedback and design changes, the client gives the go ahead for the product to be built&lt;/li>
&lt;li>The developers at the agency build the product based on the designs&lt;/li>
&lt;li>After n rounds of client feedback and code changes, the client is satisfied&lt;/li>
&lt;li>The product is launched and the client pays his bill&lt;/li>
&lt;/ol>
&lt;p>There are of course differences from one agency and one project to another, but this is a fairly ubiquitous approach. It’s not inherently tied to a certain timescale so it can be scaled up or down dependent on the project budget or external deadlines. Steps 2-5 can be repeated multiple times throughout the project if the agency in question prefers to work according to an Agile methodology (or some derivative thereof).&lt;/p>
&lt;p>Assuming a healthy flow of business it is in a growing agency’s best interest to complete this lifecycle many times and at a reasonably fast pace. Sometimes clients may wish to retain the agency’s services after launch for support and incremental improvements, but if new work is available those resources are better spent working on the next (bigger and better) project rather than maintaining old ones.&lt;/p>
&lt;p>This is because rapid project turnaround suits both client and the agency alike. The client wants to receive a quality product as cheaply and as quickly as possible, so an agency that can deliver that product in a short timeframe is attractive. For the agency, fast turnaround means they can win progressively more lucrative contracts more quickly - or simply put, grow faster. Each time a project lifecycle is completed the agency earns exposure and reputation, which may also make their services worth more money. Retainer contracts and long projects may guarantee revenue, but they also represent a plateau in growth.&lt;/p>
&lt;h2 id="why-agencies-accrue-technical-debt">Why agencies accrue technical debt&lt;/h2>
&lt;p>By this logic it is actually more pragmatic for a growth-focused agency to deliver projects with high levels of technical debt. While project constraints may be demanding, there is no long term period in which it will be the agency’s responsibility to repay that debt. The agency knows they can turn out debt-laden code because there’s too short of a development time for it to be a problem for them. This is not to say the end product will be bad, just that the codebase is not optimized for a long life span.&lt;/p>
&lt;p>This has an additional benefit for the agency because it means they don&amp;rsquo;t have to hire world-class developers to meet the client’s needs, and that means they can spend less money on salaries. Let&amp;rsquo;s assume that most projects at an average small to medium-sized agency spend less than six months in development. The talent pool of developers who are able to build and deliver a fairly standard website in six months is relatively large and therefore competitively priced. Even if below the surface the website is literally made from spaghetti, it is unlikely it can be complicated enough to cause any of nuclear meltdown if it goes wrong (spaghetti having a relatively long half-life). As long as the website is acceptable to both the client and their users at the time of project handoff, the quality of code is of little concern to the agency from a business perspective.&lt;/p>
&lt;p>Consider on the other hand a website that is in development for two years. If the team responsible for building it does not have a good understanding of design patterns, writing tests and documentation, code reviews, refactoring and other best practices, then the end product will most likely be severely marred, if it even gets shipped without one or more developers winding up in an asylum/prison. Hiring a team of people with that level of experience is much more costly, and yet for an agency that bills by a day rate, brings in the same amount of revenue as four six-month projects that can be completed by less experienced and therefore cheaper developers.&lt;/p>
&lt;p>This is in not an indictment of agencies, but rather a characteristic of the fact that it generally makes better business sense to focus on growth rather than writing immaculate code for every project. I am also not implying that agencies only need to hire junior developers, simply that it is easier from a technical standpoint to successfully deliver a six month project than a two year one.&lt;/p>
&lt;p>After all, agencies offer a service that is typically expensive but finite, and that makes them well-suited to short project lifecycles. Perhaps the project in question is just a proof of concept so the client can seek further investment. Perhaps the project is time-sensitive, like a marketing campaign tie-in, and will be shelved without anyone touching the code again once it is handed off.&lt;/p>
&lt;p>Whatever the reason, agencies are not &lt;em>aiming&lt;/em> to write debt-laden code, nor are they trying to con non-technical clients who don’t know any better. It’s just that the client would usually rather pay less and get the product faster by accepting something that is not optimized for further development.&lt;/p>
&lt;p>In other words, the end product is cheaper (or delivered sooner) &lt;em>because&lt;/em> it comes with some degree of debt built into it.&lt;/p>
&lt;h2 id="how-agencies-accrue-technical-debt">How agencies accrue technical debt&lt;/h2>
&lt;p>So far I’ve argued that growing agencies are generally better off taking on short term projects, and that it is acceptable for those projects to be delivered with some degree of technical debt. But what happens when that agency reaches a point at which short projects no longer make financial sense? So far I have used the six month heuristic to delineate projects between simple and complex, so what if an agency lands a year-or-more long contract?&lt;/p>
&lt;p>In these cases, technical debt can not be allowed to accumulate at the same rate as shorter term projects. This is for the same reason that compound interest makes people in financial debt get exponentially poorer. The quality and speed of development will curve increasingly downwards until either the codebase is unsustainable or the product is unusable.&lt;/p>
&lt;p>There are of course many agencies that undertake projects of vast complexity with contracts spanning multiple years, so what are they doing to minimize technical debt?&lt;/p>
&lt;p>In my experience there are two reasons that agencies accrue debt faster than other organizations.&lt;/p>
&lt;h3 id="1-biting-off-more-than-developers-can-chew">1) Biting off more than developers can chew&lt;/h3>
&lt;p>This is caused by an agency either trying to grow too fast; lack of communication between the team responsible for winning new business and the development team; or both.&lt;/p>
&lt;p>If a company is pitching for atypically complex projects, technical debt is going to be a sure thing. In fact, in this scenario technical debt is really a euphemistic way of saying the project will be somewhere on the scale of ‘unacceptably difficult to ‘the client will end up firing/suing you and all your developers will commit seppuku’. It’s analogous to the hungry diner at a buffet having eyes bigger than his stomach and overloading his plate. At first it may be tempting to load up 3 platefuls of tempura, but at some point in the future you&amp;rsquo;re going to be handed a bill to get vomit cleaned out of the back seat of your friend’s Toyota Prius and it will be expensive.&lt;/p>
&lt;p>The reason these longer term projects have the potential to cause problems is because the development team will have become accustomed to, and ideally good at, moving fast to meet short turnarounds. If the same approach is taken on a longer term project, the level of technical debt will move past the acceptable limit and some serious issues will arise. Of course the solution to this is seemingly simple - pay off the debt as necessary throughout the duration of the development period. But if an agency has a development team with no experience of dealing with technical debt, this is much easier said than done.&lt;/p>
&lt;p>This is an especially common problem at agencies specifically because of the focus on growth that I mentioned earlier. Winning an atypically lucrative pitch can seem like a ticket to the big time. Any gap between the dev team’s current abilities and those required to complete the project are dismissed with vague notions of learning on the job and hiring contractors to fill the gaps, whereas the bottom line is that the agency is promising to fulfill work it is not yet qualified to deliver. This by no means equals guaranteed failure, and at some level every agency will have to make these leaps from time to time under the rallying cry of &amp;lsquo;fake it ’til you make it’. But if an agency typically works on short-term projects and out of the blue puts its developers to task on a year long contract, some degree of behavioral shift will be required.&lt;/p>
&lt;p>If you consider a leap in project complexity (and thus financial value) to be a form of instant capital that comes at the price of technical debt, then you start to see how important it is to grow at a reasonable rate. The agency can then weigh the lucrative nature of a big contract against potential problems further down the line, and pitch only for projects that will result in an acceptable level of spaghetti code and developer Seppuku throughout the project lifespan. Over time, that duration will increase.&lt;/p>
&lt;p>Biting off more than you can chew is a problem with management at the company level. Another root of technical debt at agencies lies in management at the project level.&lt;/p>
&lt;h3 id="2-making-too-many-assumptions-about-client-understanding">2) Making too many assumptions about client understanding&lt;/h3>
&lt;p>Everyone knows that assumptions make an ass out of ‘u’ and ‘mptions’. But before I talk about why assumptions can lead to technical debt, I want to clarify what they are.&lt;/p>
&lt;p>During a software project (at an agency, startup or enterprise), two types of assumption will be made: reasonable assumptions and lazy assumptions. It’s important to recognize when each type is being made and to reduce the likelihood of false assumptions causing problems later on.&lt;/p>
&lt;p>Reasonable assumptions are ones that are either not sufficiently risky or cost too much to test now and so are taken as gospel until it becomes sufficiently inexpensive to prove (or disprove) them later. In software development, knowing that a reasonable assumption exists means code can be written in such a way that will make it easy to change later should the assumption prove to be false.&lt;/p>
&lt;p>Lazy assumptions are cheap to test now but are not addressed because of poor management. They can be resolved through something as simple as good, clear communication, but the longer they remain unproven, the more expensive they become. Should they prove to be false at a later date, they can be highly problematic and expensive to fix.&lt;/p>
&lt;p>Imagine a designer working on the design for an e-commerce checkout. They get to designing the buttons for progressing through the checkout. In a world free of assumptions, they would already have the user data that says ’the highest checkout completion rates are observed when buttons are pink and have a 4px border radius’, and their job would be a lot simpler. But data at that level of detail is almost never available prior to launch, so the designer makes an assumption about what will engage users and makes the button bright red instead. Later on that button styling may change as more data becomes available about user behavior, but for now it would be too costly and time-consuming to gather that data.&lt;/p>
&lt;p>This is an example of a &lt;em>reasonable&lt;/em> assumption, and designers use them all the time at agencies and elsewhere. Reasonable assumptions are often made under the Lean Startup methodology, which prioritizes launching an imperfect product faster than a perfect one. Reasonable assumptions can be proved or disproved after the product launches and metrics and data become available that make it cheaper to do so.&lt;/p>
&lt;p>Conversely, consider this exchange between an agency project manager and their client:&lt;/p>
&lt;p>Client: &lt;em>“We&amp;rsquo;re noticing the homepage has laggy scrolling on older mobile phones, we need you to fix this right away&amp;quot;&lt;/em>&lt;/p>
&lt;p>Product Manager: &lt;em>“But you asked us to use a canvas animation for the background of that page, older phones don’t have the graphics power to cope with that and scroll smoothly&amp;quot;&lt;/em>&lt;/p>
&lt;p>Client: &lt;em>“Well we didn’t realize that would cause slow scrolling, you’ll have to change it&amp;quot;&lt;/em>&lt;/p>
&lt;p>This is an example of an agency making a &lt;em>lazy&lt;/em> assumption, in this case one made about the client’s technical knowledge. Had it been established earlier on that the request for an animated background would cause poor performance on older devices, the client could have voiced their concern earlier and the development team could have built a better solution. Instead, they must now go back and retroactively solve this problem.&lt;/p>
&lt;p>This kind of assumption is one that agencies are highly prone to making. They often occur because what the client sees at the design stage is an unrealistic portrayal of the end product, or because the project is rushed into the development phase without establishing a specification that everyone can understand and agree to. As I established earlier, this doesn’t necessarily cause problems in projects of shorter lifespans when a client may be happy to skip some of the formalities to get to a working product faster. But for longer, more complex projects, when lazy assumptions are left to fester they can cause big problems later on.&lt;/p>
&lt;p>False assumptions are problematic because they have a nasty habit of coming to light later in the project’s lifecycle, when the number of hours remaining on the project does not always allow for changes to be implemented in an optimal way. In some cases, a project may be so heavily based on assumptions that the codebase cannot accommodate a fundamental change should those assumptions prove to be false.&lt;/p>
&lt;p>The more false assumptions that must be accounted for late in a project’s development, the higher the likelihood of technical debt accruing.&lt;/p>
&lt;figure>&lt;img src="https://jshakespeare.com/post_images/technical-debt-flow-chart.jpg"
alt="The more revisions are made, the greater the likelihood that corners will be cut, as project time and budget are reduced with each iteration."/>&lt;figcaption>
&lt;p>The more revisions are made, the greater the likelihood that corners will be cut, as project time and budget are reduced with each iteration.&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>Avoiding assumptions during a project at an agency means emulating as accurately as possible the end product before construction begins. By capturing the less tangible aspects of a product, such as UX and performance, the client’s vision for the product will align more closely with the agency’s and there will be fewer surprises when the client starts using the product for the first time. Good communication and prototyping are the two things I consider to be most crucial to closing the assumption gap between agency and client.&lt;/p>
&lt;p>While reasonable assumption-making can be a valid alternative to early stage research, it is essential that agencies be aware of lazy assumptions they are making too. In particular, assumptions should never be carried into the development phase if they can be proven or disproven simply by better communication with the client. Weighing up the cost of assumptions against their potential to accrue technical debt and other problems later on is an important part of effectively managing any software project, but this is especially important at an agency where the understanding of the developers, users and clients must align as closely as possible.&lt;/p>
&lt;h2 id="common-yes-unavoidable-no">Common? Yes. Unavoidable? No.&lt;/h2>
&lt;p>Ultimately, a development project at an agency isn’t so very different from a development project anywhere else, and for the most part technical debt can be treated the same. While technical debt may be more likely to accrue in an agency environment (especially fast-growing ones) due to the nature of how they make money, it is not unavoidable. Being aware of technical debt and understanding how to reduce and strategically increase it can give agencies a powerful tool in tackling projects. Instead of letting debt spiral out of control and ruin a codebase - and possibly reputation, it can be leveraged in such a way that suits the timescale and complexity of a project for all parties involved.&lt;/p>
&lt;p>But avoiding technical debt means being aware of its causes, and it’s worth noting that both the causes I have outlined above are present before a single line of code has been written. The key to avoiding technical debt is understanding the nature and limitations of the agency model. Understanding that because you can win a big pitch doesn’t mean you’re able to complete the work well. Understanding that a little time spent prototyping can save a lot of time in code clean up. Understanding that client relationships require as much communication as possible to stay on the same page.&lt;/p>
&lt;p>Understand these factors, and you can understand where technical debt comes from. Understand where it comes from, and you can be free of it.&lt;/p></description></item><item><title>Giving clients 'jailed' SFTP access to their sites</title><link>https://jshakespeare.com/giving-clients-jailed-sftp-access-to-their-sites/</link><pubDate>Sun, 09 Aug 2015 22:46:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/giving-clients-jailed-sftp-access-to-their-sites/</guid><description>&lt;p>Here’s a common problem amongst freelance web developers. You’ve built a client a website and are dutifully hosting it on your low-end Linux VPS (you know, the one you munged into a web-server by copy-pasting several hundred commands you don’t understand from Stack Overflow answers and Ubuntu forums).&lt;/p>
&lt;p>But what happens when that client sends you an email asking for access to those files so their prodigal 9 year old niece can make some tweaks to the site in exchange for a pack of gum, instead of the hundreds of dollars you would charge?&lt;/p>
&lt;p>Setting up an FTP server is complex and can lead to vulnerabilities if you don’t know what you’re doing. And obviously you don’t want them poking around on the command line with their own system login that you &lt;em>think&lt;/em> you gave the right permissions to.&lt;/p>
&lt;p>One good solution is to give the client what’s called a ‘jailed’ account login. And furthermore, disable them from using that account to SSH in on the command line, and instead force them to use SFTP.&lt;/p>
&lt;p>Before we begin, I’m going to assume you’re using OpenSSH as your SSH client (you probably are). Also it needs to be at least version 4.9.1 (it probably is). Also most, if not all the following commands will need to be run as root.&lt;/p>
&lt;p>First off, create a group called &lt;code>sftp&lt;/code>. This will contain all users we want to give SFTP access to.&lt;/p>
&lt;p>&lt;code>groupadd sftp&lt;/code>&lt;/p>
&lt;p>Now, we tell OpenSSH that it can let anyone in the &lt;code>sftp&lt;/code> group access their own home directories via SFTP. Open &lt;code>/etc/ssh/sshd_config&lt;/code> and at the &lt;em>end of the file&lt;/em> paste this, which will let users SFTP into their own home directories (that’s what the &lt;code>%h&lt;/code> is for):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">Subsystem sftp internal-sftp
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Match Group sftp
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> ChrootDirectory %h
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> ForceCommand internal-sftp
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> AllowTcpForwarding no
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And reload the SSH config&lt;/p>
&lt;p>&lt;code>/etc/init.d/ssh reload&lt;/code>&lt;/p>
&lt;p>Next, add the user you want to grant SFTP access, whose name is Steve. We’ll add Steve straight into the &lt;code>sftp&lt;/code> group&lt;/p>
&lt;p>&lt;code>useradd -g sftp steve&lt;/code>.&lt;/p>
&lt;p>Steve also needs a password and he needs a home directory&lt;/p>
&lt;p>&lt;code>passwd steve&lt;/code> and &lt;code>mkdir /home/steve&lt;/code>&lt;/p>
&lt;p>Now we run the following&lt;/p>
&lt;p>&lt;code>usermod -s /bin/false steve&lt;/code>&lt;/p>
&lt;p>to deny Steve ever getting shell access&lt;/p>
&lt;p>&lt;code>chown root:root /home/steve&lt;/code> and &lt;code>chmod 0755 /home/steve&lt;/code>&lt;/p>
&lt;p>so Steve can upload and download files to his home directory&lt;/p>
&lt;p>Steve can now SFTP into his own little jail cell and do whatever he wants in there without breaking anything. However this isn’t much use, because his website files are located elsewhere on the server, let’s say in &lt;code>/var/www/stevestevington.com/html&lt;/code>.&lt;/p>
&lt;p>The solution is to mount the website directory into Steve’s home directory. That way any changes he makes will be reflected on the actual website. First make the directory to mount&lt;/p>
&lt;p>&lt;code>mkdir /home/steve/html&lt;/code>&lt;/p>
&lt;p>Then mount the website files on it&lt;/p>
&lt;p>&lt;code>mount --bind /var/www/stevestevington.com/html /home/steve/html&lt;/code>&lt;/p>
&lt;p>And finally, ensure that Steve has permission to modify those files. The easiest way to do this is with &lt;code>chown&lt;/code>, but you may need to ensure the group ownership remains with whatever group your webserver belongs to, eg &lt;code>web&lt;/code>&lt;/p>
&lt;p>&lt;code>chown steve:web /var/www/stevestevington.com/html &lt;/code>&lt;/p>
&lt;p>Et voila. Send Steve his login, a link to download Cyberduck, and let him play around with his site to his heart’s content and not bother you ever again.&lt;/p></description></item><item><title>Sharing Swig templates between Node and Marionette.js</title><link>https://jshakespeare.com/sharing-swig-templates-between-node-and-marionette.js/</link><pubDate>Tue, 27 Jan 2015 22:46:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/sharing-swig-templates-between-node-and-marionette.js/</guid><description>&lt;p>If you’ve got a few Backbone/Marionette or Ember projects under your belt you’ll know that it’s often necessary to render HTML server-side that might later become dynamic. This is so that the user sees some kind of content before the Javascript loads (or if they have it turned off) and search engines see some content when they crawl your site.&lt;/p>
&lt;p>This can mean having to maintain two sets of templates - one for the back-end and one for the front-end. This is not ideal because those two sets of templates can easily diverge, causing a mismatch between what appears on page load and what appears when your front-end application kicks in. You may even have two different template languages, which is a headache for all involved.&lt;/p>
&lt;p>Fortunately, it’s not too hard to set up a workflow that lets us share a single set of templates between server and browser. I’ll be showing you how to do this for Marionette with Swig templates, but most of it applies to other front-end frameworks and template languages, as long as they are supported by the server and the browser. A lot of the legwork will be done by Grunt, and assumes you already have a working knowledge of Grunt and a Gruntfile set up. If you aren’t familiar with Grunt, read up on it &lt;a href="http://gruntjs.com/">here&lt;/a>.&lt;/p>
&lt;h2 id="copying-template-files">Copying template files&lt;/h2>
&lt;p>Using Grunt, we’re going to define which templates we want to share with the Marionette application.&lt;/p>
&lt;p>Because you probably don’t want all your template files to get loaded into the browser, we’ll copy the ones we want into a temp folder using &lt;code>grunt-contrib-copy&lt;/code>&lt;/p>
&lt;p>Install this with npm using &lt;code>npm install grunt-contrib-copy&lt;/code>&lt;/p>
&lt;p>In your Gruntfile, add a new config:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">grunt&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">config&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">set&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;copy&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">templates&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">files&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">[{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">src&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">[&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s1">&amp;#39;app/views/comment/inline.html&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">],&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expand&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="kc">true&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">flatten&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="kc">true&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">dest&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;.tmp/templates/comment&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">src&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">[&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s1">&amp;#39;app/views/post/new.html&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s1">&amp;#39;app/views/post/show.html&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">],&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expand&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="kc">true&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">flatten&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="kc">true&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">dest&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;.tmp/templates/post&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">grunt&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">loadNpmTasks&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;grunt-contrib-copy&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notice that we’re writing two copy actions here to keep templates for different entities in separate directories. This will come in handy. Write one block for each directory of views in &lt;code>app/views&lt;/code> that you wish to share with Marionette (or wherever your application&amp;rsquo;s views are kept).&lt;/p>
&lt;h2 id="precompiling-templates-for-the-browser">Precompiling templates for the browser&lt;/h2>
&lt;p>Using jsttojs, we can precompile the templates to strings inside single Javascript object. This lets us easily access the templates we want in the browser.&lt;/p>
&lt;p>First install the jsttojs Grunt plugin with &lt;code>npm install grunt-jsttojs&lt;/code>.&lt;/p>
&lt;p>Then declare the following configuration in your Gruntfile.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">grunt&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">config&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">set&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;jsttojs&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">root&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;.tmp/templates&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">output&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;assets/js/prod/templates.js&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">ext&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;html&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">removebreak&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="kc">true&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">requirements&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;swig&amp;#39;&lt;/span>&lt;span class="p">],&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">name&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;MyApp.templates&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">grunt&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">loadNpmTasks&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;grunt-jsttojs&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This will save all the templates to a global Javsacript object within our app namespace called &lt;code>MyApp.templates&lt;/code>, though you can declare anything here, eg. &lt;code>window.templates&lt;/code>.&lt;/p>
&lt;p>Each template will be named as &lt;code>$directory/$file&lt;/code> in a way that mirrors our original app/views directory, so for example &lt;code>app/views/comment/inline.html&lt;/code> becomes &lt;code>comment/inline&lt;/code>.&lt;/p>
&lt;h2 id="set-up-a-watch-task">Set up a watch task&lt;/h2>
&lt;p>We will now set up a watch task to update our template object every time we edit the original template files, ensuring they stay in sync with the back-end.&lt;/p>
&lt;p>Install grunt-contrib-watch with &lt;code>npm install grunt-contrib-watch&lt;/code>, then add the following config.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">grunt&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">config&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">set&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;watch&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">templates&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">files&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;app/views/**/*.html&amp;#39;&lt;/span>&lt;span class="p">],&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">tasks&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;copy:templates&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;jsttojs&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">grunt&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">loadNpmTasks&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;grunt-contrib-watch&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Now run &lt;code>grunt copy:templates &amp;amp;&amp;amp; grunt jsttojs &amp;amp;&amp;amp; grunt watch&lt;/code> to copy, compile and watch the templates.&lt;/p>
&lt;h2 id="modifying-the-rendering-engine">Modifying the rendering engine&lt;/h2>
&lt;p>By default, the Marionette renderer is configured to render Underscore templates. We will need to override it to render Swig instead.&lt;/p>
&lt;p>Download marionette-swig with &lt;code>bower install marionette-swig&lt;/code>, and include it in your vendors file (&lt;code>bower_components/marionette-swig/src/backbone.marionette.swig.js&lt;/code>).&lt;/p>
&lt;h2 id="modifying-the-template-cache">Modifying the template cache&lt;/h2>
&lt;p>Finally, we need to tell Marionette to look in our templates object (&lt;code>MyApp.templates&lt;/code>) for the templates we declare in our views. To do this we will override the template cache built into Marionette.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">Backbone&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Marionette&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">TemplateCache&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">prototype&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">loadTemplate&lt;/span>&lt;span class="o">=&lt;/span> &lt;span class="kd">function&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">templateId&lt;/span>&lt;span class="p">){&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kd">var&lt;/span> &lt;span class="nx">template&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">charZero&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">charZero&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">templateId&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">charAt&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">charZero&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s1">&amp;#39;#&amp;#39;&lt;/span> &lt;span class="o">||&lt;/span> &lt;span class="nx">charZero&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s1">&amp;#39;|&amp;#39;&lt;/span>&lt;span class="p">){&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// If we request the template by providing a jQuery selector, behave as usual
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="nx">template&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">Backbone&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Marionette&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">$&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">templateId&lt;/span>&lt;span class="p">).&lt;/span>&lt;span class="nx">html&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">else&lt;/span>&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">template&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">MyApp&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">templates&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="nx">templateId&lt;/span>&lt;span class="p">];&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="o">!&lt;/span>&lt;span class="nx">template&lt;/span> &lt;span class="o">||&lt;/span> &lt;span class="nx">template&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">length&lt;/span> &lt;span class="o">===&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">){&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">errorMessage&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;Could not find template: \&amp;#39;&amp;#39;&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="nx">templateId&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="s1">&amp;#39;\&amp;#39;&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">error&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">errorMessage&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;NoTemplateError&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="nx">template&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="declaring-templates-in-views">Declaring templates in views&lt;/h2>
&lt;p>Now we can simply declare our templates within our views by matching their path within &lt;code>app/views&lt;/code>. Eg&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">MyApp&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">InlineComment&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">Marionette&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">ItemView&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">extend&lt;/span>&lt;span class="p">({&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">template&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;comment/inline&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// ...
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And there you have it. Just add new templates to your Gruntfile&amp;rsquo;s copy config, and ensure they get copied and precompiled before you use them in the browser.&lt;/p></description></item><item><title>Tasty media queries in Sass</title><link>https://jshakespeare.com/tasty-media-queries-in-sass/</link><pubDate>Thu, 29 May 2014 22:46:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/tasty-media-queries-in-sass/</guid><description>&lt;p>Writing and maintaining media queries has become a snap since version 3.2 of Sass. This was the release that debuted &lt;code>@content&lt;/code>, which lets us pass custom styles to mixin inclusions.&lt;/p>
&lt;p>This made writing media queries much easier because we could define mixins for smaller-than-n and larger-than-n. These mixins act as filters that let us easily add width, height or resolution specific styles to definitions. The mixin accepts any styles defined within the content of the &lt;code>@include&lt;/code> and adds them into a media query based on the value specified in the argument. For example, with the following mixins:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> smaller-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> larger-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">min-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We could write&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="nc">.heading&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@include&lt;/span>&lt;span class="nd"> smaller-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">300&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="mf">.5&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@include&lt;/span>&lt;span class="nd"> larger-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">768&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Which outputs this CSS&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-css" data-lang="css">&lt;span class="line">&lt;span class="cl">&lt;span class="p">.&lt;/span>&lt;span class="nc">heading&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">font-size&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">@&lt;/span>&lt;span class="k">media&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="nt">max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nt">300px&lt;/span>&lt;span class="o">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">.&lt;/span>&lt;span class="nc">heading&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">font-size&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">1.5&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">@&lt;/span>&lt;span class="k">media&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="nt">min-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nt">768px&lt;/span>&lt;span class="o">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">.&lt;/span>&lt;span class="nc">heading&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">font-size&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We could take this one step further and use variables for each device breakpoint, making our code more DRY-friendly.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">$mobile-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">300&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">$tablet-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">768&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> smaller-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$device&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@if&lt;/span> &lt;span class="nv">$device&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">mobile&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$mobile-width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@else if&lt;/span> &lt;span class="nv">$device&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">tablet&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$tablet-width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// ... Etc for each device
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>(You can read more about this technique on the &lt;!-- raw HTML omitted -->Sass blog&lt;!-- raw HTML omitted -->)&lt;/p>
&lt;p>The problem is, until now we’ve had to define our &lt;code>smaller-than&lt;/code> and &lt;code>larger-than&lt;/code> mixins with a set of if/else if statements to transform our device argument into a value that Sass can use to create a media query. This quickly gets unwieldy, as each new breakpoint means updating both our mixins with a new statement.&lt;/p>
&lt;p>The good news is Sass 3.3 includes full support for maps (aka hashes or data objects), which makes writing media queries even easier. Now we can declare all our breakpoints in one variable:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">$mqs&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">mobile_portrait&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">300&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="o">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">mobile_landscape&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">500&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="o">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">tablet_portrait&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">768&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="o">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">tablet_landscape&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">1024&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="o">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">desktop&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">1280&lt;/span>&lt;span class="kt">px&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And use the new &lt;code>map-get&lt;/code> function to access the values. That lets us whittle our mixins down to&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> smaller-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$device&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nv">$width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nf">map-get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$mqs&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$device&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> larger-than&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$device&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nv">$width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nf">map-get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$mqs&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$device&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">min-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We can even write a ‘between’ mixin&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> between&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$smallest&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$largest&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nv">$min-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nf">map-get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$mqs&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$smallest&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nv">$max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nf">map-get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$mqs&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$largest&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@media&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">min-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$min-width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="na">max-width&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nv">$max-width&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Which makes our media queries highly readable&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="nc">.heading&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@include&lt;/span>&lt;span class="nd"> between&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">mobile_portrait&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="n">mobile_landscape&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="mf">.5&lt;/span>&lt;span class="kt">em&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>By separating values and mixins using the above technique you can easily add, remove and edit breakpoints in one place. Your code becomes more readable and more maintainable as a result, and you go home with some serious internet points.&lt;/p></description></item><item><title>Slowing down</title><link>https://jshakespeare.com/slowing-down/</link><pubDate>Mon, 26 May 2014 23:22:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/slowing-down/</guid><description>&lt;p>It’s a simple Newtonian law that explains why the faster you’re travelling before a crash, the slower your life becomes after it. When I was cut up on my bike by an SUV two weeks ago I hit the side of the car with just the right momentum to fracture and displace my collarbone by an inch. And, true to our physicist friend’s theory, my pace of life has been considerably slower since then. I have to do most things with my left (non-dominant) hand, being physically unable to lift my right arm from the shoulder and keeping it strapped to my body most of the time. I can’t walk at my usual clip because the bone bulging out of my shoulder tears at the skin if I do. I can’t ride my bike, I can’t take the subway and I can’t cook.&lt;/p>
&lt;p>This has given me some fresh perspectives. The most prevalent perspective is a burning hatred of SUV drivers, but after channelling my inner Pollyanna and looking for the positives, I can also see how a slower pace of life has its benefits. Perhaps it’s ironic that only after leaving an imprint of myself in the side of a car do I see the importance of slowing down in life, but it is a valuable revelation nonetheless: in slowing down, life can be enjoyed more thoroughly. The pressure of being 25 years old in New York City and having to do every imaginable activity right this instant disappears, and with it disappears the tunnel vision that obscures what is available to be enjoyed simply by looking around and enjoying the moment.&lt;/p>
&lt;p>Taking life slowly gives me more time to think about what I’m doing. The &amp;rsquo;next thing’, be it another social event or task in a project is placed just far enough out of reach that it gives the present moment more time and space to receive the attention it deserves.&lt;/p>
&lt;p>Even writing this, I’m typing so slowly that I have plenty of time to form a complete sentence in my head long before I reach for the full stop key. The same has gone for coding. Normally my monitor is a frenzy of windows fighting for the foreground and code and commands spilling into my text editor before my brain has had a chance to think about what I’m even trying to achieve. Slowing down gives me that bit of extra time to think and do the job properly.&lt;/p>
&lt;p>Of course having a broken bone comes with many frustrations, but it also comes with the perfect excuse to simplify. Perhaps there’s something wrong with me that I need do major bodily harm to myself in order to enjoy life’s simple pleasures, but I don’t think I’m the only one. We’re very good at filling our lives with so much stuff that we’re forced to operate at breakneck speed just to keep up. Much of the time we do this simply because the opposite is scary. We fear the void of an empty calendar and having no company but ourselves. The reality is that life has just as much to offer when you’re out for a stroll as it does when you’re sprinting.&lt;/p></description></item><item><title>Your creations will not outlast you</title><link>https://jshakespeare.com/your-creations-will-not-outlast-you/</link><pubDate>Fri, 04 Apr 2014 00:00:00 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/your-creations-will-not-outlast-you/</guid><description>&lt;p>If you work in the creative industries, it’s inviting to think that someday people might look back on the things you made and marvel at the genius that sculpted them. Just as we look at the masterful brush strokes of Starry Night or the sweeping arches of the Brooklyn Bridge, we dream that our own creations too will outlive us.&lt;/p>
&lt;p>What we are dreaming of is called legacy. But on the web, the nature of legacy is very different to that of Van Gogh or the Roeblings. What we build will not outlast us as their creations did. In the tech world, old equals bad. In fact, the term ‘legacy’ is used by programmers to describe code that is obsolete and has been superseded.&lt;/p>
&lt;p>The basis of HTTP, the internet technology that has enabled you to access the article you’re reading right now, is transience. In computer science we call it statelessness. A stateless protocol means that when our computer requests data from a server it is a one-time thing. Once the data has been sent, both parties cease to be aware of the other’s status. In the space between the server sending you this article and you having read up this point, the entire website could have been wiped from human history with a single keystroke.&lt;/p>
&lt;p>This transience means that our creations only exist as long as some machine somewhere is willing to serve it up to us. If that machine breaks down or there’s a power cut or a number of critical underwater cables are damaged, our creations effectively cease to exist, even if it’s for a short period of time.&lt;/p>
&lt;p>A few years ago a post appeared on Reddit marveling at the fact that the Space Jam website was still live on Warner Bros server, unchanged from its launch in 1996. People were astounded that something so dated could possibly still exist.&lt;/p>
&lt;p>That website is 18 years old. One of the designers of the website, Andrew Stachler, still works at Warner Bros. The money the developers were paid to build this is probably still sitting in their 401(k)s. And yet now the web looks back at this creation and chuckles at how different things were &amp;lsquo;back then’. Months of work probably went into building that website, and now it seems quaint merely by virtue of the fact that no one has bothered to delete it.&lt;/p>
&lt;p>By building things on the web we are not artists or engineers or even creators. We are scientists, working alongside each other for an ongoing cause. The things we build today will not be used after our death. They may not even be used after our next birthday. That Space Jam website may look comically dated now, but for the developers that built it and the users that visited it, it was just one of many millions of stepping stones that have helped shape the web we know today.&lt;/p>
&lt;p>Steve Jobs summed this notion up perfectly in a &lt;a href="https://www.youtube.com/watch?v=zut2NLMVL_k">1994 interview&lt;/a>:&lt;/p>
&lt;blockquote>
&lt;p>It’s sort of like sediment of rocks. You’re building up a mountain and you get to contribute your little layer of sedimentary rock to make the mountain that much higher. But no one on the surface will ever see your sediment. They’ll stand on it; it’ll be appreciated by that rare geologist. But it’s not like the renaissance at all. It’s very different&lt;/p>
&lt;/blockquote>
&lt;p>This doesn’t mean that we can have no legacy on the web. Almost every computer built during Steve Job’s tenure as Apple CEO is already obsolete, but his legacy is one of the most prominent of our time. Jobs stood on the shoulders of giants like Alan Kay, Alan Turing, Ada Lovelace and Charles Babbage. The legacies of these men and women are not in what they individually made during their lifetimes, but in the fact that you loaded this article in a fraction of a second and read it on a machine of vast complexity now, in 2014.&lt;/p>
&lt;p>What we make on the web today will not outlast us. But people can be inspired by what you make, and the things those people make can inspire others, and so on. That can be forever.&lt;/p>
&lt;p>&lt;em>This post was originally published on &lt;a href="http://bighumandotcom.tumblr.com/">bighumandotcom.tumblr.com&lt;/a>&lt;/em>&lt;/p></description></item><item><title>Code isn't poetry</title><link>https://jshakespeare.com/code-isnt-poetry/</link><pubDate>Wed, 02 Apr 2014 12:00:00 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/code-isnt-poetry/</guid><description>&lt;p>Ernest Hemingway’s masterfully uncluttered prose made for some of the best novels to come out of America in the 20th century. He sought to achieve truthfulness of language, primarily by eschewing ornament and flamboyance in his writing. Of his 1964 novel &lt;em>A Moveable Feast&lt;/em> he said&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;If I started to write elaborately, or like someone introducing or presenting something, I found that I could cut that scrollwork or ornament out and throw it away and start with the first true simple declarative sentence I had written.”&lt;/p>
&lt;/blockquote>
&lt;p>People tend to mistake Hemingway’s style as pure terseness, but this is not always the case. He did not write with a minimalist agenda, he simply championed readability. Whilst he often wrote in a staccato fashion he also penned sentences spanning an entire paragraphs, repeating the same words several times to enforce their meaning.&lt;/p>
&lt;p>The reason Hemingway wrote this way was because he understood that the medium is not the message. If syntax or vocabulary got in the way of the story it would detract from the essence of the story itself. Through writing as he did, Hemingway emulated a conversational style of writing that reads with little effort.&lt;/p>
&lt;p>As developers we can apply this aesthetic to the way we write code. When we know that our code will be read by a human at some point (even it is by our future selves), we should try to write it in a way that is as uncluttered, simple and non-egotistical as possible.&lt;/p>
&lt;p>Twitter developer Angus Croll explored how Hemingway would have written Javascript in &lt;!-- raw HTML omitted -->a post&lt;!-- raw HTML omitted --> from a couple of years ago. He described his Hemingway example as&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Code reduced to its essentials with no word or variable wasted. It’s not fancy; maybe its even a little pedantic - but that’s the beauty of Hemingway’s writing.”&lt;/p>
&lt;/blockquote>
&lt;p>An ongoing point of contention amongst programmers is whether code should be terse or verbose. Terse code is that which is reduced to its minimum required syntax to achieve its means, while advocates of verbose code favour redundancy and legibility to aid understanding.&lt;/p>
&lt;p>But to argue for either a terse or verbose style is reductive. What we should aim for is the shortest possible elapsed time to both write and read code. Sometimes that means writing and structuring code in a way that forgoes clever tricks and time-saving devices because in the long run it will make it easier to maintain. It can also mean writing code that appears terse to make it more legible.&lt;/p>
&lt;p>Let’s consider a basic example. Ternary operators are often used by programmers to shorten three-way operations. If we wanted to represent the phrase &amp;lsquo;red sky at night, shepherd&amp;rsquo;s delight&amp;rsquo; in Ruby we could do so using a ternary operator like this&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-ruby" data-lang="ruby">&lt;span class="line">&lt;span class="cl">&lt;span class="n">forecast&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">sky_color&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;red&amp;#34;&lt;/span> &lt;span class="p">?&lt;/span> &lt;span class="s2">&amp;#34;sunny&amp;#34;&lt;/span> &lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;rainy&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Or we could write it in longhand form&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-ruby" data-lang="ruby">&lt;span class="line">&lt;span class="cl">&lt;span class="n">forecast&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">sky_color&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;red&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;sunny&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;rainy&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">end&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>While the first example is more terse the second is certainly more readable. It may even be that the first takes longer for the developer to write, dependent on their familiarity with ternary operators.&lt;/p>
&lt;p>There&amp;rsquo;s a tendency in web and software development to be constantly pushing ourselves - often on the job - to write code in increasingly abstract and inventive ways. This typically stems from the natural and healthy desire to do a job as well as possible and use the full set of tools available, but it comes with increased comprehension time both to write and to read. It’s the programmer’s equivalent of digging out a thesaurus and picking out unfamiliar and superfluous vocabulary.&lt;/p>
&lt;p>In the words of Brian Kerningham&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”&lt;/p>
&lt;/blockquote>
&lt;p>In other words, if you can do a job effectively with the tools available and familiar to you and your fellow developers, it&amp;rsquo;s quicker to use them than to drive out to the hardware store and pick up a bunch of fancy new tools you don&amp;rsquo;t know how to use.&lt;/p>
&lt;p>To apply Hemingway&amp;rsquo;s style of writing to programming is to understand that &amp;lsquo;clever&amp;rsquo; programming does not inherently improve the quality or readability of code, just as abstract or overly-elaborate prose does not inherently improve the readability of a novel. Rather, by writing in as pragmatic a way as possible, a developer will produce code that is both easy to write and to read. Economy of language is championed whilst simultaneously reducing the barrier of entry to readers.&lt;/p>
&lt;p>As with literature, code can be a window into another realm. But whereas simply-written code is like a clear and clean pane of glass, overly clever code can be like an ornate stained glass window. It may look decorative and have taken great skill to make, but it’s almost impossible to see through it.&lt;/p>
&lt;p>The Hemingway philosophy is this: do not write with ego or pretense, but rather with pragmatism. If something can’t be written by simple means, the concept must be broken down into simpler constituent parts. Do not clutter your writing with self-indulgent embellishments. Do not confuse simplicity with brevity. Write in a direct, clear manner regardless of how many bytes it takes to do so.&lt;/p>
&lt;p>Stick to this philosophy, and make Papa proud.&lt;/p></description></item><item><title>Easy Icomoon icons with Sass</title><link>https://jshakespeare.com/easy-icomoon-icons-with-sass/</link><pubDate>Sun, 09 Mar 2014 18:22:56 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/easy-icomoon-icons-with-sass/</guid><description>&lt;p>Using icons on the web has become exactly 9000% better since the arrival of &lt;!-- raw HTML omitted -->Icomoon&lt;!-- raw HTML omitted -->, a web app that lets you easily create icon fonts from vector files.&lt;/p>
&lt;p>Trouble is, implementing the icons that Icomoon gives you is still a bit of a pain. It requires adding the &lt;code>.icon&lt;/code> and &lt;code>.icon-fontname&lt;/code> classes to your elements, which is non-semantic and not DRY-friendly. Modification of the icon requires more CSS that targets specifically the &lt;code>:before&lt;/code> pseudo-element, which means a load of rules that aren’t bound explicitly to the icon itself. Furthermore, if you want to display the icon &lt;code>:after&lt;/code> the element content, or replace the content entirely, you have a lot more hassle on your hands.&lt;/p>
&lt;p>Wouldn’t it be nice if you could just include icons, positioned exactly how you want them, in your existing CSS classes with one line?&lt;/p>
&lt;p>Using this Sass mixin, you can:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="k">@mixin&lt;/span>&lt;span class="nf"> icomoon&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nv">$icon&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$position&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;before&amp;#34;&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="nv">$replace&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="n">false&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// If we&amp;#39;re replacing the text, set font-size to 0
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="k">@if&lt;/span> &lt;span class="nv">$replace&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Pseudo-element properties
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="k">&amp;amp;&lt;/span>&lt;span class="nd">:&lt;/span>&lt;span class="si">#{&lt;/span>&lt;span class="nv">$position&lt;/span>&lt;span class="si">}&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@extend&lt;/span> &lt;span class="nc">.icon-&lt;/span>&lt;span class="si">#{&lt;/span>&lt;span class="nv">$icon&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-family&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s1">&amp;#39;icomoon&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">speak&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="ni">none&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-style&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="ni">normal&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-weight&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="ni">normal&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-variant&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="ni">normal&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">text-transform&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="ni">none&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">line-height&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@if&lt;/span> &lt;span class="nv">$replace&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="kt">rem&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">@content&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The mixin assumes two things:&lt;/p>
&lt;ul>
&lt;li>You have already declared the ‘icomoon’ font-family using &lt;code>@font-face&lt;/code> (grab this from Icomoon’s style.css).&lt;/li>
&lt;li>You have classes assigned to each unicode character in your font file (this is more or less what Icomoon gives you in style.css, minus the &lt;code>:before&lt;/code>), like this:&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="nc">.icon-search&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">content&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;\e602&amp;#34;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nc">.icon-fb&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">content&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;\e605&amp;#34;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nc">.icon-email&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">content&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;\e606&amp;#34;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// ...
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="usage">Usage&lt;/h2>
&lt;p>To use the mixin, just call:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="k">@include&lt;/span>&lt;span class="nd"> icomoon&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;myIcon&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first argument, &lt;code>$icon&lt;/code>, accepts the icon name, minus the &amp;ldquo;icon-&amp;rdquo; prefix used to define the class.&lt;/p>
&lt;p>For the second argument, &lt;code>$position&lt;/code>, you can provide “before” or “after”, depending on where you want the icon to be placed relative to the contents of the element.&lt;/p>
&lt;p>The &lt;code>$replace&lt;/code> argument is a bool that sets the font-size of the element&amp;rsquo;s immediate contents to 0 when true. Note that while the mixin will attempt to restore the font-size of the icon to &lt;code>1rem&lt;/code>, you should pass in a pixel-based font size value to the content block to reliably set the size of the icon.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-scss" data-lang="scss">&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Visually replace the contents of the element with the search icon
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">@include&lt;/span>&lt;span class="nd"> icomoon&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;search&amp;#34;&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="s2">&amp;#34;before&amp;#34;&lt;/span>&lt;span class="o">,&lt;/span> &lt;span class="n">true&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">font-size&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="mi">20&lt;/span>&lt;span class="kt">px&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Replacing text in this way is a good idea because it means the text is still there as a fallback for screen-readers or in case your stylesheet fails to load.&lt;/p>
&lt;p>Hopefully this will make managing your icons a little easier.&lt;/p></description></item><item><title>How to be a terrible writer</title><link>https://jshakespeare.com/how-to-be-a-terrible-writer/</link><pubDate>Sun, 02 Mar 2014 18:22:56 +0500</pubDate><guid isPermaLink="true">https://jshakespeare.com/how-to-be-a-terrible-writer/</guid><description>&lt;ul>
&lt;li>
&lt;p>Don’t set aside time for writing, but rather panic every couple of weeks about how little you write. Once every few months, publish an apologetic post about how you haven’t written for ages and tell your friends you have ’something in the pipeline&amp;rsquo;. Over time, the frequency of these crises will diminish and normalise.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Only read books when you get into bed at night after a long day’s work, ensuring that you read no more than a couple of pages before falling asleep and forgetting what you just read. Re-read the same pages the following night. Repeat ad nihil profectus.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Intersperse your writing with Latin phrases to make yourself sound intelligent and alienate your audience.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Each time you sit down to write a blog post, you should actually be fiddling with bits of code on your site. Every six months you should move your entire site to the blogging platform du jour, then write a post about the migration process.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Never carry any form of physical writing material, so that should an idea come to mind, you’ll have time to forget it while you mess around on your phone waiting for Evernote to load.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Any opportunity that could be used by your subconscious for introspection and reflective thought should be quashed. For example, instead of staring out the window on a train journey, try rejecting hundreds of eligible life-partners on Tinder.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Worry constantly about what other people think of your writing. History has proven that the quality of an author’s writing is directly proportional to its popularity and click-through rate.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Stick primarily to writing about topics you don’t understand or have any real interest in. Compensate for lack of veracity and vehemence by being overly verbose, by using words like veracity and vehemence, and with gratuitous use of alliteration.&lt;/p>
&lt;/li>
&lt;/ul></description></item><item><title>You aren't learning if you aren't doing</title><link>https://jshakespeare.com/you-arent-learning-if-you-arent-doing/</link><pubDate>Mon, 20 May 2013 17:24:56 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/you-arent-learning-if-you-arent-doing/</guid><description>&lt;p>One of the most exciting things about being involved with the web is that new technologies are emerging all the time. The thing that sucks about that is new things are emerging &lt;em>all the damn time&lt;/em>. If a developer is to remain competitive, pioneering and most importantly, in a job, then learning must be a core part of her career.&lt;/p>
&lt;p>But learning purely to avoid atrophy won&amp;rsquo;t ever help you achieve a demonstrably useful level in a new skill. Even &lt;em>wanting&lt;/em> to learn isn&amp;rsquo;t enough. If the onus is entirely on self-motivation and the intrinsic rewards of bettering oneself to sustain interest, then for most of us it will be a short-lived venture every time. We must have a &lt;em>need&lt;/em> to learn - an environment with real demands and a place to immediately put fresh knowledge to task - not just a sandbox that will never see the light of day.&lt;/p>
&lt;p>In other words, we must create a knowledge vacuum, not simply crowbar more information into an already overloaded brain.&lt;/p>
&lt;h2 id="necessity-is-the-mother-of-learning">Necessity is the mother of learning&lt;/h2>
&lt;p>Case in point - I&amp;rsquo;ve been learning Rails, or rather &lt;em>intending&lt;/em> to learn Rails, on and off for longer than I care to say. I&amp;rsquo;m still entirely incompetent because, although the framework excites me, until recently I haven&amp;rsquo;t the incentive of seeing it solve real need. Only when I thought up an idea for a real-world Rails app did I really start to ramp up the learning process.&lt;/p>
&lt;p>There are, of course, countless Rails books and tutorials that would have walked me through building a Twitter clone or an event ticketing website or whatever else, and I would undoubtedly have learned a lot along the way. But this mindset is wrong for two reasons:&lt;/p>
&lt;ol>
&lt;li>
&lt;p>What you&amp;rsquo;re building is dull and, more importantly, not real. Any good teacher will tell you that the key to engaging students is to present the subject matter in a relevant and exciting way.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>It&amp;rsquo;s information that is, at the immediate time of learning, redundant. That means that you&amp;rsquo;re less likely to retain that information and you don&amp;rsquo;t really have any real-world understanding of how to apply it.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>Compare this to starting with an idea and learning as you build. You have both the ongoing rewards of solving your own problems and the stronger contextual factors that will aid your memory further down the line.&lt;/p>
&lt;p>Of course, you&amp;rsquo;d be going about things in a very backwards manner if you were to learn something from scratch armed with nothing more than a blank text file and Stack Overflow, and anyone who were to do follow such an ad-hoc methodology would probably turn out to be a worse than useless programmer. But when you&amp;rsquo;re plodding your way through your Codecademy tutorials and Youtube screencasts, it undeniably helps to at least have an idea in the back of your mind as to how you will apply your newfound knowledge to something you actually care about.&lt;/p>
&lt;p>Rack your brains for something to build that will benefit you, or look for ways to use emergent technology to improve projects you already work on. Think of it this way, which of the following sentences excites you more: &amp;ldquo;I&amp;rsquo;m learning Node.js&amp;rdquo; or &amp;ldquo;I&amp;rsquo;m building a kickass web app with Node.js that will let me easily calculate how much petrol money I save each week by cycling&amp;rdquo;? (You can have that idea for free, internet. You&amp;rsquo;re welcome).&lt;/p>
&lt;p>Learning is a vital part of keeping your mind active and your career chops honed, but ultimately it&amp;rsquo;s street smarts, real world application of knowledge, that will help you the most and spur you to keep learning. Invest your learning time in doing and you will, without even realising, be absorbing the skills to tackle real situations you will face in the field.&lt;/p></description></item><item><title>No comment.</title><link>https://jshakespeare.com/no-comment./</link><pubDate>Thu, 11 Apr 2013 07:32:47 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/no-comment./</guid><description>&lt;p>&amp;ldquo;A Blog Without Comments Is Not a Blog&amp;rdquo;. So reads the title of an &lt;a href="http://www.codinghorror.com/blog/2006/04/a-blog-without-comments-is-not-a-blog.html">article&lt;/a> by Jeff Atwood published in 2006, in which he argues that disabling comments on a blog is tantamount to preaching a sermon; it is a one way delivery of content that doesn&amp;rsquo;t facilitate further discussion or user engagement.&lt;/p>
&lt;p>But let’s think about what value a comment form actually adds to a site. Should we simply assume that because comments bring added engagement that they are always a good thing? When you reach the end of this article, you will notice there are no comments. It&amp;rsquo;s a decision that I have made both as a designer looking to optimise the user experience of my site and as a writer looking to foster discussion in a wider sphere.&lt;/p>
&lt;p>When was the last time you stopped scrolling when you reached the end of an article you felt engaged with? If there’s content below that article then the likelihood is that you’re going to read that too - or at least skim it.&lt;/p>
&lt;p>This is because of a key principle in the study of HCI called &lt;a href="http://en.wikipedia.org/wiki/Information_foraging">Information Foraging&lt;/a>. It&amp;rsquo;s the same instinctive nature that makes animals follow a scent to find food in the wild. On a computer, if we&amp;rsquo;re following an information trail and receiving clues that it is taking us closer to what we&amp;rsquo;re searching for, then we&amp;rsquo;ll keep going. From &lt;a href="http://www.nngroup.com/articles/information-scent/">Jakob Nielsen&lt;/a>:&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Informavores will keep clicking as long as they sense that they&amp;rsquo;re &amp;ldquo;getting warmer&amp;rdquo; &amp;ndash; the scent must keep getting stronger and stronger, or people give up.&amp;rdquo;&lt;/p>
&lt;/blockquote>
&lt;p>Let&amp;rsquo;s think about this theory in terms of content on a single web page. Someone is reading your article and it elicits in them a response, negative or positive, that either encourages them to keep scrolling or to leave (let&amp;rsquo;s assume they keep scrolling). The more they scroll, the more content is revealed and the more the user&amp;rsquo;s desire to keep reading is satisfied. When they reach the end of the article, what will they do if there’s more content below it? Keep going. It&amp;rsquo;s a natural, subconscious reflex. They’ve been scrolling down for the past five minutes and have been lavished with juicy blog goodness, why stop now?&lt;/p>
&lt;p>Before the user has had a chance to process what they&amp;rsquo;ve read and form their own opinion, the content of the article has blurred into the responses of others. The stream of information that was so fervently stimulating their grey matter mere seconds ago has descended into a clattering of opinions that rapidly drowns out the intended message of the article. It&amp;rsquo;s the equivalent of reaching the end of a book and having a mob of other readers immediately descend on you, everyone trying to make their review heard above the fray. They could all have valid opinions on what you&amp;rsquo;ve just read, but with everyone shouting over each other it&amp;rsquo;s more likely to inhibit your ability to process information than it is to aid it.&lt;/p>
&lt;p>The real kicker of this is that the content below the article is likely to have a greater power of retention than the article itself. This is thanks to another psychological phenomenon known as &lt;a href="http://en.wikipedia.org/wiki/Recency_effect#Recency_effect">Recency Effect&lt;/a>, an observation that people are more likely to recall the items at the end of a list than the items preceding them. Coupled with compulsive &amp;lsquo;overflow&amp;rsquo; scrolling this means users are most likely walking away with someone else’s message; not ideal if you’re trying to inform or make a point. (This is especially bad news for vote-based comment systems that see less popular comments get pushed to the bottom.)&lt;/p>
&lt;p>Of course, this logic flies in the face of modern-day thinking around democratised content - in which user engagement is to be prized above all else. But even you do consider this to be the holy grail, there still remains another problem: the traditional format of comments is terrible for discussion.&lt;/p>
&lt;p>Let me bring this back to you, dear reader, by way of example. After all, you&amp;rsquo;re an intelligent individual who&amp;rsquo;s just read 685 whole words of prose and probably has valid opinions coming out the wazoo. Say you get to the end of this article and pen the most eloquent, rational comment that makes everything I’ve written look like pseudo-intellectual drivel. What’s the likelihood that a) you will have read the comments of every other user before leaving yours and b), that you will come back and check for a reply to your comment, or even read a reply if you are notified of one? Most comments are simply &lt;a href="http://www.youtube.com/watch?v=7R6_Chr2vro">drive-by arguments&lt;/a>, and with everyone doing it we’re not cultivating valuable engagement.&lt;/p>
&lt;p>Even if you were to find yourself in a discussion with one or more other readers, this thread is likely to take a tangential course fairly quickly - most likely flanked by several other discussions also going off in different directions. For new readers wanting to weigh in with their thoughts, or consider the article in the light of other people’s views, it&amp;rsquo;s like turning up sober to a party after everyone else has got drunk; you probably won&amp;rsquo;t understand what anyone is saying and will quickly forget why you even wanted to go in the first place.&lt;/p>
&lt;p>By disabling comments an author is not denying the right to discuss what she has written, she is ensuring that the signal isn&amp;rsquo;t drowned out by noise.&lt;/p>
&lt;p>I&amp;rsquo;m not saying comments are inherently bad and that no site should have them. There are plenty of scenarios in which providing a centralised point of discussion is the right thing to do. I understand that it&amp;rsquo;s not always appropriate for people to post their responses in external fora, especially when issues of anonymity and community are considered. But if an article contributes to a wider sphere of reasoning then I don’t think that having a comment form below it does anything substantial to further that debate. Conversely, it’s not regressive to encourage people to take their thoughts into the wider world instead of leaving them at the bottom of a webpage.&lt;/p>
&lt;p>If you feel compelled to comment on this article, please do so. Tweet about it, email me, rant at your spouse/child/cat for a few minutes. Discussion is healthy. But as soon as it degrades into noise, it&amp;rsquo;s probably time to change the topic.&lt;/p></description></item><item><title>Stop externalising your life</title><link>https://jshakespeare.com/stop-externalising-your-life/</link><pubDate>Wed, 03 Apr 2013 16:58:08 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/stop-externalising-your-life/</guid><description>&lt;p>Recently the Barbican museum in London held an exhibition called the Rain Room. It was an installation in which water poured from the ceiling, but sensors detected where people were standing and would turn off the taps above their heads so they didn&amp;rsquo;t get wet. It was a clever and engaging piece of interactive art and was immensely popular. During the time this installation was open, my Twitter stream was filled with photos of people standing in the Rain Room, accompanied by the caption &amp;lsquo;Rain Room @ The Barbican!&amp;rsquo; and a location attachment to prove that they were indeed in the Rain Room.&lt;/p>
&lt;p>This stream of homogenous photos got me thinking. What were people actually saying by Tweeting about their visit? They certainly weren&amp;rsquo;t sharing some hidden gem with their followers, nor were they bringing a unique interpretation of the artwork to the table. Ultimately, all they were doing was fulfilling the obligation that we have to Share. Not sharing in the sense of treasuring a moment with people close to us, but Sharing in the sense of &amp;rsquo;notify the world that I am doing a thing&amp;rsquo;.&lt;/p>
&lt;p>I&amp;rsquo;ve just spent a month in Singapore. Throughout the first couple of weeks I felt a constant nagging that everyone back home had to know what was going on. I felt like I should be photographing everything I did as proof; that all the exotic food I was eating and the sights I was seeing wouldn&amp;rsquo;t really matter if they weren&amp;rsquo;t digitally logged in a data centre somewhere in Utah. I found myself taking pictures purely to convey what an amazing time I was having, so that my friends would see it on their smartphones whilst riding the bus back in London and be impressed.&lt;/p>
&lt;p>It&amp;rsquo;s natural to want to share experiences with the people you care about. After all, the classic postcard greeting is &amp;lsquo;Wish you were here&amp;rsquo;. But I think our reasons for sharing experiences on social media are more cynical than that. It&amp;rsquo;s not sharing, it&amp;rsquo;s bragging. When we log in to Facebook or Twitter we see an infinitely updating stream of people enjoying themselves. It&amp;rsquo;s not real life, of course, because people overwhelmingly post about the good things whereas all the crappy, dull or deep stuff doesn&amp;rsquo;t get mentioned. But despite this obvious superficiality, it subconsciously makes us feel like everyone is having a better time than us. We try to compete by curating our own life experiences to make it look like we&amp;rsquo;re also having non-stop fun and doing important things. It breeds in us a Pavlovian response that means every time something good is happening to us we must broadcast it to as many people as possible.&lt;/p>
&lt;p>There are plenty of &amp;lsquo;Facebook is bad for you because X&amp;rsquo; posts, but I&amp;rsquo;m talking about a mindset that goes beyond any single web service. This is the curse of our age. We walk around with the tools to capture extensive data about our surroundings and transmit them in real-time to the bedrooms and pockets of friends, family and every acquaintance we&amp;rsquo;ve made in the past eight years. We end up with a diminished perception of reality because we’re more concerned about choosing a good Instagram filter for our meal than we are about how it tastes. We become Martian rovers, trundling around our environment, uploading data without the ability or desire to make any sense of it. Ultimately, we end up externalising our entire lives.&lt;/p>
&lt;p>I don&amp;rsquo;t think that it&amp;rsquo;s inherently wrong to want to keep the world updated about what you&amp;rsquo;re doing. But when you go through life robotically posting about everything you do, you&amp;rsquo;re not a human being. You’re just a prism that takes bits of light and sound and channels them into The Cloud, to be stored with all the other bits of light and sound from everyone else. You become nothing more than the thumb operating your smartphone.&lt;/p>
&lt;p>The key thing to remember is that you are not enriching your experiences by sharing them online; you’re detracting from them because all your efforts are focussed on making them look attractive to other people. Your experience of something, even if similar to the experience of many others, is unique and cannot be reproduced within the constraints of social media. So internalise that experience instead. Think about it. Go home and think about it some more. Write about it in more than 140 characters; on paper even. Paint a picture of it. Talk about it face to face with your friends. Talk about how it made you feel.&lt;/p>
&lt;p>Once you stop seeing things through the eyes of the people following you on Twitter or Facebook or Instagram you become able to make experiences your own. You can relieve yourself of the burden of having to make everyone aware of what you&amp;rsquo;re doing at all times. You can make your experiences significant because you were there and you saw the sights and smelled the smells and heard the sounds, not because you snapped a photo of it through a half-inch camera lens built into your phone.&lt;/p>
&lt;p>&lt;em>Discussion over at &lt;a href="https://news.ycombinator.com/item?id=5491611">Hacker News&lt;/a>&lt;/em>&lt;/p></description></item><item><title>Climbing the fig tree</title><link>https://jshakespeare.com/climbing-the-fig-tree/</link><pubDate>Wed, 27 Mar 2013 21:30:00 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/climbing-the-fig-tree/</guid><description>&lt;blockquote>
&lt;p>&amp;ldquo;I saw my life branching out before like the green fig-tree in the story… I saw myself sitting there in the crotch of this fig-tree, starving to death, just because I couldn&amp;rsquo;t make up my mind which of the figs I would choose. I wanted each and every one of them, but choosing one meant losing all the rest, and, as I sat there, unable to decide, the figs began to wrinkle and go black, and, one by one, they plopped to the ground at my feet.&amp;rdquo;&lt;/p>
&lt;/blockquote>
&lt;p>—Sylvia Plath, &lt;em>The Bell Jar&lt;/em>&lt;/p>
&lt;p>As a web developer in my twenties I often feel overwhelmed by the sheer breadth and depth of the potential career paths open to me. Working in an underskilled, high-value industry where money is practically thrown at anything resembling a good idea feels just about as close to being the kid in the proverbial candy shop as one can get.&lt;/p>
&lt;p>Yet I&amp;rsquo;m starting to feel increasingly like Esther Greenwood sitting in the fig tree. Admiring the abundance of choices but at the same time frozen with indecision and the fear that the branch will break when I set out to pick one of them.&lt;/p>
&lt;p>While I and everyone else working in the tech industry scurries around, reaping its bounty, picking the low-hanging fruit, part of me can&amp;rsquo;t help but wonder: where is it going? Will I still be doing this in twenty years time? Do I even want to be doing this in twenty years time? Will I have exhausted the desire I have now to write code?&lt;/p>
&lt;p>Will the fig I choose have wrinkled before I even get to it?&lt;/p>
&lt;p>It&amp;rsquo;s certainly unproductive and paranoid to assume that when things are going well the rug will suddenly be pulled out from underneath you. Tragic is the figure who spends all summer worrying about provision for winter. Nonetheless, the need to look up in one&amp;rsquo;s career is universally relevant and essential. To assume that there will always be jobs, demand and money is at best imprudent and at worst dangerous. Most of all, to assume that what you&amp;rsquo;re doing right now will still be a personally rewarding and challenging career in twenty years time - or to assume that you will be swept into new and exciting and challenging areas by forces beyond your control - is wishful thinking.&lt;/p>
&lt;p>I&amp;rsquo;m not saying you should sit down and plan out a roadmap for your entire career, or even that you could if you wanted to. But it is important to have some idea of what you hope to find when you reach the upper echelons of your profession. To not consider what lies at the end of the branch is to reactively feel your way through your career based solely on your current circumstances. But by picking a fig to progress towards you afford yourself context and foresight that will inform the choices you make along the way.&lt;/p>
&lt;p>In the short term this can mean recognising where areas relevant to your current job will flourish and adjusting your climb accordingly, say, learning a new programming language because you think it will be a big deal in a few years time. And in the longer term, having that aspirational fig at the end of the branch will help you make life decisions that go deeper and are ultimately more rewarding than the impulsive snatch and grab of the young careerist.&lt;/p>
&lt;p>Just as it&amp;rsquo;s important not to be frozen with indecision, it&amp;rsquo;s pointless to start climbing without aiming for something that transcends the situation you&amp;rsquo;re in now. Otherwise you might reach the top and still starve to death, albeit whilst looking at a nicer view.&lt;/p></description></item><item><title>The dire state of Wordpress</title><link>https://jshakespeare.com/the-dire-state-of-wordpress/</link><pubDate>Tue, 19 Mar 2013 07:09:40 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/the-dire-state-of-wordpress/</guid><description>&lt;p>Wordpress powers a massive tranche of the world&amp;rsquo;s websites - &lt;a href="http://en.wordpress.com/stats/">17.4% of them&lt;/a> in fact, at the time of writing. Over the years its huge community and ease-of-use as a CMS has won it favour with web developers looking for out-of-the-box solutions to homogeneous client briefs. The admin is friendly and clients tend to get to grips with it quickly, making it a good choice for agency environments where the customer may not be savvy enough to wrap their heads around a bespoke but hokey UI.&lt;/p>
&lt;p>Its weakness lies in how it has evolved, or rather mutated, from a blogging platform to pseudo-framework in a highly inelegant way. Functionality has been added along the road to accommodate WP&amp;rsquo;s growing use as a CMS: custom post types, custom menus etc - but really these are just hacks to a platform that fundamentally still behaves like blogging software.&lt;/p>
&lt;p>When you learn PHP from Wordpress as I (and probably many other people) did, you just assume that all the idiosyncrasies and illogicalities are par for the course when building a CMS-driven site. It&amp;rsquo;s taken me several years to realise that it doesn&amp;rsquo;t have to be like this. In fact, it wasn&amp;rsquo;t really until I started learning more about OO principles and working with frameworks that I started looking at WP in a different light. Why &lt;em>is&lt;/em> every instance of every model treated like a post? Why can&amp;rsquo;t I extend the functionality of a post type without writing a whole mess of hooks, filters and functional code into a file that isn&amp;rsquo;t semantically related to that post in any way?&lt;/p>
&lt;p>These may sound like the gripes of someone whose needs will never be met by something like Wordpress, but the lack of fundamental logic that gives rise to the above issues is what makes Wordpress problematic for developers at all levels. And because the crossover on the venn diagram of &amp;lsquo;people who know good application development practises&amp;rsquo; and &amp;lsquo;people who build sites in Wordpress&amp;rsquo; is such a thin sliver, it just trundles along in its current hokey state, baffling both the unseasoned and the proficient developer alike.&lt;/p>
&lt;p>Before leaving my last job I had to hand over several codebases to PHP developers who work mainly in Symfony. If I&amp;rsquo;d had a penny for every raised eyebrow when I was talking them through it I could have retired there and then. I&amp;rsquo;d also just spent a few weeks with a junior employee getting him up and running with WP sites. Trying to describe how code works on a platform that has the kind of fundamental logic that Wordpress does is like trying to teach a language that doesn&amp;rsquo;t have an alphabet. I would discourage anyone from learning it until they’re more au fait with a platform that operates on better principles. Basically, Wordpress isn&amp;rsquo;t a platform suited to anyone except those unlucky enough to have somehow become Wordpress developers.&lt;/p>
&lt;p>If you think I&amp;rsquo;m exaggerating or being deliberately hyperbolic to prove a point, take a look at &lt;a href="https://github.com/WordPress/WordPress/tree/master/wp-content/themes/twentytwelve">Twenty Twelve&lt;/a>, currently the default theme for WP and developed by Automattic. (Why is it in a directory called &amp;lsquo;wp-content&amp;rsquo; when it&amp;rsquo;s actually a bunch of views? Who knows). Click through to &lt;a href="https://github.com/WordPress/WordPress/blob/master/wp-content/themes/twentytwelve/functions.php">functions.php&lt;/a> - one file to manage ALL the extraneous functionality specified by the theme. This file contains functions for customising registration page layouts, comment handling, printing post meta and so on. HTML snippets sit fragmented between ifs and elses, loops and comment blocks that make Tolstoy look like Dr Seuss. It’s just an ugly, unreadable, unscalable mess of spaghetti code.&lt;/p>
&lt;p>I&amp;rsquo;m not going to list all the things that make WP at odds with best practises, but here are the brass tacks:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Views, controllers and data are mangled together in a way that makes even the most well-written themes hard to maintain.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Helper functions are &lt;a href="http://lorib.me/code/dont-get-the-permalink/">inconsistent&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>There are no rules or best practices for theme development, so inheriting legacy code is a nightmare.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Designers and front-end developers struggle to make changes because their files are full of functional code&lt;/p>
&lt;/li>
&lt;li>
&lt;p>PHP developers struggle to make changes because their files are full of template code&lt;/p>
&lt;/li>
&lt;li>
&lt;p>It&amp;rsquo;s not optimised for speed of development or speed of operation.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>There are about fifty ways to perform any given operation in Wordpress, but determining which way is best pretty much depends who you talk to. Even the &lt;a href="http://codex.wordpress.org/Function_Reference/query_posts">documentation&lt;/a> is chock full of caveats about obscure scenarios in which you should and shouldn’t use certain functions&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="why-is-all-this-a-problem">Why is all this a problem?&lt;/h2>
&lt;p>Wordpress&amp;rsquo; growth isn&amp;rsquo;t going to stop. For agencies and designers it&amp;rsquo;s becoming a de facto platform for cookie-cutter websites with a development time of roughly 10-100 hours. This isn&amp;rsquo;t necessarily a problem if you&amp;rsquo;re a competent developer and know the ins and outs of WP, but undoubtedly anyone working in this field will inherit legacy WP code from time to time. And because Wordpress doesn&amp;rsquo;t insist on any kind of template structure beyond a basic naming convention, codebase inheritance requires an unsustainable investment of time to figure just what the hell is going on. Even a fairly tidy theme probably won’t look remotely like any of yours.&lt;/p>
&lt;h2 id="why-not-just-use-something-else">Why not just use something else?&lt;/h2>
&lt;p>Because Wordpress is still far and away the best off-the-shelf platform for small to medium size websites. It has arguably the best UI, a ton of features that come for free, a huge community of developers and all of the benefits that come with being a hugely popular piece of software. These things are much harder to come by than a decent codebase.&lt;/p>
&lt;h2 id="what-should-be-done">What should be done?&lt;/h2>
&lt;p>Wordpress needs more rules. People treat it like a framework and largely it is, but it lacks the scalability and rules that make frameworks worth using. Instead of forcing front-end developers and back-end developers to share the same resources there needs to be some separation of design and functionality. Having more rules would make inheriting code and working in a team easier. It would mean developers with framework-agnostic coding principles would be able to quickly get up and running within Wordpress sites without having to learn a backwards and entirely domain-specific vernacular.&lt;/p>
&lt;h2 id="wordpress-is-oss-why-dont-you-shut-up-and-do-it-yourself">Wordpress is OSS, why don&amp;rsquo;t you shut up and do it yourself?&lt;/h2>
&lt;p>The kind of rewrite I&amp;rsquo;m talking about would require a fundamental rethink of the entire platform. We&amp;rsquo;re talking thousands of man-hours for no direct financial reward. If Automattic were to do this it would require a shakeup of their business model, given they don&amp;rsquo;t make any money directly from the Wordpress codebase.&lt;/p>
&lt;p>But this doesn&amp;rsquo;t mean it couldn&amp;rsquo;t or shouldn&amp;rsquo;t be attempted. If Wordpress is going to continue to power the world&amp;rsquo;s highest traffic websites then it&amp;rsquo;s in the interest of a vast number of developers to refactor it into a logical, scaleable, easily inheritable platform. As I see it, the solution would be to migrate the functional aspects of Wordpress to a PHP MVC framework, much like Drupal has recently done by incorporating Symfony 2. However, one cannot simply be melded into the other, and a lot of cherry picking would be required to avoid bloat.&lt;/p>
&lt;p>So consider this post to be the start of a &lt;a href="https://news.ycombinator.com/item?id=5407879">discussion&lt;/a> about how this can happen. I ain&amp;rsquo;t quittin&amp;rsquo; you yet, Wordpress.&lt;/p></description></item><item><title>Don't worry that your job is pointless</title><link>https://jshakespeare.com/dont-worry-that-your-job-is-pointless/</link><pubDate>Mon, 11 Mar 2013 16:11:42 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/dont-worry-that-your-job-is-pointless/</guid><description>&lt;p>A fellow developer friend and I often used to enjoy validating our career choices by comparing them to the &amp;lsquo;unproductive&amp;rsquo; careers of our investment banker friends who were earning twice as much as us. We would scoff at the life choice of someone to pursue a career so utterly vacuous and money-driven. How could anyone be content to just &lt;em>earn&lt;/em>? Surely the great calling of humankind is to create, to build, to give back.&lt;/p>
&lt;p>But upon recently re-reading Tim Kreider&amp;rsquo;s now famous New York Times article &lt;a href="http://opinionator.blogs.nytimes.com/2012/06/30/the-busy-trap/">&lt;em>The Busy Trap&lt;/em>&lt;/a>, a particular sentence has been replaying itself in my mind. Kreider surmises that &amp;ldquo;if your job wasn’t performed by a cat or a boa constrictor in a Richard Scarry book I’m not sure I believe it’s necessary&amp;rdquo;. If bankers fall into this category, then so surely do web developers.&lt;/p>
&lt;p>I&amp;rsquo;ve known my oldest friend, Pete, since the age of ten. Even at that age he knew that he wanted to be a doctor. Between the ages of ten and eighteen I considered being a physicist, engineer, architect and bohemian skater layabout as career paths before eventually settling on graphic designer - probably the least prestigious job on that list and one I haven&amp;rsquo;t even ended up doing. Pete worked hard right through school, sixth form and university, and now when I see him at Christmas and ask him what he&amp;rsquo;s been up to he&amp;rsquo;ll say &amp;ldquo;saving babies&amp;rdquo; or &amp;ldquo;working at a hospital in Uganda - saving babies.&amp;rdquo; To say that I&amp;rsquo;ve been optimising the Javascript performance of a website for a company no one&amp;rsquo;s ever heard of sounds fairly trite by comparison.&lt;/p>
&lt;p>The fact is that if all the web developers (or investment bankers) disappeared tomorrow the world wouldn&amp;rsquo;t change much - at least not compared to doctors or nurses or firemen. There wouldn&amp;rsquo;t be a global meltdown. People wouldn&amp;rsquo;t die. And even though we try to justify our jobs in a universal sense by saying we&amp;rsquo;re &amp;lsquo;disrupting industries&amp;rsquo; or &amp;lsquo;democratising information&amp;rsquo; or whatever else, ultimately these tend to just be mantras we repeat to make ourselves feel like our work is more than just &amp;lsquo;doing what we want to do&amp;rsquo;. In reality, we do what we do because we&amp;rsquo;re good at it and doing it makes money. And if we are to be at peace with this, with doing the same thing every day for all our adult lives, there becomes a necessity to align our sense of purpose and self-worth with it.&lt;/p>
&lt;p>I’ve certainly experienced my share of cognitive dissonance when it comes to determining the social value of my career. But, like most people in their twenties, I find it very hard not to derive a lot of my self-worth from that career. For any recent graduate taking their first steps onto the ladder, a job is the culmination of fifteen years of education and all of the hard work, money and striving that comes with them. It seems like the most important thing in the world because it&amp;rsquo;s what finally qualifies us as grown-ups. In career terms, being in your twenties is a time to be selfish, to take what you can and claw your way to the top. The consideration as to whether or not this career, this badge that we wear so proudly, is actually of any significance to the universe can wait until later. Right?&lt;/p>
&lt;p>Wrong. Part of the reason people burn out and have mid-life crises is because they reach a point where they realise that they&amp;rsquo;ve given the best years of their life to a career that didn&amp;rsquo;t really mean anything. And because all their self-worth was built around their job title, when that becomes devalued then so does everything else.&lt;/p>
&lt;p>The only healthy way to come to terms with doing a non-altruistic day job is to realise that life will offer you more opportunities to do good than you will find within the four walls of an office. There are plenty of people that do Richard Scarry jobs that don’t care what they do for a living as long as they can afford the lifestyle they want. Just as there are probably investment bankers who only care about making a ton of money so they can make life more comfortable for other people. The point is that if you obsess about pursuing a worthy career path, deluded by the sense that it&amp;rsquo;s the only way you will be a useful person, then you may well be forgoing the chance to profit from the talents that you were born with. Just because optimising Javascript isn&amp;rsquo;t making the world a better place right this second doesn&amp;rsquo;t mean you&amp;rsquo;re destined to a life of misanthropic selfishness.&lt;/p>
&lt;p>Let&amp;rsquo;s look at an example from my industry: Bill Gates. He founded and ran Microsoft on a policy of aggressive expansion and saturated the market with software that wasn&amp;rsquo;t really that great. But throughout his career he&amp;rsquo;s used his immense wealth to do more to alleviate poverty and disease around the world than he ever could had he decided to embark upon some virtuous and altruistic career thirty years ago.&lt;/p>
&lt;p>I suppose what I&amp;rsquo;m saying is: don&amp;rsquo;t fret that your day job isn&amp;rsquo;t necessarily useful or altruistic or virtuous, but at the same time ensure that you are not defined by that job. A job provides the means, not necessarily the venue, to do good things.&lt;/p>
&lt;p>&lt;em>Join the discussion on &lt;a href="https://news.ycombinator.com/item?id=5374387">Hacker News&lt;/a>.&lt;/em>&lt;/p></description></item><item><title>Building for humans is hard</title><link>https://jshakespeare.com/building-for-humans-is-hard/</link><pubDate>Sat, 23 Feb 2013 11:00:04 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/building-for-humans-is-hard/</guid><description>&lt;p>The greatest successes in the field of HCI are those that bend technology to the will of human need. But bridging the gap of silicon and flesh is arguably one of the hardest challenges that faces those working in the tech industry. Building anything technical requires technical thinking, but to step away from that and consider human factors, then back into the technical realm to serve those factors, is a rare skill indeed.&lt;/p>
&lt;p>It is for this reason that getting it right with humanistic technical products is really hard. Making things work well for humans and for machines requires constant realignment of thinking. When writing code, one must be detail-oriented and take a granular approach. But as users we tend to view things in a holistic, top-down way: at first making sense of interface, then functionality, then breaking down individual process in our head. So in order to build something that is functionally sound but also usable, one must continually shift their perspective from bottom-up to top-down.&lt;/p>
&lt;p>How then do we make the process of building human-friendly products easier? The most obvious and common approach is to do things in teams. If one person is focused on usability and one person on making it work at a technical level, then the end product will be informed by both machine and human-influenced thought processes. In reality, however, even close-knit teams are subject to drifting from a common goal. And what if you&amp;rsquo;re going it alone? There are certain heuristics that can be applied to humanistic design regardless of team size.&lt;/p>
&lt;h2 id="be-ruthless-about-mvp">Be ruthless about MVP&lt;/h2>
&lt;p>Countless reams of blog posts and books have been written on the importance of sticking to an MVP, but more often than not this is for the simple reason that scope-creep and bloated functionality result in blown deadlines and budgets. When building a humanistically designed product, MVP becomes important becuse that added complexity can easily make your product less suited to the need of the target user.&lt;/p>
&lt;h2 id="talk-to-people">Talk to people&lt;/h2>
&lt;p>No web-based service that has ever gained substantial traction has got away with not thinking about how to tailor their product to real people. Who are you really building for? Children? Adults? Restauranteurs? French horn players? Even if you think you are building for yourself then at least find someone like-minded and get them to take a look at your product. The value of external input should never be overlooked for the simple reason that they don&amp;rsquo;t know what&amp;rsquo;s going on behind the scenes. While your mind is cluttered with wireframes and code, they are innocent and bright-eyed, unfettered by the hours you spent tearing your hair out fixing bugs. A fresh pair of eyes can do something that yours can never do, and that is to see the product in a completely holistic way.&lt;/p>
&lt;h2 id="dont-be-a-magpie">Don&amp;rsquo;t be a magpie&lt;/h2>
&lt;p>It&amp;rsquo;s easy to be distracted by the possibilities afforded to you by shiny new technology. But in order to keep your product lean and user-centric it&amp;rsquo;s important to evaluate any new technology through the filter of &amp;ldquo;is this improving the product for the user&amp;rdquo;?&lt;/p>
&lt;p>Just because a couple of extra lines of code make your product navigable via webcam-based semaphore detection doesn&amp;rsquo;t mean you should necessarily add them. The added layer of complexity, i.e. having to learn semaphore to use your product, can easily put people off. An even greater danger is basing your entire product around a new technology. Indeed, part of the reason so many startups fail is because they try to use technology to solve problems that no users actually have, and therefore end up with a technically exciting but ultimately useless product.&lt;/p>
&lt;h2 id="wear-hats">Wear hats&lt;/h2>
&lt;p>Wear a developer hat when you&amp;rsquo;re knee-deep in views and controllers, but routinely take that hat off, put it to one side, and put on your user hat. Stop thinking about the functionality of your product and look at it from the perspective of a human being. Always thinking like a machine will only result in a machine-friendly product. It&amp;rsquo;s important to take a step back and inject some humanity into what you&amp;rsquo;re building.&lt;/p></description></item><item><title>Self-started projects: invest as much as you can, as early as you can</title><link>https://jshakespeare.com/self-started-projects-invest-as-much-as-you-can-as-early-as-you-can/</link><pubDate>Thu, 27 Dec 2012 20:03:08 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/self-started-projects-invest-as-much-as-you-can-as-early-as-you-can/</guid><description>&lt;blockquote>
&lt;p>&amp;ldquo;Think about it really hard, then forget about it&amp;rdquo;&lt;/p>
&lt;/blockquote>
&lt;p>—Don Draper, Mad Men&lt;/p>
&lt;p>This oft-quoted soundbite may well be the essence of the creative process. Great ideas do not come whilst sitting at a desk for eight hours a day but whilst taking a shower or immediately before falling asleep. It&amp;rsquo;s the methodology of the &amp;lsquo;creative soak&amp;rsquo;: just as dishes are easier to clean when they&amp;rsquo;ve been sitting overnight in the sink, a project is much easier to tackle when it&amp;rsquo;s been mulled over by the subconscious mind for a few days. It&amp;rsquo;s about engaging as much of your brain as possible to the task at hand, so that even your down-time becomes potential brainstorming territory.&lt;/p>
&lt;p>Yet when it comes to self-started projects, lacking the driving forces of external deadlines or budget, it&amp;rsquo;s not enough to just &amp;rsquo;think hard&amp;rsquo; about something. It&amp;rsquo;s important to invest assets to offset the inevitable decay of enthusiasm and flow of ideas over time. Existing commitments will often come first and increase the decay rate of your idea. To ensure that you stay enthusiastic and on track it&amp;rsquo;s important to raise the stakes as quickly as possible.&lt;/p>
&lt;p>There are several ways to do this. The first and most obvious form of capital is time. Time is arguably the most valuable commodity at your disposal and is how we judge the worth of most things in life - especially in a professional sense. The sooner you start pouring time into a project, the sooner it will attain a valuable status in your life. In other words, you&amp;rsquo;re much less likely to let your killer idea slide to the bottom of the pile if you&amp;rsquo;ve already logged a few days of your time against it.&lt;/p>
&lt;p>Money is another form of investment, and one which has a universal value. It&amp;rsquo;s not always as appropriate to spend money on an idea as it is to spend time, but if there are physical things needed to progress the idea, get them early on. A domain name is an obvious example.&lt;/p>
&lt;p>Another asset is reputation. Start talking about your idea, telling friends and colleagues. If you&amp;rsquo;re enthused about something then people will ask you about it the next time they see you - &amp;ldquo;how&amp;rsquo;s that project going?&amp;rdquo;. If your response is always that you&amp;rsquo;ve &amp;ldquo;been too busy&amp;rdquo; then you&amp;rsquo;re damaging your own reputation, so this should give you further incentive to knuckle down.&lt;/p>
&lt;p>What you should be aiming for by investing these assets is to assign value to your idea so that you come to care about it. Get your idea to the point where, by not working on it, you feel guilty and it nags at you.&lt;/p>
&lt;p>Of course, it&amp;rsquo;s important to remember that you do not have unlimited assets. Just as you wouldn&amp;rsquo;t bet all your chips on every hand you&amp;rsquo;re dealt in poker, don&amp;rsquo;t start throwing time, money and reps at every idea that pops into your head. There is no metric for measuring the viability of ideas, but the chances are you will know when you hit upon something good. When that idea comes, write it down and immediately start allocating resources to invest in it. Over time you will get better at recognising where you will get a good ROI.&lt;/p></description></item><item><title>Product placement is fine (in its place)</title><link>https://jshakespeare.com/product-placement-is-fine-in-its-place/</link><pubDate>Thu, 29 Nov 2012 10:10:08 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/product-placement-is-fine-in-its-place/</guid><description>&lt;p>With great movie franchises come great advertising opportunities, and few are more lucrative than James Bond. &lt;em>Skyfall&lt;/em>, being the most expensive Bond film to date, has had a heavy and controversial reliance on product placement that has seen historic advertising contracts (Heineken having paid a reported £28 million for the usurping of the iconic vodka martini).&lt;/p>
&lt;p>This isn&amp;rsquo;t going to be another polemic about the inclusion of very &amp;rsquo;non Bond&amp;rsquo; brands in this film - the internet has been awash with them since the film&amp;rsquo;s release - and it would be naïve to think that a movie with such a tumultuous production history and sky-high budget would have been possible without such deals. I will make this observation about product placement in films, though: it has the unfortunate effect of alienating only the people who care most about films. Product placement &amp;lsquo;done right&amp;rsquo; is the ideal marketing tool for most brands, making a subliminal connection in all but a handful of viewers who, whilst not having noticed its presence during the film itself, can be targeted outside the cinema with cross-media advertising that will help cement the product in their minds. The problem is that these are the casual cinema-goers, the people that whisper &amp;ldquo;is that the guy from earlier? I thought he was dead&amp;rdquo; and seem to absorb only the most tacit elements of the plot required to string together some form of narrative in their heads.&lt;/p>
&lt;p>But for the people that watch a lot of films, the people that spend a not inisgnificant portion of their income on DVDs and cinema tickets and Netflix subscriptions (ie, the consumers actually contributing to the ongoing existence of the industry), the syntax of the motion picture is well established. This means that when the close-up shot rests on Daniel Craig&amp;rsquo;s hand changing gear for just a fraction of a second too long so as to highlight his Omega wristwatch, it&amp;rsquo;s like having the demon head of the exorcist flash onto the screen.&lt;/p>
&lt;p>Suddenly the whole fun of engrossing yourself in a film - spotting subtle gestures of the actors, predicting plot twists, admiring sets and costume - becomes a punishment. Like the boom mic dropping into shot or the extra looking into the camera for a split second, it jars the viewer back into the real world and reminds them that it&amp;rsquo;s just a film after all.&lt;/p>
&lt;p>This may seem a minor gripe for a phenomenon that is, after all, facilitating the production of increasingly spectacular movies in an industry struggling to pull in revenue, and furthermore one that only blights the $100m+ budget films. Honestly, to say that the product placement in Skyfall was any more than a minor annoyance would be an exaggeration. But it is worthy of discussion as it raises the very real prospect that product placement could become a defacto source of funding for big budget films. In an age where movie studios know a picture will be watched illegally by an increasing number of people it seems the only sure-fire way to guarantee the books will be balanced. What&amp;rsquo;s more, it provides risk-free capital before the film is even made, making the film more financially attractive to all parties and helping attract further investment.&lt;/p>
&lt;p>That&amp;rsquo;s really what this comes down to - catering to the people with the money. Film is the last virgin territory of the marketeer, every other form of media having become saturated, if not designed around, advertising. In an age where entertainment is considered a basic human right, it follows that studios would rather chase investment than revenue, given how reluctant consumers are to give up their hard earned cash in exchange for a few hours&amp;rsquo; spectacle.&lt;/p>
&lt;p>So let&amp;rsquo;s hope that for now product placement stays in the realm of the mega-budget summer blockbusters. Because the more money these movies make, the more likely it is that the quirky, independent film is to get produced the following year.&lt;/p></description></item><item><title>Being a jack-of-all-trades to master one</title><link>https://jshakespeare.com/being-a-jack-of-all-trades-to-master-one/</link><pubDate>Sat, 03 Nov 2012 11:57:31 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/being-a-jack-of-all-trades-to-master-one/</guid><description>&lt;p>People that have known me for a few years will know that I never planned to be a web developer, but rather to go into graphic and print design. In fact, until about six months ago the header on this site still read &amp;lsquo;portfolio of web and graphic designer James Shakespeare&amp;rsquo;. It was always my dream to design great logos, books, magazines and typefaces,  one day having my own graphic design studio and being the next Alan Fletcher. I only began designing web pages as a means of making money while I was still at school, and by the time I started my BSc I realised I was more interested in the implementation of good websites than I was in simply how they looked.&lt;/p>
&lt;p>As my time at university progressed I moved from being a sketchbook-carrying art student to sandal and sock-wearing, monitor-propped-up-with-O&amp;rsquo;Reilly-books developer, covering a lot of ground in between. When I moved into the &amp;lsquo;real world&amp;rsquo; I was worried that I&amp;rsquo;d spread myself too thinly across the web design spectrum and would only be valuable to small, under-skilled businesses requiring a full-stack developer. I was scared of specialising because I felt I would be wasting the skills I had learned in other areas of the web design process.&lt;/p>
&lt;p>What I didn&amp;rsquo;t realise was that having this broad, jack of all trades background is probably the best way to become good at what you do. Having a good grasp of what other people in your team are doing makes you much better at your job. By taking an active and ongoing interest in the entire process of your company&amp;rsquo;s output you not only become better aware of the factors that affect how you do your job, but you also stay hungry and excited for the new ways in which the industry is moving. It also ensures that as a person you don&amp;rsquo;t stagnate. If you aren&amp;rsquo;t constantly pushing at the edges of what is considered to be the remit of your job you will trap yourself in a bubble that prevents career development and will soon leave you behind the times.&lt;/p>
&lt;p>Yesterday I sat down and talked to one of our senior developers for about an hour on how AWS works, what services they offer, and how to set up new instances and databases. I&amp;rsquo;m not a sysadmin, and that was an hour I could have spent swotting up on ways to better deliver projects I was working on, or just hacking out more code. Instead I now have a better grasp of the machines my code runs on, how much that costs us and how I could (but hope never to have to) move data around or restore machines. I&amp;rsquo;ve increased the number and breadth of potential conversations I can have with other developers, clients, maybe even sysadmins.&lt;/p>
&lt;p>Perhaps the best thing about taking this approach to your career: you become more aware of the challenges and pressures facing your coworkers, and what is required of them to produce the assets for you to do your job and vice versa. That&amp;rsquo;s not a career thing per se, it just makes you a nicer person to work with.&lt;/p></description></item><item><title>Why App.net (unfortunately) won't work</title><link>https://jshakespeare.com/why-app.net-unfortunately-wont-work/</link><pubDate>Mon, 22 Oct 2012 22:20:52 +0000</pubDate><guid isPermaLink="true">https://jshakespeare.com/why-app.net-unfortunately-wont-work/</guid><description>&lt;p>I&amp;rsquo;ll start out by saying that while this post will probably come across as cynical, I do genuinely want &lt;a href="http://join.app.net">App.net&lt;/a> to work. Competition is always healthy  and what I&amp;rsquo;ve seen of the Alpha platform so far is encouraging. Anyone who can raise $750,000 on Kickstarter is clearly onto something and should be taken seriously. There are, however, fundamental issues with the project&amp;rsquo;s approach that are at odds with successful user-based services on the web.&lt;/p>
&lt;h2 id="twitter">Twitter&lt;/h2>
&lt;p>Let&amp;rsquo;s get this one out of the way first. While App.net doesn&amp;rsquo;t claim to be a competitor to Twitter, people will always compare the two. Describing App.net to a layperson in these early stages always results in the response: &amp;ldquo;Oh, a micro-blogging service? Like Twitter?&amp;rdquo;. Now I&amp;rsquo;m not suggesting that Twitter&amp;rsquo;s dominance of this medium should go unchallenged. The problem is this: &lt;em>Twitter is fine&lt;/em>. It has its flaws, sure; the API is a little hokey and not amazingly well documented, it goes down from time to time, etc. But it&amp;rsquo;s not doing anything badly enough to make users cry out for an alternative. As a developer (and historically amongst the main audience for the project) I really don&amp;rsquo;t have any major problems with Twitter. In fact, if nothing else, App.net would simply become yet another platform to cater for, and as a lazy person I am opposed to this.&lt;/p>
&lt;h2 id="dalton-caldwell-is-building-fordalton-caldwell">Dalton Caldwell is building for Dalton Caldwell&lt;/h2>
&lt;p>Watching the introductory &lt;a href="http://vimeo.com/48111032">video&lt;/a> for App.net, presented by their founder and CEO, one thing jumped out at me. He says the words &amp;lsquo;me&amp;rsquo;, &amp;lsquo;I&amp;rsquo; and &amp;lsquo;we&amp;rsquo; a hell of a lot. This project hasn&amp;rsquo;t arisen from some great outcry amongst web users, it&amp;rsquo;s arisen as a personal solution for Caldwell&amp;rsquo;s own sense of feeling &amp;rsquo;let down&amp;rsquo; by free web 2.0 services. There&amp;rsquo;s nothing wrong with building for yourself, as long as you don&amp;rsquo;t expect anyone else to care. I don&amp;rsquo;t doubt that there are people out there that share his sentiment, and clearly there must be given the project&amp;rsquo;s overwhelmingly popular response on Kickstarter; but when building a social network, solipsism isn&amp;rsquo;t a great starting point.&lt;/p>
&lt;h2 id="kickstarter-investment-isnt-real-value">Kickstarter investment isn&amp;rsquo;t real value&lt;/h2>
&lt;p>While App.net did raise a hugely impressive three-quarters of a million on Kickstarter, this figure isn&amp;rsquo;t really as significant as it sounds. Because it is made up of thousands of small donations, as opposed to several large ones, the money doesn&amp;rsquo;t carry as much weight as that raised through VC or angel investment. The donators aren&amp;rsquo;t looking for good ROI and therefore aren&amp;rsquo;t evaluating it as a business venture, just as something they&amp;rsquo;d like to see. Lots of things raise money on Kickstarter, and they &lt;a href="http://www.outsideonline.com/blog/outdoor-adventure/fatbike-expedition-comes-to-a-halt.html">aren&amp;rsquo;t always profitable&lt;/a>.&lt;/p>
&lt;h2 id="it-isnt-open-source">It isn&amp;rsquo;t open source&lt;/h2>
&lt;p>Despite the obvious attempt to appeal to developers, bizarrely App.net have overlooked what would surely be the best way to get people on board: open sourcing it. All the developers I know are cynical when it comes to branding and spiel, but give them an opportunity to tear something apart and figure out how it works and they&amp;rsquo;ll be more likely to get on board. What&amp;rsquo;s more is that a developer account for access to the API currently stands at $100 p/a - surely a huge barrier to casual developers and hackers looking to play around.&lt;/p>
&lt;h2 id="it-will-never-go-mainstream">It will never go mainstream&lt;/h2>
&lt;p>One of the big selling points of App.net is that they are &amp;lsquo;selling their product, not their users&amp;rsquo;. By using a subscription payment model, there is no catering to advertisers or selling off data to third parties.  This is certainly a healthy ideology, and there&amp;rsquo;s no denying that there are serious issues facing data privacy on social networks. The problem is that most people simply don&amp;rsquo;t care about data privacy. Facebook&amp;rsquo;s questionable approach to data protection has been blown open in the past few months, but has it affected user figures?&lt;/p>
&lt;p>As for as advertising is concerned, advertisements invade our privacy every day, across every imaginable form of media. They are often well-targeted and invasive but we learn to filter them out. Premium models do work: mobile apps offering an ad-supported but free version or a no-ad premium version are becoming the norm; but the majority will always go for the free version. In this instance, the free version is Twitter.&lt;/p>
&lt;p>In fact, I&amp;rsquo;d go so far as to say that even if App.net were free, it would still not attract a paradigm-shiftingly large user base. Take Google+ as an example - it&amp;rsquo;s a better product than Facebook in almost every way, but it hasn&amp;rsquo;t attracted a large user-base because Facebook is so well established. For the average user it would be a hassle to migrate over to a new platform, and would mean bringing a substantial quotient of contacts with them (I tried this last year with Google+ and believe me, it doesn&amp;rsquo;t work).&lt;/p>
&lt;p>None of this is a problem if App.net is only interested in cultivating a niche, developer-centric community, but do developers really just want to build things for other developers? Given the choice between building an app for a potential platform of 100,000 or 100m, I know which one I&amp;rsquo;d choose - especially if it has commercial intent.&lt;/p>
&lt;p>I&amp;rsquo;d love to look back on this post in a couple of years time in the way we know scoff at news videos from the 80s saying nothing will ever come of the Internet. Sadly though, that&amp;rsquo;s not going to happen until App.net start talking to more users and fewer Silicon Valley gentry.&lt;/p></description></item></channel></rss>