<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0' xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>David Weldon</title>
    <description>Co-Founder and CTO at Edthena</description>
    <link>https://dweldon.silvrback.com/feed</link>
    <atom:link href="https://dweldon.silvrback.com/feed" rel="self" type="application/rss+xml"/>
    <category domain="dweldon.silvrback.com">Content Management/Blog</category>
    <language>en-us</language>
      <pubDate>Fri, 28 Oct 2016 20:24:54 +0100</pubDate>
    <managingEditor>dweldon@gmail.com (David Weldon)</managingEditor>
      <item>
        <guid>https://dweldon.silvrback.com/vue-play#28043</guid>
          <pubDate>Fri, 28 Oct 2016 20:24:54 +0100</pubDate>
        <link>https://dweldon.silvrback.com/vue-play</link>
        <title>Storybook for Vue</title>
        <description>How to add vue-play to your vue-cli project</description>
        <content:encoded><![CDATA[<p>I&#39;ve spent the past few months experimenting with <a href="https://facebook.github.io/react/">React</a> and <a href="https://vuejs.org/">Vue</a> in order to familiarize myself with view layers other than <a href="http://blazejs.org/guide/introduction.html">blaze</a>.</p>

<p>I&#39;ve been using a component-first approach when working with these libraries, and found <a href="https://getstorybook.io/">Storybook</a> to be an invaluable tool for react development. Before giving Vue a try, I looked for a Storybook-like alternative and was pleased to find <a href="https://github.com/vue-play/vue-play/">vue-play</a>.</p>

<p>While not as sophisticated as Storybook, vue-play does meet the basic need: show a list of components and their configurations.</p>

<p>@egoist has put a lot of work into making the package as easy to install as possible. In this post, I&#39;ll describe how I added vue-play to my existing <a href="https://github.com/vuejs/vue-cli">vue-cli</a> webpack project.</p>

<blockquote>
<p>I created a more detailed (and updated) set of instructions <a href="https://gist.github.com/dweldon/b6ab44b4f53ffdfad3a77b3157f0833c">here</a>. I will leave the following in place for older versions of vue-play.</p>
</blockquote>

<hr>

<p>The first step is to get the pacakges:</p>
<div class="highlight"><pre><span></span>npm install -D vue-play vue-play-cli
</pre></div>
<p>Note that we&#39;ll be using <a href="https://github.com/vue-play/vue-play-cli">vue-play-cli</a> to make the webpack magic happen. I&#39;ve tried manually configuring webpack for vue-play, and found it to be a huge pain.</p>

<p>Next, add a <code>play</code> script to your <code>package.json</code>. Your <code>scripts</code> section should look like this:</p>
<div class="highlight"><pre><span></span>&quot;scripts&quot;: {
  &quot;dev&quot;: &quot;node build/dev-server.js&quot;,
  &quot;play&quot;: &quot;vue-play start ./src/play --webpack-config ./build/webpack.dev.conf.js&quot;,
  ...
},
</pre></div>
<p>Now add <code>src/play/index.js</code>. I recommend using a script to load all of your scenarios (stories) for you. Mine looks like this:</p>
<div class="highlight"><pre><span></span><span class="kr">import</span> <span class="p">{</span> <span class="nx">configure</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;vue-play&#39;</span><span class="p">;</span>

<span class="c1">// import any global css for your application</span>
<span class="kr">import</span> <span class="s1">&#39;../css&#39;</span><span class="p">;</span>

<span class="kr">const</span> <span class="nx">load</span> <span class="o">=</span> <span class="nx">requireContext</span> <span class="p">=&gt;</span> <span class="nx">requireContext</span><span class="p">.</span><span class="nx">keys</span><span class="p">().</span><span class="nx">map</span><span class="p">(</span><span class="nx">requireContext</span><span class="p">);</span>
<span class="kr">const</span> <span class="nx">scenarios</span> <span class="o">=</span> <span class="nx">load</span><span class="p">(</span><span class="nx">require</span><span class="p">.</span><span class="nx">context</span><span class="p">(</span><span class="s1">&#39;./scenarios&#39;</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="sr">/.js$/</span><span class="p">));</span>
<span class="nx">configure</span><span class="p">(</span><span class="nx">scenarios</span><span class="p">,</span> <span class="nx">module</span><span class="p">);</span>
</pre></div>
<p>In other words, it looks through the <code>src/play/scenarios</code> directory and automatically loads all the <code>js</code> files. This ends up being a lot easier than manually importing them each time one is added.</p>

<p>Finally, we&#39;ll add a scenario - this example will be <code>src/play/scenarios/FancyButton.js</code>:</p>
<div class="highlight"><pre><span></span><span class="kr">import</span> <span class="p">{</span> <span class="nx">play</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;vue-play&#39;</span><span class="p">;</span>
<span class="kr">import</span> <span class="nx">FancyButton</span> <span class="nx">from</span> <span class="s1">&#39;components/FancyButton&#39;</span><span class="p">;</span>

<span class="nx">play</span><span class="p">(</span><span class="s1">&#39;FancyButton&#39;</span><span class="p">,</span> <span class="nx">module</span><span class="p">)</span>
  <span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s1">&#39;default&#39;</span><span class="p">,</span> <span class="nx">h</span> <span class="p">=&gt;</span> <span class="nx">h</span><span class="p">(</span><span class="nx">FancyButton</span><span class="p">));</span>
</pre></div>
<p>When you&#39;re ready, you can <code>npm run play</code> and browse your components. Enjoy!</p>
]]></content:encoded>
      </item>
      <item>
        <guid>https://dweldon.silvrback.com/true-false#23047</guid>
          <pubDate>Sun, 06 Mar 2016 19:57:23 +0000</pubDate>
        <link>https://dweldon.silvrback.com/true-false</link>
        <title>true : false</title>
        <description></description>
        <content:encoded><![CDATA[<p>I try not to be too pedantic about grammar, but I have a few pet peeves. It bothers me when people use <em>less</em> instead of <em>fewer</em>, and don&#39;t get me started on the overuse of <em>literally</em>.</p>

<p>In JavaScript, it feels like nails on a chalkboard whenever I see a conditional which redundantly evaluates to <code>true</code> or <code>false</code>. Take this example:</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">isEmpty</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>Because <code>isEmpty</code> should return <code>true</code> or <code>false</code>, the developer did exactly that. However, he failed to realize that the condition itself is already a boolean. The same function could be rewritten as:</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">isEmpty</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">array</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<p>Here&#39;s an example where the condition is not already a boolean:</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">docExists</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">findOne</span><span class="p">(</span><span class="nx">id</span><span class="p">))</span> <span class="p">{</span>
    <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>In this situation we should use a <a href="https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript">!!</a> to coerce the result of <code>findOne</code>:</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">docExists</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="o">!!</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">findOne</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
<p>And that&#39;s it! Now go write <em>fewer</em> lines of code.</p>
]]></content:encoded>
      </item>
      <item>
        <guid>https://dweldon.silvrback.com/cs2es6#22939</guid>
          <pubDate>Wed, 02 Mar 2016 21:01:36 +0000</pubDate>
        <link>https://dweldon.silvrback.com/cs2es6</link>
        <title>coffeescript =&gt; es6</title>
        <description></description>
        <content:encoded><![CDATA[<p>I&#39;ve been using CoffeeScript for most of my development work since 2011. In general I appreciate the  language&#39;s brevity and features (most of which keep you from shooting yourself in the foot).</p>

<p>With the rising popularity of ES6, I decided to read Axel Rauschmayer&#39;s <a href="http://exploringjs.com/es6/">excellent book</a> and take it for a spin with some of our new projects. Listed below are my initial impressions after having written a couple of thousand lines with it.</p>

<h2 id="variable-declarations">Variable Declarations</h2>

<p>ES6 comes with a ton of features which are not supported in CoffeeScript, such as maps, sets, symbols, generators, and promises. However, I&#39;ve found that <code>let</code> and <code>const</code> are profoundly important. Having block-scoped variables removes a significant number of potential gotchas in JavaScript. <code>const</code>, in particular, provides both a write lock and a visual cue that&#39;s compelling. I use it everywhere I can in our code.</p>

<h2 id="modules">Modules</h2>

<p>I haven&#39;t yet experimented with modules, as they aren&#39;t available in meteor 1.2. However, with 1.3 and beyond, the module syntax will enable the build system to make substantial optimizations including dead code elimination and lazy loading. In other words, once meteor understands how your modules are connected, it can do things like ship smaller binaries and load your code only as needed.</p>

<p>None of these optimizations exist at the time of this writing, but their potential is game changing. Although CS doesn&#39;t have ES6-style imports and exports, meteor will be supporting the feature via CommonJS-style require statements. [1]</p>

<h2 id="punctuation">Punctuation</h2>

<p>CoffeeScript is concise and JavaScript is, comparatively, verbose. My biggest concern about this switch was that I&#39;d end up writing many more parens and curly braces. While this is true, I&#39;ll say there is something liberating about knowing exactly what my code is doing without having to periodically paste snippets into a transpiler. For example, does <code>subtractNumbers 12, addNumbers 5,7</code> actually do what I think it does?</p>

<h2 id="surprises">Surprises</h2>

<p>Even though ES6 was heavily influenced by CoffeeScript, some syntax features didn&#39;t make the cut. Here are the two most glaring omissions:</p>

<p><strong>Existential Operator</strong><br>
In CS, we have the existential operator <code>?</code>, which lets us write <code>x?</code> instead of <code><br>
typeof x !== &quot;undefined&quot; &amp;&amp; x !== null</code>. It&#39;s 2016, and we still don&#39;t have a suitable shortcut for testing existence in JavaScript, which is just absurd.</p>

<p><strong>Arrow Functions</strong><br>
ES6 gave us &quot;fat arrow&quot; functions, but deliberately left out &quot;thin arrow&quot;functions (those which don&#39;t give you a lexical <code>this</code>). While this solves some annoying issues in JS, it introduces the opposite problem where programmers blindly replace <code>function</code> with <code>=&gt;</code> and end up binding the wrong context. When the <code>ecmascript</code> package became available in meteor 1.2 there was a continual stream of questions on StackOverflow related to this misunderstanding.</p>

<p>You may be asking, &quot;but don&#39;t you want a lexical <code>this</code> the majority of the time?&quot; As it turns out, a linted CoffeeScript codebase provides the perfect answer to this question: <strong>no</strong>. At Edthena, 98% of the time we use <code>-&gt;</code>.</p>

<h2 id="linters">Linters</h2>

<p>In the past year, we&#39;ve made heavy use of <a href="http://www.coffeelint.org/">CoffeeLint</a>, which has been an adequate way to enforce style throughout our codebase. Using a linter with CoffeeScript is nice, whereas with JavaScript it&#39;s a necessity because of the potential for leaking global variables (among other things).</p>

<p>The good news is that some combination of <a href="https://github.com/jscs-dev/node-jscs">jscs</a> and <a href="http://eslint.org/">eslint</a> can provide you with solid protection against yourself. The bad news is that you need to have a Ph.D. in eslint in order to configure it. Have a look at the one from mantra&#39;s example app <a href="https://github.com/mantrajs/mantra-sample-blog-app/blob/master/.eslintrc">here</a>. Yikes.</p>

<h2 id="final-thoughts">Final Thoughts</h2>

<p>Overall I enjoy writing in ES6 more than I thought I would, and it&#39;s nice to use the same language as everyone else when posting publicly.</p>

<p>My initial concerns over having a punctuation explosion in our code have been somewhat alleviated by using a decent editor. For those using atom, I&#39;d recommend the <a href="https://atom.io/packages/linter-jscs">jscs linter</a> which can auto-correct your style blunders when saving.</p>

<p>Best of luck to anyone making the switch. Feel free to post a comment and tell us how it&#39;s going.</p>

<hr>

<p>[1] Thanks to <a href="">Loren Sands-Ramshaw</a> for pointing this out.</p>
]]></content:encoded>
      </item>
  </channel>
</rss>