<?xml version="1.0" encoding="UTF-8"?><rss 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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[First, do it - Medium]]></title>
        <description><![CDATA[Iterative (software) development - Medium]]></description>
        <link>https://firstdoit.com?source=rss----92a9d6520edc---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>First, do it - Medium</title>
            <link>https://firstdoit.com?source=rss----92a9d6520edc---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 23 Feb 2024 07:47:34 GMT</lastBuildDate>
        <atom:link href="https://firstdoit.com/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[How to write a React stateless component in TypeScript with children]]></title>
            <link>https://firstdoit.com/how-to-write-a-react-stateless-functional-component-in-typescript-5d150419bbbc?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/5d150419bbbc</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Fri, 05 May 2017 00:03:02 GMT</pubDate>
            <atom:updated>2017-05-05T00:27:15.812Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYJqZHIThlcXbEFFHgIDvw.jpeg" /><figcaption>W. H. Parkinson&#39;s &quot;In The School Room&quot; — 1891</figcaption></figure><blockquote><em>When implementing a functional component for the first time in TypeScript, this pesky error caught me off-guard. Hope this saves your time.</em></blockquote><p>If you&#39;re trying to create a stateless component in TSX you might come up with something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8c4f2e3f806ee336d9ffc8731369105/href">https://medium.com/media/d8c4f2e3f806ee336d9ffc8731369105/href</a></iframe><p>But you will be surprised by this little fellow:</p><pre>ERROR in ./src/components/home/index.tsx<br>error TS2324: Property &#39;children&#39; is missing in type &#39;IntrinsicAttributes &amp; { children: any; }&#39;.</pre><p>Wait, what? Well, never mind the problem. We stand in the shoulders of giants, and the giants <a href="https://github.com/Microsoft/TypeScript/issues/7992">delivered a solution</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb0aa066d9388ef14f09024331c68ca1/href">https://medium.com/media/bb0aa066d9388ef14f09024331c68ca1/href</a></iframe><p>Voilà. Adding the &quot;React.StatelessComponent&lt;{}&gt;&quot; type suffices to make the compiler understand the passed object, because the resulting parameter type becomes:</p><pre>{} &amp; { children?: ReactNode; }</pre><p><em>(Remember, you have to add “</em><strong><em>@types/react</em></strong><em>” to your devDependencies in order to have access to React&#39;s type definitions.)</em></p><p>As a corollary, if you want to have other props passed down to your component it suffices to define them in the object between brackets, e.g.:</p><pre>const Button: React.StatelessComponent&lt;{primary?: boolean}&gt; = ({children, primary}) =&gt; (...)</pre><p>And that&#39;s it.</p><p>Some might argue that TypeScript is verbose. I really think this verbosity (which is optional, and you can buy into on your own time) <strong>really</strong> pays off. If you&#39;re starting a large project, stick around for it and let me know if it worked out.</p><p><em>PS</em>: If you&#39;re wondering what are those insane class names I&#39;m using, boy, you need to <a href="http://tachyons.io/">take a look at Tachyons</a>, by <a href="http://mrmrs.io/writing/2016/03/24/scalable-css/">Adam Morse</a>. Never write CSS again. Do you need a better pitch? (I don&#39;t.)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d150419bbbc" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/how-to-write-a-react-stateless-functional-component-in-typescript-5d150419bbbc">How to write a React stateless component in TypeScript with children</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Change your Mac OS X hostname for a better looking terminal prompt]]></title>
            <link>https://firstdoit.com/change-your-mac-os-x-hostname-for-a-better-looking-terminal-prompt-6aff5246c56d?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/6aff5246c56d</guid>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mac]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Wed, 01 Jun 2016 12:01:45 GMT</pubDate>
            <atom:updated>2016-05-21T08:06:00.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x4g7r5bqyFci2nnFEphAFg.jpeg" /></figure><p>The first command to run on your new Mac:</p><pre>sudo scutil --set HostName &lt;new name&gt;</pre><p>Finally! I can say goodbye to:</p><pre>guilherme@Guilhermes-MacBook-Pro ~&gt;</pre><p>and hello to:</p><pre>guilherme@trinsic ~&gt;</pre><p>PS: You should also check out the awesome <a href="https://www.iterm2.com/">iTerm2</a> terminal emulator along with the excellent <a href="http://fishshell.com/">fish shell</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6aff5246c56d" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/change-your-mac-os-x-hostname-for-a-better-looking-terminal-prompt-6aff5246c56d">Change your Mac OS X hostname for a better looking terminal prompt</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lead by example or don’t lead at all]]></title>
            <link>https://firstdoit.com/lead-by-example-or-dont-lead-at-all-6213aa1c8dea?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/6213aa1c8dea</guid>
            <category><![CDATA[leadership]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[history]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Wed, 01 Jun 2016 12:01:15 GMT</pubDate>
            <atom:updated>2016-05-25T09:06:04.354Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P_3_4TCb5qhQoOFgT4u7FQ.jpeg" /></figure><p>Around 2494 years ago, a battle happened on a narrow coastal pass called <a href="http://en.wikipedia.org/wiki/Battle_of_Thermopylae"><strong>Thermopylae</strong></a>. In this battle, <strong>300 spartans</strong> and a thousand more soldiers led by <strong>King Leonidas</strong> held the pass for <strong>three days</strong> against an overwhelmingly larger Persian army. They were eventually betrayed and lost.</p><p>This weakened and delayed the Persians, but the Greeks were forced to retreat to the <strong>Bay of Salamis</strong> nonetheless. A while later, <strong>Themistocles</strong>, an Athenian general, led the navy to a <strong>decisive victory</strong> at the <a href="http://en.wikipedia.org/wiki/Battle_of_Salamis">Battle of Salamis</a> which broke the Persians and eventually <strong>turned the tide of war</strong>.</p><p>These battles are believed to have <strong>changed the course of history</strong>. By stopping the Persian invasion, they allowed the Greek culture to prosper and develop. Many historians believe that a different result might have <strong>significantly changed the western civilization as we know it</strong>.</p><h3>Leading by example</h3><p>Both battles have something in common: the <strong>Greek leaders</strong> were <strong>absolutely committed</strong> to the result of their armies. Both <strong>offered their lives</strong> and spared no expense.</p><p>Unlike Xerxes, the Persian king who commanded from afar, these generals were <strong>fighting alongside their soldiers</strong>, not above them. They did not ask for anything more than <strong>they themselves were willing to give</strong>.</p><p>And this, in a nutshell, is the meaning of <strong>leading by example: </strong>if you are not interested in fighting the fight along your soldiers, you might as well quit — because they <strong>will</strong> stop respecting you… sooner or later.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6213aa1c8dea" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/lead-by-example-or-dont-lead-at-all-6213aa1c8dea">Lead by example or don’t lead at all</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Chain your solutions with Underscore]]></title>
            <link>https://firstdoit.com/chain-your-solutions-with-underscore-513fba67ec1f?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/513fba67ec1f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[underscorejs]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Wed, 25 May 2016 08:38:14 GMT</pubDate>
            <atom:updated>2016-05-25T08:37:58.949Z</atom:updated>
            <content:encoded><![CDATA[<p>After getting to <a href="https://medium.com/first-do-it/reduce-your-problems-with-functional-programming-getting-started-with-underscore-or-lodash-86d78aad6338#.8if4rsp91">know the basics of Underscore in part I</a>, it’s time to delve a little deeper. If you are not familiar with each, map, or reduce, you should probably read it before proceeding. Otherwise, let’s move on!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s1we3wzUQ1Cil4DGgcmRKA.jpeg" /><figcaption>A _.chain of functions.</figcaption></figure><p>Let’s consider this array of students and these utility functions for the examples:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13187c284ee7f2150b74ac902ce2e48e/href">https://medium.com/media/13187c284ee7f2150b74ac902ce2e48e/href</a></iframe><h3>Filter</h3><p><em>filter</em> gives us a new array which only includes the values that pass a truth test. Let’s get all the approved students names.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27a684bc3c0afe2cb43054506f6c4230/href">https://medium.com/media/27a684bc3c0afe2cb43054506f6c4230/href</a></iframe><h3>Pluck</h3><p><em>pluck</em> extracts a property from each object on a list. Now we can make our previous code simpler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/678a25fac6c730d0e32586227c0f944f/href">https://medium.com/media/678a25fac6c730d0e32586227c0f944f/href</a></iframe><h3>Group By</h3><p>It would be useful to get the averages from each class. <br>Turns out <em>groupBy</em> is just the right tool for the job. It returns a <strong>map</strong>, grouping results in arrays. Let’s have a look:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78ad4173b33396452408b97a8da5ef9e/href">https://medium.com/media/78ad4173b33396452408b97a8da5ef9e/href</a></iframe><p>Notice that the keys of the map are the result of the function application. In this case, we are simply grouping by a property. Underscore offers a simpler way of doing this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40b6b8f7cc7c68497f07316df0258f7f/href">https://medium.com/media/40b6b8f7cc7c68497f07316df0258f7f/href</a></iframe><h3>Flatten</h3><p>But, hey! We didn’t actually get the averages. Well, flatten makes this easy by un-nesting arrays:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fba94c1f9455b1659fbfe8e0cc495f1b/href">https://medium.com/media/fba94c1f9455b1659fbfe8e0cc495f1b/href</a></iframe><h3>Every</h3><p>Let’s find out if all students in a class are approved. You guessed it, <em>every</em> is the tool for the job.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a4de4163f8da84416654af6b7fdb3f0/href">https://medium.com/media/0a4de4163f8da84416654af6b7fdb3f0/href</a></iframe><h3>Chain and value</h3><p>Until now, we have been applying simple, individual transformations to our data. Sometimes, we need to combine multiple functions successively to achieve what we want.</p><p>Luckily, Underscore has got us covered. You can use <em>chain</em> to wrap an array, then call other Underscore functions on it. Then, you use <em>value</em> to unwrap the result. Let’s see an example with chaining:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e6b81caf2a4a3bd692f73699852733a/href">https://medium.com/media/6e6b81caf2a4a3bd692f73699852733a/href</a></iframe><p>As you can imagine, this gives us the minimum grade of all approved students. See? We created a complex operation simply by chaining together Underscore functions. That’s the power of chain.</p><h3>There’s more</h3><p>There are many more interesting functions over at <a href="http://underscorejs.org/">Underscore’s documentation</a>. If you are in doubt on how something works, you can always open <a href="https://developer.chrome.com/devtools"><em>Developer Tools</em></a><em> </em>directly in the Underscore site and try it out! Try copy and pasting the code from these examples there for a start.</p><h3>Getting <em>more</em> functional with Ramda</h3><p>In his excellent <a href="http://www.youtube.com/watch?v=m3svKOdZijA">talk at HTML5DevConf in 2013</a>, Brian Lonsdorf (<a href="https://twitter.com/drboolean">@drboolean</a>) points out that Underscore could better promote functional programming if the parameters were reversed, so as to allow currying and composing.</p><p>If you want to dig deeper, I really recommend watching the talk and getting to know <a href="http://ramdajs.com/">Ramda</a>, which is <em>like</em> Underscore but actually <em>functional</em>. It really lends itself to better programming practices in some impressive ways.</p><p>Thanks for reading, and feel free to send me questions. Glad to help!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=513fba67ec1f" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/chain-your-solutions-with-underscore-513fba67ec1f">Chain your solutions with Underscore</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spotify Engineering Culture — Lessons on Autonomy and Alignment]]></title>
            <link>https://firstdoit.com/spotify-engineering-culture-lessons-on-autonomy-and-alignment-e86abe5ebca7?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/e86abe5ebca7</guid>
            <category><![CDATA[agile]]></category>
            <category><![CDATA[culture]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Wed, 25 May 2016 08:05:08 GMT</pubDate>
            <atom:updated>2016-05-23T08:31:01.572Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q_ISHGhCj3fMFfGIM7iHaw.jpeg" /><figcaption>This giant poster is the result of Henrik Kniberg&#39;s talk on Spotify&#39;s culture.</figcaption></figure><p>From <em>The Lean Startup</em> to <em>Good to Great</em>, from <em>Tribal Leadership</em> to <em>Zero To One</em>, we are constantly discovering new, useful ideas that <strong>guide our day-to-day decisions</strong>.</p><p>Yet few works have captured the essence of a startup as <em>concisely</em> as the <a href="https://labs.spotify.com/2014/03/27/spotify-engineering-culture-part-1/"><strong>Spotify Engineering Culture</strong></a> videos. In the two part series, <a href="https://twitter.com/henrikkniberg">Henrik Kniberg</a> delivers <strong>incredibly impactful startup insights</strong> based on the culture at Spotify.</p><p>In this post I will highlight my favorite quotes from the videos regarding <strong>autonomy and alignment</strong>— why they&#39;re essential to startups and how to achieve them.</p><h3>Rules are a good start, but break them when needed</h3><p>(<a href="http://player.vimeo.com/video/85490944#t=0m26s">00:26</a>)</p><blockquote><em>All models are wrong, but some are useful. <br> — George Box</em></blockquote><p>When a team is in complete chaos, a process like Scrum may be useful to start organising and measuring it’s work. Clear rules have an advantage: it’s pretty obvious if you’re following them or not. This makes it easy for an immature team to advance to a more organised stage.</p><p>However, Scrum isn’t perfect, as multiple <a href="http://agileoverflow.com/t/scrum-seems-to-be-mostly-about-having-better-alibis/47">articles</a> like to <a href="http://lostechies.com/jimmybogard/2012/09/12/why-im-done-with-scrum/">point out</a> from <a href="http://www.writemoretests.com/2011/09/agile-scrum-delivering-broken-software.html">time to time</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KcqRLzS5ilFVixCZcosDqw.jpeg" /></figure><p><strong>Autonomy means being able to choose what works</strong>. A motivated team may soon find itself hampered, instead of helped, by lots of rules. As it matures, a team naturally discovers and selects which rules actually work on their unique context. It’s important to let go of blind discipline and <strong>adapt to the actual needs</strong>.</p><blockquote><em>Rules will eventually slow you down, so you must adapt and keep only the essentials.</em></blockquote><h3>Autonomy makes us fast</h3><p>(<a href="http://player.vimeo.com/video/85490944#t=2m02s">02:02</a>)</p><p>The <a href="http://allthingsd.com/20110405/exlusive-larry-page-mulls-google-reorg/">fall of managers</a> may have been a surprise four years ago, but now it’s pretty common-sense. If you need a layer of people to manage people, you are not being efficient — and you are <strong>definitely not fast enough</strong>. As Paul Graham <a href="http://www.paulgraham.com/boss.html">puts it</a>:</p><blockquote><em>Companies will inevitably slow down as they grow larger, no matter how hard they try to keep their startup mojo.</em></blockquote><p>This happens because, as they grow, companies try to add structure to compensate for harder communication. However, <strong>more structure necessarily means less autonomy</strong>. When people must constantly report and coordinate, they cannot move as quickly.</p><p>Small teams, on the other hand, move fast. Carl Waldekranz, CEO and co-founder of <a href="http://tictail.com">Tictail</a>, believes that <strong>“small teams are automatically in sync at all times”</strong>. He suggests that <a href="https://www.youtube.com/watch?v=_uTR9Cp5DsY&amp;t=573">teams should operate as startups</a>: 3 to 5 people and fully autonomous.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u6LdStv8rHazRr_CplWjUw.png" /></figure><p>There’s a reason why startups look like they work much faster than big corporations: <strong>they actually do</strong>. Maintaining autonomy is paramount to keeping that speed. But there is only one way to scale autonomy…</p><h3>Alignment enables autonomy</h3><p>(<a href="http://player.vimeo.com/video/85490944#t=3m05s">03:05</a>)</p><p>Unmotivated people need very close guidance. They fail to see the big picture and must be told what their next step is, every step along the way. This often happens when leaders choose to micro-manage instead of sharing their vision.</p><p>However, when an objective is clear to a team of motivated people, everyone knows what’s their role in getting there. When people are aligned they do not need to coordinate as heavily. They can make decisions by themselves — and they <em>trust each other</em> to make the right decisions.</p><blockquote><em>The leader’s job is to communicate what problem needs to be solved.</em></blockquote><p>Motivated people who are granted <strong>autonomy and trust</strong> don’t need to be told what to do. They must only be aligned on <strong>what’s the destination</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-Vvoil9jSuOqui9sDI_aw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e86abe5ebca7" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/spotify-engineering-culture-lessons-on-autonomy-and-alignment-e86abe5ebca7">Spotify Engineering Culture — Lessons on Autonomy and Alignment</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick Tip: one liner cookie read]]></title>
            <link>https://firstdoit.com/quick-tip-one-liner-cookie-read-c695ecb4fe59?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/c695ecb4fe59</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cookies]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Fri, 20 May 2016 07:00:20 GMT</pubDate>
            <atom:updated>2016-05-20T06:57:54.144Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJjhWp2Qf4gB-qfnX3lttA.jpeg" /></figure><p>For all the obtuse native functions JavaScript offers (“wat”.blink()?), there isn’t a simple, <em>native</em> way to <strong>parse cookies and get the value of a single key</strong>.</p><p>The inter-webs, however, always provide. In my roamings, I have found what is now my favorite getCookie one-liner:</p><pre>(&#39;; &#39; + document.cookie)<br>.split(&#39;; &#39; + key + &#39;=&#39;)<br>.pop()<br>.split(&#39;;&#39;)<br>.shift()</pre><p>Okay, that’s not <em>exactly</em> an one-liner, but only so that we can understand this beauty step by step.</p><p>Let’s assume we have the following cookie: “first=1; doit=2; cookies=3”</p><h4>Line 1</h4><pre>(‘; ‘ + document.cookie)</pre><p>Cookies are separated by semicolons. As we might be searching for the first key, it&#39;s useful to prefix the entire thing with a semicolon.</p><p>Our cookie now looks like this: “; first=1; doit=2; cookies=3”</p><h4>Line 2</h4><pre>.split(‘; ‘ + key + ‘=’)</pre><p>From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split">MDN</a>:</p><blockquote><em>The split() method splits a String object into an array of strings by separating the string into substrings. When found, separator is removed from the string and the substrings are returned in an array.</em></blockquote><p>Here, we split the string in two parts, creating an array. <br> The first part contains everything <em>before</em> our key. We can ignore that. <br> The second part contains everything <strong>after</strong> our key. <em>Including</em> the value.</p><p>Using the key &quot;doit&quot;, this is what we would get: [“; first=1”, “2; cookies=3”]</p><h4>Line 3</h4><pre>.pop()</pre><p>From <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop">MDN</a>:</p><blockquote><em>The pop method removes the last element from an array and returns that value to the caller</em></blockquote><p>As I promised, we can ignore the first value in that array. Therefore, pop gives us the interesting part: “2; cookies=3”</p><h4>Line 4</h4><pre>.split(‘;’)</pre><p>It’s pretty easy from now on, huh? We’re getting an array whose first index will contain our desired value. The result: [“2”, “ cookies=3”]</p><h4>Line 5</h4><pre>.shift()</pre><p>From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift">MDN</a>:</p><blockquote><em>The shift() method removes the first element from an array and returns that element. This method changes the length of the array.</em></blockquote><p>For the grand finale, we extract the first value of the array.</p><p>Voilà: “2”</p><p>That’s it.</p><p>Cya!</p><pre>(&#39;; &#39;+document.cookie).split(&#39;; &#39;+key+&#39;=&#39;).pop().split(&#39;;&#39;).shift()</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c695ecb4fe59" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/quick-tip-one-liner-cookie-read-c695ecb4fe59">Quick Tip: one liner cookie read</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why "First Do It"? Because you should get started right now.]]></title>
            <link>https://firstdoit.com/why-first-do-it-because-you-should-get-started-right-now-910a9ccd4d4b?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/910a9ccd4d4b</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[iteration]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Wed, 18 May 2016 18:24:52 GMT</pubDate>
            <atom:updated>2016-05-18T18:59:39.355Z</atom:updated>
            <content:encoded><![CDATA[<p>After noticing my username, people sometimes ask me:</p><blockquote><em>What does &quot;first do it&quot; mean?</em> <em>Where does it come from? Why do you feel it is important?</em> <em>Can’t you act like a grown up and go by your own name?</em></blockquote><p>(I wish! If you knew how many Guilherme Rodrigues there are, you’d understand.)</p><p>Turns out there <em>is</em> a pretty good reason: I love this sentence because it helps me focus on the right things, everyday.</p><h3>The origin</h3><p>So, am I just trying to rip off billions of dollars of some giant sports company marketing budget? Well, no. I’m ripping off someone else!</p><p>I came across this expression on an excellent talk about <a href="https://speakerdeck.com/addyosmani/automating-front-end-workflow">Automating Front-end Workflow</a> from the front-end overlord <a href="https://twitter.com/addyosmani">Addy Osmani</a>.</p><p>Specifically, this slide struck a cord with me:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*X2QDjWcOqX_5Pj26.jpg" /><figcaption>This guy knows what he&#39;s talking about.</figcaption></figure><h3>Iterative improvement</h3><p>My whole life, being iterative was one of the hardest things for me. I would spend <em>inordinate</em> amounts of time trying to get something <strong>perfect</strong>, getting frustrated over it, and finally <strong>quitting</strong>. Rinse and repeat.</p><p>Turns out there’s a pretty neat solution to that: knowing that <strong>you won’t get it right the first time around anyway</strong>. I was already coming to terms with this philosophy, but this slide condensed it in a (<em>three</em>) perfect sentence(<em>s</em>).</p><h3>Get started right now</h3><p>The sentence is genius because the <strong>fear of starting</strong> is what <em>actually</em> keeps us from doing so many great things. We often get paralysed, over-analysing every possibility, getting <strong>prepared</strong>.</p><p>You know what? It’s mostly useless. As soon as you get started, you’ll realize how little you <em>could</em> know about it by preparing in the first place.</p><p>There&#39;s no replacement for <strong>getting started right now!</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TccGT7afhafmdHF1O2HGGA.jpeg" /><figcaption>Even Michelangelo had to <strong>make the decision to get started</strong> before turning a piece of marble into the masterpiece Pietà.</figcaption></figure><h3>In sight, in mind</h3><p>I liked it so much I decided to have it in front of me every day. I wanted a constant reminder to myself that said: Hey, don’t worry about perfection. <strong>First do it.</strong></p><p>Well, what better place to put something you want to see every time than in your username?</p><p>And, thus, I became… <strong>firstdoit</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DiDxnDu2hksK8rBSrD48Kw.png" /><figcaption>The universe, when I chose my new username.</figcaption></figure><p>Except in <a href="http://twitter.com/firstdoit">god-damned Twitter</a>. Jesus, Brian. Why won&#39;t you let go of that handle?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=910a9ccd4d4b" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/why-first-do-it-because-you-should-get-started-right-now-910a9ccd4d4b">Why &quot;First Do It&quot;? Because you should get started right now.</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reduce your problems with functional programming — getting started with Underscore or Lodash]]></title>
            <link>https://firstdoit.com/reduce-your-problems-with-functional-programming-getting-started-with-underscore-or-lodash-86d78aad6338?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/86d78aad6338</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[underscorejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Mon, 14 Mar 2016 10:26:13 GMT</pubDate>
            <atom:updated>2016-03-13T16:43:26.871Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OtnHBnR8v0BKpLj6xNyxoA.jpeg" /><figcaption>The road to Functional Programming is long — but very rewarding.</figcaption></figure><p>There are some inflection points when learning JavaScript — moments when you learn something that doesn’t merely add to your productivity, but increase it exponentially.</p><p>Learning <a href="http://underscorejs.org/">Underscore</a> was, for me, one of those moments. It was my first contact with <strong>functional programming</strong> concepts and this, by itself, is a Big Deal®.</p><p>Underscore is a collection of utility functions that free you from a world of pain with nested for’s and if’s by allowing you to organise your algorithms neatly into <strong>data</strong> and <strong>transformations</strong> you need to apply to the data.</p><p>If you never touched Underscore or functional programming, fear not! Let me guide you in your first steps in this long and rewarding road. I promise you won&#39;t be disappointed.</p><h3>Your new best friends — each, map and reduce</h3><p>The most important and commonly used of those functions are:</p><ul><li><strong>each</strong>,</li><li><strong>map</strong>, and</li><li><strong>reduce</strong>.</li></ul><p>Actually, they are so powerful that most other operations can be expressed in terms of these. Let’s get to know them first, because they are the building blocks to thinking in functional terms.</p><h3>Each, the boring brother</h3><p><strong>each</strong> is the equivalent of a simple for. It receives an array, iterates over its values and applies a given function on them.</p><p>In plain JS, you would write:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d64c73ba9ed82bd5d333355a11a2a352/href">https://medium.com/media/d64c73ba9ed82bd5d333355a11a2a352/href</a></iframe><p>Now, some things are unpleasent here. First, you need to deal with the iterating on your own, minding <strong>i</strong> and <strong>value</strong>, which are only the means to an end. Second, your <strong>transformation</strong> is entrenched in the loop. It should be reusable! Let’s fix that first:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b8cf1c4619ba5a85b74f1743d2cccb7/href">https://medium.com/media/0b8cf1c4619ba5a85b74f1743d2cccb7/href</a></iframe><p>Ok, that got a little better. But with <strong>each</strong>, things get even simpler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/845fddbebfde022ab578b278632a673d/href">https://medium.com/media/845fddbebfde022ab578b278632a673d/href</a></iframe><p>Looking good! But there’s a caveat…</p><h3>Side effects are evil</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*yT7F6AkwGS0FMKKz.gif" /><figcaption>Dark side effects</figcaption></figure><p>In our first, simple example we altered the same array we were given. That may seem harmless, but one of the virtues of functional programming you will learn to love is the <strong>immutability of values</strong>.</p><p>This ugly name simply means that values should not be <em>changed</em>, but rather used to generate other values. You learned this in math: a function <strong>maps</strong> one value to another. It never alters the original.</p><pre>f(x) -&gt; x * 2<br>a = 2<br>f(a) // results 4; a is still 2, of course.</pre><h4>Back to our example</h4><p>So, a better implementation of our simple example, removing side effects, would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c47378dcfbd8d1ee92b8b4d050ab2f39/href">https://medium.com/media/c47378dcfbd8d1ee92b8b4d050ab2f39/href</a></iframe><h4>An aside — why purity matters</h4><p>A function that doesn’t has side effects is called a <strong>pure function</strong>. They are important for a number of reasons, but most importantly: because they don’t change any state, they are <strong>very predictable</strong>. This makes it easier to reason about your code.</p><p>It also means that <strong>pure functions are very testable! </strong>Given the same arguments, they will always return the same output. For greater detail, read on about <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a>.</p><h3>Map those values</h3><p>So, now we know we shouldn’t mutate those objects, it’s probably a good time for you to get to know <strong>map</strong>. It receives an array, iterates over its contents applying a transformation and returns a <strong>new array</strong> with the transformed values.</p><p>Let’s make that last, clunky example better!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5bece49230007ae10b3b6bc8bf16add/href">https://medium.com/media/b5bece49230007ae10b3b6bc8bf16add/href</a></iframe><p>Wow. Can you feel it already?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/0*dSiW-Gd_NlKXqeni.gif" /></figure><h3>Reduce it all</h3><p>Sometimes, you are not interested in the individuals, but in an aggregate value. This is precisely what <strong>reduce</strong> is for. It <em>reduces</em> all values in a collection down to a single value.</p><p>It receives an array, a function and an <strong>initial value</strong>. The iteratee function is a little more complex — it receives <em>first</em> the <strong>partial result</strong> so far, and then the <strong>current value</strong>. That’s better understood with an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37cf60500f0f5182cdfe16b7ad42744d/href">https://medium.com/media/37cf60500f0f5182cdfe16b7ad42744d/href</a></iframe><p>For each player in the array, <strong>reduce</strong> will call <em>totalPoints</em>. The first time around, it will use the initial value as the first parameter. The consecutive calls will use the result from the preceding call.</p><p>So, there you have it. You can use <strong>reduce</strong> whenever you want to collect information on all the values from a collection and create something else out of it.</p><h3>Conclusion</h3><p>This has been a very brief introduction to some of the basic tools in the Underscore toolset. However, there is much more to be learned, and those functions alone have far more interesting applications.</p><p>I hope you enjoyed it and hang around for the next part, where we will discuss chaining, using objects instead of arrays and some more advanced functions.</p><p>You can always get started right away with the great <a href="http://underscorejs.org/">documentation</a>, and also get to know <a href="http://lodash.com/">lodash</a>, Underscore’s leanest, meanest younger brother.</p><p><strong>UPDATE</strong> Some people have pointed that I should mention that modern browsers offer native implementations of <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach">forEach</a>, <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map">map</a> and <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce">reduce</a> as methods of Array. Just be careful using them if you plan on supporting older browsers, as you’ll need to <a href="http://remysharp.com/2010/10/08/what-is-a-polyfill">polyfill</a> them!</p><p><em>Originally published at </em><a href="http://firstdoit.com/underscore-1/"><em>firstdoit.com</em></a><em> on September 22, 2014.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86d78aad6338" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/reduce-your-problems-with-functional-programming-getting-started-with-underscore-or-lodash-86d78aad6338">Reduce your problems with functional programming — getting started with Underscore or Lodash</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[No need for globals — using npm dependencies in npm scripts.]]></title>
            <link>https://firstdoit.com/no-need-for-globals-using-npm-dependencies-in-npm-scripts-3dfb478908?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/3dfb478908</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Mon, 14 Mar 2016 10:25:54 GMT</pubDate>
            <atom:updated>2016-03-13T08:40:54.460Z</atom:updated>
            <content:encoded><![CDATA[<p>Did you know npm adds executables to PATH when running scripts?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EOhGyXM_yU7pqraLZqLmrA.jpeg" /><figcaption>A regular npm dependency tree.</figcaption></figure><p><a href="https://www.npmjs.com/">npm</a> is the <em>awesome</em> package manager bundled with the <a href="https://nodejs.org/">Node.js</a> environment. It is a very simple tool to get started with: you usually just &quot;npm install stuff&quot; and it ends up in &quot;./node_modules/stuff&quot;.</p><p>You can also <strong>globally install</strong> some packages by using the global -g flag. mocha is an excellent test runner that is usually installed like that:</p><pre>$ npm install -g mocha<br>$ mocha -V <br>2.2.5</pre><p>But this leads us to a big problem: <strong>what if two different projects depend on different versions</strong> of a test runner? How <em>do</em> you manage that? Well, you don’t!</p><h3>Decide what’s actually a dependency</h3><p>You only want to install globally <strong>tools which your projects don’t depend on</strong>. These are often handy CLI’s like mkdirp or node-inspector.</p><p>Other packages (like test-runners) <strong>should be considered dependencies</strong>, so you would install them using the flags &quot;save&quot; or &quot;save-dev&quot;. This way, they get saved to your package.json dependencies section (Read up on the <a href="http://stackoverflow.com/questions/18875674/whats-the-difference-between-dependencies-devdependencies-and-peerdependencies">differences between dependencies and devDependencies</a>).</p><p>When you install a package like mocha locally, you’ll notice something interesting:</p><pre>$ npm install --save-dev mocha<br>$ ls -lah node_modules/.bin<br>mocha -&gt; ../mocha/bin/mocha</pre><p>Apparently, npm created a folder called &quot;node_modules/.bin/&quot; which contains a mocha symlink to &quot;node_modules/mocha/bin/mocha&quot;.</p><p>So, maybe we could use that directly?</p><pre>$ ./node_modules/.bin/mocha -V<br>2.2.5</pre><p>Great! Now we know we can install tools locally and <strong>avoid version conflicts between projects!</strong></p><p>But do we need to add that long, boring &quot;./node_modules/.bin/&quot; prefix to every command we need? No way! ;)</p><h3>Use npm scripts to run your tools</h3><p>If you don’t know what npm scripts are, you should definitely take a look at the <a href="https://docs.npmjs.com/misc/scripts">npm scripts documentation</a>.</p><p>Simply put, it’s a way to document commands which are relevant to your project. You use it by defining a scripts section in the package.json file.</p><pre>&quot;scripts&quot;: { &quot;hello&quot;: &quot;echo Hello, scripts!&quot; }</pre><p>Then, you can run it by using the &quot;npm run&quot; command:</p><pre>$ npm run hello<br>Hello, scripts!</pre><p>Not all script names are born equal, however, and some don’t need the run to work. The most commonly used are start and test.</p><pre>&quot;scripts&quot;: { <br>  &quot;start&quot;: &quot;echo Hello, start!&quot;,<br>  &quot;test&quot;: &quot;echo Hello, test!&quot;<br>}</pre><p>And we can see it working:</p><pre>$ npm start<br>Hello, start!</pre><p>Defining start and test scripts is a common convention in Node.js. For example, <a href="https://devcenter.heroku.com/articles/node-best-practices">Heroku</a> and <a href="http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html">AWS BeanStalk</a> will run npm start in order to run your node project by default. <a href="http://docs.travis-ci.com/user/languages/javascript-with-nodejs/">Travis CI</a> will automatically run npm test when you upload your project.</p><h3>npm scripts understand .bin</h3><p>Presumably, then, we can now create a script to run our local mocha installation, right?</p><pre>&quot;scripts&quot;: {<br>  &quot;test&quot;: &quot;./node_modules/.bin/mocha -V&quot;<br>}</pre><p>Voilà — it works.</p><p>But npm has another trick up its sleeve. When evaluating scripts, <strong>npm adds node_modules/.bin/ to the PATH environment variable</strong>. That means you can forego the folder prefix entirely!</p><p>Our final script would look like this:</p><pre>&quot;scripts&quot;: {<br>  &quot;test&quot;: &quot;mocha -V&quot;<br>}</pre><p>Let’s test it:</p><pre>$ npm test<br>2.2.5</pre><p>Perfect!</p><p>So, remember:</p><ul><li>Prefer installing test runners and other project-specific tools <strong>as dependencies.</strong></li><li>Use &quot;npm install --save-dev stuff&quot; to save stuff to your package.json.</li><li>Use npm scripts to run these tools <strong>as if</strong> they were installed globally!</li></ul><p><em>Originally published at </em><a href="http://firstdoit.com/npm-scripts/"><em>firstdoit.com</em></a><em> on May 26, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dfb478908" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/no-need-for-globals-using-npm-dependencies-in-npm-scripts-3dfb478908">No need for globals — using npm dependencies in npm scripts.</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I learned to stop worrying and love React]]></title>
            <link>https://firstdoit.com/how-i-learned-to-stop-worrying-and-love-react-4e22b0bb6c2a?source=rss----92a9d6520edc---4</link>
            <guid isPermaLink="false">https://medium.com/p/4e22b0bb6c2a</guid>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Guilherme Rodrigues]]></dc:creator>
            <pubDate>Sun, 21 Feb 2016 13:16:02 GMT</pubDate>
            <atom:updated>2016-02-21T13:15:50.136Z</atom:updated>
            <content:encoded><![CDATA[<p>If you asked me what I thought of <a href="https://facebook.github.io/react/docs/getting-started.html">React</a> one year ago, I would probably say…</p><blockquote><em>Where are </em><strong><em>my templates?</em></strong><em> What’s that crazy </em><strong><em>HTML doing in my JavaScript?</em></strong><em> JSX looks </em><strong><em>weird!</em></strong><em> Hurry! </em><strong><em>Kill it with fire!</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*JWnc-EG8Orl44K9m.gif" /></figure><p>That was because <strong>I didn’t get it.</strong></p><p>But I swear: React is definitely on the right track… <strong>Please, hear me out.</strong></p><h3>Good old MVC</h3><p>The <em>Root of All Evil</em> in an interactive application is <strong>managing state</strong>. The “traditional” approach is the MVC architecture, or some variation thereof.</p><p>MVC proposes that your <strong>Model is the single source of truth</strong> — all state lives there. Views are <strong>derived</strong> from the Model, and must be <strong>kept in sync</strong>. When the Model changes, so does the View.</p><p>Finally, user <strong>interactions</strong> are captured by the Controller, which <strong>updates the Model</strong>. So far, so good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*FHrLNpFQuWMbINk7.png" /></figure><h3>Render the view when the model changes</h3><p>This looks quite simple. First, we need to describe our View — how it transforms the model state into DOM. Then, whenever the user <strong>acts</strong> we update the Model and <strong>re-render the entire thing</strong>… right? Not so fast. Unfortunately, this is not very straight forward for 2 reasons:</p><ol><li>The DOM actually has some state, like the content of a text input. If you re-render disregarding your DOM completely, this content will be lost.</li><li>DOM operations (like removing and inserting nodes) are <strong>really slow</strong>. Constantly rendering everything will lead to terrible performance.</li></ol><p>So how <strong>do</strong> we keep the Model and View in sync avoiding these problems?</p><h3>Data binding</h3><p>For the past 4 years, the most common framework feature introduced to solve this problem was <strong>data binding</strong>.</p><p>Data binding is the ability to <strong>keep your model and view in sync automatically</strong>. Usually, in JavaScript, that means your Objects and your DOM.</p><p>It achieves that by letting you <strong>declare the dependencies between the pieces of data</strong> in your app. Changes in the <strong>state</strong> are propagated throughout your application and all depending pieces are <em>automagically</em> updated.</p><p>Let’s see how that works in practice with some famous frameworks.</p><h4>Knockout</h4><p>Knockout argues for the <a href="http://knockoutjs.com/documentation/observables.html"><strong>MVVM</strong> (Model-View-ViewModel) approach</a> and helps you implement the “View” parts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f03901c0a7c7de98f9732bc1ada082a/href">https://medium.com/media/7f03901c0a7c7de98f9732bc1ada082a/href</a></iframe><p>And, voilà. Changing the value of either input will provoke a change in the span. You never wrote code to wire it up. How cool is that, huh?</p><p>But wait, what about the <strong>Model</strong> being the single source of truth? Where should this ViewModel get its <strong>state</strong> from? How does it know that the Model changed? Interesting questions.</p><h4>Angular 1</h4><p>Angular 1 describes data binding in terms of keeping the Model and View in sync. From the docs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*y9Ys6k6F2pGL4rUB.png" /></figure><p>But… should the View communicate with the Model directly? Are they that tightly coupled?</p><p>Anyway, let’s look at the obligatory hello world example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77d8b2fcf616c2a79879ed749cceca74/href">https://medium.com/media/77d8b2fcf616c2a79879ed749cceca74/href</a></iframe><p>From this example, it looks like the Controller <strong>has state</strong> and is behaving like a Model — or perhaps a ViewModel? Assuming the Model is elsewhere, how is it <strong>kept in sync with the Controller?</strong></p><p>My head is starting to hurt a little.</p><h3>The problems with data binding</h3><p>Data binding works wonderfully for small examples. However, as your app grows you’ll probably face some of these problems.</p><h4>Declaring dependencies can quickly introduce loops</h4><p>The most common problem is having to cope with the <strong>side effects</strong> of changes in your state. This image from the <a href="https://facebook.github.io/flux/docs/overview.html">introduction of Flux</a> explains quite clearly how dependency hell starts to creep in:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XZ1ro3e4nxVrtDeF.png" /></figure><p>In this scenario, <strong>can you predict what changes will happen</strong> when <em>one change</em> happens to a <em>single</em> Model? It is <strong>very hard</strong> to reason about code that can be executed in a completely arbitrary order when any dependency changes.</p><h4>Template and display logic are artificially separated</h4><p>What’s the role of a View? To present the data to the user. What’s the role of a ViewModel? To present the data to the user. What’s the difference? <strong>None!</strong></p><blockquote><em>Templates separate technologies, not concerns ~ Pete Hunt</em></blockquote><p>In the end, a View component should be able to <strong>manipulate</strong> its data and <strong>present it</strong> in the desired format. However, all template languages are inherently crippled: they can never achieve the same expressiveness and <strong>power</strong> as code.</p><p>Quite simply, {{# each}}, ng-repeat and databind=”foreach” are all poor replacements for something that is native and <strong>trivial</strong> in JavaScript: a <strong><em>for loop</em></strong>. And they can’t go any step further, so no <em>filter</em> or <em>map</em> for you.</p><h3>Data binding is a hack around re-rendering</h3><p>The Holy Grail of simplicity is not in discussion. What everyone always wanted was to <strong>re-render our entire app when state changes</strong>. This way, we could stop having to deal with <em>Root of All Evil</em> problem: <strong>state changing over time</strong> — we could simply describe what our app looks like <strong>given any particular state</strong>.</p><p>So, the problem is clear. Man, I wish some giant company could spare a team of genius developers to really <em>nail</em> the solution to this problem…</p><h3>Enter Facebook’s React</h3><p>Turns out they did. React implements a <strong>virtual DOM</strong> which kind of <em>delivers us the Holy Grail</em>.</p><h4>What is virtual DOM anyway?</h4><p>I’m glad you asked! Let’s look at a trivial React example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6525692ceaab62b0f692d79b21fe6d84/href">https://medium.com/media/6525692ceaab62b0f692d79b21fe6d84/href</a></iframe><p>That’s <em>all</em> of the required API for a React component. You must have a render method. Complex, huh?</p><p>OK, but what about that &lt;div&gt;? That’s not JavaScript! It sure isn’t.</p><h4>Your new friend, JSX</h4><p>This code is actually written in <a href="https://facebook.github.io/react/docs/jsx-in-depth.html">JSX</a>, a super-set of Javascript which includes that brackets syntax for defining components. The code above, when compiled into JavaScript, will actually become:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39a60953373f76fcde47d7cc028d13f6/href">https://medium.com/media/39a60953373f76fcde47d7cc028d13f6/href</a></iframe><p>Did you notice the calls to createElement? These objects compose the virtual DOM implementation.</p><p>Quite simply: React first assembles the <em>entire</em> structure of your app <strong>in-memory</strong>, using those objects. Then, it <strong>converts</strong> that structure into <strong>actual DOM nodes</strong> and inserts them in your browser’s DOM.</p><p>OK, but what’s the point of writing our HTML with those strange createElement functions?</p><h4>Virtual DOM is FAST</h4><p>As we already discussed, manipulating the DOM is <em>ridiculously</em> expensive, so it must be done <strong>as few times as possible</strong>.</p><p>React’s virtual DOM, however, makes it really fast to <strong>compare two trees</strong> and find exactly <strong>what changed between them</strong>. That way, React is able to compute the <strong>minimum set of changes</strong> necessary to update the DOM.</p><p>Practically speaking, React can <strong>diff</strong> two DOM trees and discover the minimum set of operations it needs to perform. This means two things:</p><ol><li>If an input with text is re-rendered and React expects it to have that content, it <strong>won’t touch</strong> the input. No more state loss!</li><li>Diffing the virtual DOM is not expensive at all, so we can diff it as much as we like. When it’s ready to actually alter the DOM, it will only perform the <strong>least possible number of operations</strong>. No more slow layout thrashing!</li></ol><p>Remember the two problems with re-rendering our entire app when state changes?</p><p><strong>Gone.</strong></p><h4>React maps state to DOM</h4><p>Virtual DOM rendering and diffing is the <strong>only</strong> magical part about React. Its excellent performance, however, is what fundamentally <strong>enables us</strong> to have a <em>much simpler</em> architecture overall. How simple?</p><blockquote><em>React components are idempotent functions. They describe your UI at any point in time, just like a server-rendered app.</em></blockquote><blockquote><em>— Pete Hunt, </em><a href="https://www.youtube.com/watch?v=x7cQ3mrcKaY"><em>React: Rethinking best practices</em></a></blockquote><p>Idempotent-function-simple.</p><p>That’s all a React component should be, really. It maps the <strong>current app state</strong> to DOM. And you have the <strong>full power of JavaScript</strong> to describe your UI — loops, functions, scope, composition, modules — <em>not</em> a crippled template language.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/549cd02b29a71c4bab60c4d0de242b44/href">https://medium.com/media/549cd02b29a71c4bab60c4d0de242b44/href</a></iframe><h3>Start using React today</h3><p>React can be a little <em>daunting</em> at first. It proposes a really large paradigm shift, which is always uncomfortable. However, the advantages become clear when you start using it.</p><p>The <a href="https://facebook.github.io/react/docs/getting-started.html">React documentation</a> is excellent. You should give it a try and follow the tutorial. I’m sure you’ll love it if you give it a chance.</p><p>Happy coding!</p><p><em>Originally published at </em><a href="http://firstdoit.com/react-1/"><em>firstdoit.com</em></a><em> on May 15, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e22b0bb6c2a" width="1" height="1" alt=""><hr><p><a href="https://firstdoit.com/how-i-learned-to-stop-worrying-and-love-react-4e22b0bb6c2a">How I learned to stop worrying and love React</a> was originally published in <a href="https://firstdoit.com">First, do it</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>