<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Posts on t3h2mas.xyz</title><link>https://www.t3h2mas.xyz/post/</link><description>Recent content in Posts on t3h2mas.xyz</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 09 Jun 2020 00:00:00 +0000</lastBuildDate><atom:link href="https://www.t3h2mas.xyz/post/index.xml" rel="self" type="application/rss+xml"/><item><title>Fluent Builder with JavaScript</title><link>https://www.t3h2mas.xyz/post/fluent-builder-with-javascript/</link><pubDate>Tue, 09 Jun 2020 00:00:00 +0000</pubDate><guid>https://www.t3h2mas.xyz/post/fluent-builder-with-javascript/</guid><category>JavaScript, Builder Pattern, Fluent Builder</category><description>&lt;figure>
&lt;img src="https://www.t3h2mas.xyz/images/fluent-builder/construction.jpg"
alt="construction"/> &lt;figcaption>
&lt;p>Photo by Scott Blake on Unsplash&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;h2 id="experimenting-with-implementing-the-fluent-builder-pattern-in-javascript">Experimenting with implementing the fluent builder pattern in JavaScript.&lt;/h2>
&lt;p>The fluent builder pattern is a composition of the builder pattern and the fluent interface pattern.&lt;/p>
&lt;p>It&amp;rsquo;s a pattern that holds our hand through the maze of object construction.&lt;/p>
&lt;p>Our implementation uses es6 classes to give us something resembling a fluent builder.&lt;/p>
&lt;p>Traditionally fluent interfaces are built using&amp;hellip; interfaces.&lt;/p>
&lt;p>Vanilla JavaScript doesn&amp;rsquo;t have interfaces. We&amp;rsquo;re left to do what we can with what we have.&lt;/p>
&lt;p>(This is where someone says something about &lt;a href="https://www.typescriptlang.org/">TypeScript&lt;/a>. Have at it, but I never said I was writing about TypeScript. However, I would be delighted to see someone implement their own Fluent Builder in TypeScript or your language of choice)&lt;/p>
&lt;p>For the curious, here is my &lt;a href="https://gist.github.com/t3h2mas/37c80de8b5c644f94702cf2bd61c2aec">attempt&lt;/a> at implementing the pattern using JSDOC interfaces. I changed approaches after I realize that editor behavior was different between implementations.&lt;/p>
&lt;h2 id="how-to-build-a-burrito">How to build a burrito&lt;/h2>
&lt;p>To get to where we&amp;rsquo;re going first we will have to take a look at the &lt;a href="https://en.wikipedia.org/wiki/Builder_pattern">builder pattern.&lt;/a>&lt;/p>
&lt;p>Wikipedia summarizes the pattern as&lt;/p>
&lt;blockquote>
&lt;p>The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.&lt;/p>
&lt;/blockquote>
&lt;p>That&amp;rsquo;s right. We are about to attempt to apply an &lt;em>object-oriented&lt;/em> design pattern from a book[1] written in &lt;strong>1984&lt;/strong> to JavaScript in 2020. What a time to be alive!&lt;/p>
&lt;p>Anyway&amp;hellip;&lt;/p>
&lt;p>Maybe we want to make a burrito&amp;hellip; Relax, this isn&amp;rsquo;t a &lt;a href="https://www.chrisdone.com/posts/monads-are-burritos/">monad&lt;/a> tutorial&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Everyone loves Burritos
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
&lt;span style="color:#0087ff">class&lt;/span> Burrito {
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} protein
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} carb
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {?string} salsa
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {?string} cheese
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
constructor(protein, carb, salsa, cheese) {
&lt;span style="color:#4e4e4e">// required toppings
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>.protein = protein;
&lt;span style="color:#5f8700">this&lt;/span>.carb = carb;
&lt;span style="color:#4e4e4e">// optional toppings
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>.salsa = salsa;
&lt;span style="color:#5f8700">this&lt;/span>.cheese = cheese;
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Our take on a burrito has the following properties required in the constructor&lt;/p>
&lt;ul>
&lt;li>a carb(ohydrate) such as brown or white rice&lt;/li>
&lt;li>a protein such as shredded pork or beef&lt;/li>
&lt;/ul>
&lt;p>The following are optional (for whatever reason)&lt;/p>
&lt;ul>
&lt;li>a salsa of some variety&lt;/li>
&lt;li>cheese, queso, that ripe, runny, young or old fromage&lt;/li>
&lt;/ul>
&lt;hr>
&lt;p>Making (or constructing) a burrito as shown could look like this&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">const&lt;/span> burrito = &lt;span style="color:#5f8700">new&lt;/span> Burrito(
&lt;span style="color:#00afaf">&amp;#34;brown rice&amp;#34;&lt;/span>,
&lt;span style="color:#00afaf">&amp;#34;shredded pork&amp;#34;&lt;/span>,
&lt;span style="color:#00afaf">&amp;#34;green salsa&amp;#34;&lt;/span>,
&lt;span style="color:#00afaf">&amp;#34;cojita&amp;#34;&lt;/span>
);
&lt;span style="color:#4e4e4e">// do stuff to the burrito
&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If this burrito gets popular somehow we&amp;rsquo;re going to have to continue to make more and more burritos. Passing parameter after parameter in the same order to our &lt;code>Burrito.constructor&lt;/code> [2]&lt;/p>
&lt;p>We pass the parameters at the same time to construct the class instance.&lt;/p>
&lt;p>To be annoyingly repetitive, using individual parameters got the job done, but have implications such as&lt;/p>
&lt;ul>
&lt;li>all parameters must be passed at the same time&lt;/li>
&lt;li>each parameter must be passed in the correct order&lt;/li>
&lt;li>the constructor definition grows with each new parameter passed [3]&lt;/li>
&lt;/ul>
&lt;p>Now we will attempt to bypass these implications using a builder&amp;hellip; (The burrito in the following snippet is the same one we looked at before.)&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Everyone loves Burritos
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
&lt;span style="color:#0087ff">class&lt;/span> Burrito {
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} protein
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} carb
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {?string} salsa
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {?string} cheese
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
constructor(protein, carb, salsa, cheese) {
&lt;span style="color:#4e4e4e">// required toppings
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>.protein = protein;
&lt;span style="color:#5f8700">this&lt;/span>.carb = carb;
&lt;span style="color:#4e4e4e">// optional toppings
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>.salsa = salsa;
&lt;span style="color:#5f8700">this&lt;/span>.cheese = cheese;
}
}
&lt;span style="color:#4e4e4e">/*
&lt;/span>&lt;span style="color:#4e4e4e"> * BurritoBuilder adds flexibility to burrito construction
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
&lt;span style="color:#0087ff">class&lt;/span> BurritoBuilder {
constructor() {
&lt;span style="color:#5f8700">this&lt;/span>.toppings = {}; &lt;span style="color:#4e4e4e">// 1
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> }
&lt;span style="color:#4e4e4e">// 2
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Add a protein to burrito
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} protein
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {BurritoBuilder}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withProtein(protein) {
&lt;span style="color:#5f8700">this&lt;/span>.toppings.protein = protein;
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>; &lt;span style="color:#4e4e4e">// 3
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> }
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Add a carbohydrate to burrito
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} carb
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {BurritoBuilder}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withCarb(carb) {
&lt;span style="color:#5f8700">this&lt;/span>.toppings.carb = carb;
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Add salsa to our burrito
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {salsa} salsa
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {BurritoBuilder}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withSalsa(salsa) {
&lt;span style="color:#5f8700">this&lt;/span>.toppings.salsa = salsa;
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Add cheese to our burrito
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} cheese
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {BurritoBuilder}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withCheese(cheese) {
&lt;span style="color:#5f8700">this&lt;/span>.toppings.cheese = cheese;
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>;
}
&lt;span style="color:#4e4e4e">// 4
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * Wrap our toppings into a finished burrito
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {Burrito}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
build() {
&lt;span style="color:#0087ff">const&lt;/span> { protein, carb, cheese, salsa } = &lt;span style="color:#5f8700">this&lt;/span>.toppings;
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">new&lt;/span> Burrito(protein, carb, cheese, salsa);
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>There is a lot to unpack from our builder implementation! Let&amp;rsquo;s break down a few key points&lt;/p>
&lt;ol>
&lt;li>We store toppings in an object as a class property&lt;/li>
&lt;li>Topping adding methods follow the pattern of &lt;code>.with[ToppingName]&lt;/code>&lt;/li>
&lt;li>We return a reference to the instance of the Burrito Builder after adding each ingredient&lt;/li>
&lt;li>Finally, we have a build method that will attempt to build a burrito using the toppings we selected. This method &lt;a href="https://pics.me.me/that-rug-really-tied-the-room-together-the-big-lebowski-24483025.png">ties the room together&lt;/a> by providing a tortilla wrapped resolution&lt;/li>
&lt;/ol>
&lt;p>Enough with the lists, time to put our &lt;code>BurritoBuilder&lt;/code> to use!&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">const&lt;/span> burrito = &lt;span style="color:#5f8700">new&lt;/span> BurritoBuilder()
.withCarb(&lt;span style="color:#00afaf">&amp;#34;brown rice&amp;#34;&lt;/span>)
.withSalsa(&lt;span style="color:#00afaf">&amp;#34;green&amp;#34;&lt;/span>)
.withCheese(&lt;span style="color:#00afaf">&amp;#34;cojita&amp;#34;&lt;/span>)
.withProtein(&lt;span style="color:#00afaf">&amp;#34;shredded pork&amp;#34;&lt;/span>)
.build();
&lt;/code>&lt;/pre>&lt;/div>&lt;p>In this example we&amp;rsquo;re passing all the ingredients at once. We&amp;rsquo;re able to build a burrito in one statement by method chaining. Method chaining is one flavor found in builders and is available because we return a reference to the builder in every method besides the finalizing &lt;code>build&lt;/code>. (The &lt;code>return this&lt;/code> in each chain-able method allows us to chain, but we are still free to assign our burrito-to-be to a variable whenever we&amp;rsquo;d like.)&lt;/p>
&lt;p>We could easily do something in the spirit of 2020 era popular &amp;ldquo;healthy fast food&amp;rdquo; burrito joints&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">class&lt;/span> CarbStation {
&lt;span style="color:#0087ff">static&lt;/span> addIngredient(burrito, ingredient) {
&lt;span style="color:#5f8700">return&lt;/span> burrito.withCarb(ingredient);
}
}
&lt;span style="color:#0087ff">class&lt;/span> GrillStation {
&lt;span style="color:#0087ff">static&lt;/span> addIngredient(burrito, ingredient) {
&lt;span style="color:#5f8700">return&lt;/span> burrito.withProtein(ingredient);
}
}
&lt;span style="color:#0087ff">class&lt;/span> ExtraStation {
&lt;span style="color:#0087ff">static&lt;/span> addIngredient(burrito, category, ingredient) {
&lt;span style="color:#5f8700">if&lt;/span> (category === &lt;span style="color:#00afaf">&amp;#34;salsa&amp;#34;&lt;/span>) {
&lt;span style="color:#5f8700">return&lt;/span> burrito.withSalsa(ingredient);
}
&lt;span style="color:#5f8700">if&lt;/span> (category === &lt;span style="color:#00afaf">&amp;#34;cheese&amp;#34;&lt;/span>) {
&lt;span style="color:#5f8700">return&lt;/span> burrito.withCheese(ingredient);
}
&lt;span style="color:#5f8700">throw&lt;/span> &lt;span style="color:#5f8700">new&lt;/span> &lt;span style="color:#0087ff">Error&lt;/span>(&lt;span style="color:#00afaf">&amp;#34;We don&amp;#39;t sell that here!&amp;#34;&lt;/span>);
}
}
&lt;span style="color:#0087ff">class&lt;/span> Cashier {
&lt;span style="color:#4e4e4e">// oops, no register logic, free burritos
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#0087ff">static&lt;/span> pay(burrito) {
&lt;span style="color:#5f8700">return&lt;/span> burrito.build();
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Let&amp;rsquo;s recreate our burrito from before. Notice how we&amp;rsquo;re passing a burrito builder around from class to class
so they can each add toppings with love and care. Construction of the burrito is delayed until we see fit.&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#4e4e4e">// Warning, the following may offend you if you only speak const or point-free
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span>&lt;span style="color:#0087ff">const&lt;/span> burritoBuilder = &lt;span style="color:#5f8700">new&lt;/span> BurritoBuilder(); &lt;span style="color:#4e4e4e">// (reference #1)
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span>
&lt;span style="color:#0087ff">let&lt;/span> burritoWithCarb = CarbStation.addIngredient(burritoBuilder, &lt;span style="color:#00afaf">&amp;#34;brown rice&amp;#34;&lt;/span>); &lt;span style="color:#4e4e4e">// (reference #2)
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span>&lt;span style="color:#0087ff">let&lt;/span> burritoWithCarbAndProtein = GrillStation.addIngredient(
burritoWithCarb,
&lt;span style="color:#00afaf">&amp;#34;shredded pork&amp;#34;&lt;/span>
); &lt;span style="color:#4e4e4e">// (reference #3)
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span>
ExtraStation.addIngredient(burritoWithCarbAndProtein, &lt;span style="color:#00afaf">&amp;#34;guac&amp;#34;&lt;/span>, &lt;span style="color:#d75f00">true&lt;/span>);
ExtraStation.addIngredient(burritoWithCarbAndProtein, &lt;span style="color:#00afaf">&amp;#34;salsa&amp;#34;&lt;/span>, &lt;span style="color:#00afaf">&amp;#34;green salsa&amp;#34;&lt;/span>);
ExtraStation.addIngredient(burritoWithCarbAndProtein, &lt;span style="color:#00afaf">&amp;#34;cheese&amp;#34;&lt;/span>, &lt;span style="color:#00afaf">&amp;#34;cojita&amp;#34;&lt;/span>);
&lt;span style="color:#0087ff">const&lt;/span> readyToEatBurrito = Cashier.pay(burritoWithCarbAndProtein);
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notice a few things here.&lt;/p>
&lt;ol>
&lt;li>We can reference our burrito mid-construction with chaining or by variable assignment&lt;/li>
&lt;li>We have 3 different variables (marked with comments) referencing the same thing&lt;/li>
&lt;li>&lt;code>BurritoBuilder#build&lt;/code> must be called when we&amp;rsquo;re ready to finalize our burrito build out&lt;/li>
&lt;li>We passed around an incomplete burrito builder. We called methods that independently added their own modifications.&lt;/li>
&lt;/ol>
&lt;p>So far we have briefly explored the second component of the term &amp;ldquo;fluent builder.&amp;rdquo; In true &lt;a href="https://en.wikipedia.org/wiki/LIFO">LIFO&lt;/a> fashion, we will now look at the &amp;ldquo;fluent&amp;rdquo; component.&lt;/p>
&lt;h3 id="fluent-interfaces">Fluent interfaces&lt;/h3>
&lt;p>&lt;a href="https://martinfowler.com/">Martin Fowler&lt;/a> suggests that the term &amp;ldquo;&lt;a href="https://martinfowler.com/bliki/FluentInterface.html">fluent interface&lt;/a>&amp;rdquo; is synonymous with an &lt;em>internal&lt;/em> &lt;a href="https://martinfowler.com/bliki/DomainSpecificLanguage.html">domain specific language&lt;/a>.&lt;/p>
&lt;p>In a summary of Fowler&amp;rsquo;s post, Piers Cawley poetically describes the fluent interface as a way to &lt;a href="https://bofh.org.uk/2005/12/21/fluent-interfaces/">&amp;ldquo;move [sic moving] object construction behind a thoughtful, humane interface.&amp;quot;&lt;/a>&lt;/p>
&lt;p>Our implementation will be using classes to work around JavaScripts lack of interfaces.&lt;/p>
&lt;p>Without further ado, let&amp;rsquo;s introduce a plot twist so we can try to construct burritos behind a thoughtful, humane &amp;ldquo;interface.&amp;rdquo;&lt;/p>
&lt;h3 id="a-wild-boss-appears">A wild boss appears&lt;/h3>
&lt;p>You&amp;rsquo;re sitting at your keyboard when suddenly a wild boss appears
Boss &amp;gt; Your burrito code has been working for us so far but there&amp;rsquo;s a problem! When I presented the code to the client (Healthy Burrito Chain) they told us about some business rules we failed to discover in the original project specification!
You &amp;gt; Oh no! Not surprise business rules!
Boss &amp;gt; Instead of filing TPS reports on Saturday, you need to come in and make sure we enforce the following rules when creating burritos&amp;hellip;&lt;/p>
&lt;p>(The rules the boss give you are as follows)&lt;/p>
&lt;ol>
&lt;li>In order for a burrito to be built, it must have a carb and a protein. We cannot allow a burrito to be created without these ingredients.&lt;/li>
&lt;li>After the required ingredients are submitted, we must allow customers to either pay or add one or more extra ingredient.&lt;/li>
&lt;li>The extra ingredients are salsa, and cheese&lt;/li>
&lt;/ol>
&lt;p>&lt;em>Oh No&lt;/em> you think. It&amp;rsquo;s going to be a long weekend&amp;hellip;.&lt;/p>
&lt;h3 id="saturday-rolls-around">Saturday rolls around&lt;/h3>
&lt;p>Instead of throwing out the decision to use the builder pattern for our burritos, maybe we can make some adjustments by making our builder &lt;em>fluent&lt;/em>.&lt;/p>
&lt;p>Another way to look at our new business model by translating our burrito shop into a &lt;a href="https://en.wikipedia.org/wiki/Finite-state_machine">finite state machine&lt;/a>&lt;/p>
&lt;figure>
&lt;img src="https://www.t3h2mas.xyz/images/fluent-builder/burrito-fsm.png"
alt="fluent builder finite state machine"/> &lt;figcaption>
&lt;h4>fluent builder finite state machine&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;h3 id="shut-up-and-show-me-the-code">Shut up and show me the code&lt;/h3>
&lt;p>Let us take our implementation, wrap it with some classes. Hopefully whatever comes out won&amp;rsquo;t make Mr. Fowler cringe.&lt;/p>
&lt;hr>
&lt;p>We&amp;rsquo;ll start with a class that allows us to set the protein.&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">class&lt;/span> ProteinSetter {
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {BurritoBuilder} builder
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
constructor(builder) {
&lt;span style="color:#4e4e4e">// 1
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>.builder = builder;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} protein
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {CarbSetter}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withProtein(protein) {
&lt;span style="color:#4e4e4e">// 2
&lt;/span>&lt;span style="color:#4e4e4e">&lt;/span> &lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">new&lt;/span> CarbSetter(&lt;span style="color:#5f8700">this&lt;/span>.builder.withProtein(protein));
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notes:&lt;/p>
&lt;ol>
&lt;li>Our &lt;code>ProteinSetter&lt;/code> class takes our builder from before. We&amp;rsquo;re wrapping the existing builder class instead of replacing the implementation.&lt;/li>
&lt;li>We pass the builder to the &lt;code>CarbSetter&lt;/code> class after choosing a protein.&lt;/li>
&lt;/ol>
&lt;p>The &lt;code>CarbSetter&lt;/code> class looks like this&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">class&lt;/span> CarbSetter {
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {BurritoBuilder} builder
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
constructor(builder) {
&lt;span style="color:#5f8700">this&lt;/span>.builder = builder;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} carb
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {ExtraSetter}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withCarb(carb) {
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">new&lt;/span> ExtraSetter(&lt;span style="color:#5f8700">this&lt;/span>.builder.withCarb(carb));
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>This class is pretty similar to the &lt;code>ProteinSetter&lt;/code> we just saw. After the carb is set, we pass our builder along to the &lt;code>ExtraSetter&lt;/code>.&lt;/p>
&lt;p>Are you starting to see the pattern here? We return class instances to control the flow of burrito construction.&lt;/p>
&lt;p>The &lt;code>ExtraSetter&lt;/code> class looks like this&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">class&lt;/span> ExtraSetter {
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {BurritoBuilder} builder
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
constructor(builder) {
&lt;span style="color:#5f8700">this&lt;/span>.builder = builder;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {number} salsa
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {ExtraSetter}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withSalsa(salsa) {
&lt;span style="color:#5f8700">this&lt;/span>.builder.withSalsa(salsa);
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @param {string} cheese
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {ExtraSetter}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
withCheese(cheese) {
&lt;span style="color:#5f8700">this&lt;/span>.builder.withCheese(cheese);
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>;
}
&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * @returns {Burrito}
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
wrapUp() {
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">this&lt;/span>.builder.build();
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Just like the other classes that we&amp;rsquo;ve seen, except for one crucial detail. The &lt;code>ExtraSetter&lt;/code> can complete a build.&lt;/p>
&lt;p>Our extra setter can:&lt;/p>
&lt;ol>
&lt;li>Add optional toppings in any order&lt;/li>
&lt;li>Complete the construction of our tortilla wrapped master piece&lt;/li>
&lt;/ol>
&lt;p>This last class is our entry point to the fluent &lt;em>burrito&lt;/em> builder work flow.&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#4e4e4e">/**
&lt;/span>&lt;span style="color:#4e4e4e"> * FluentBuilder to use as a starting point
&lt;/span>&lt;span style="color:#4e4e4e"> */&lt;/span>
&lt;span style="color:#0087ff">class&lt;/span> FluentBuilder {
&lt;span style="color:#0087ff">static&lt;/span> onTortilla() {
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#5f8700">new&lt;/span> ProteinSetter(&lt;span style="color:#5f8700">new&lt;/span> BurritoBuilder());
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="drum-roll-please">Drum roll, please&lt;/h2>
&lt;p>Now for the moment we&amp;rsquo;ve all been waiting for&amp;hellip;&lt;/p>
&lt;p>We can use our Fluent Builder as follows&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">const&lt;/span> burrito = FluentBuilder.onTortilla()
.withProtein(&lt;span style="color:#00afaf">&amp;#34;a&amp;#34;&lt;/span>)
.withCarb(&lt;span style="color:#00afaf">&amp;#34;brown rice&amp;#34;&lt;/span>)
.withCheese(&lt;span style="color:#00afaf">&amp;#34;cojita&amp;#34;&lt;/span>)
.wrapUp();
&lt;/code>&lt;/pre>&lt;/div>&lt;p>This is valid usage. Most editors will &lt;em>guide&lt;/em> us down this path. Unlike the &lt;code>BurritoBuilder&lt;/code> we can only call the methods that were intentionally exposed at any particular stage.&lt;/p>
&lt;figure>
&lt;img src="https://www.t3h2mas.xyz/images/fluent-builder/build-small.gif"
alt="Fluent Builder in action"/> &lt;figcaption>
&lt;h4>Fluent Builder in action&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;p>We&amp;rsquo;re forced down the happy path.&lt;/p>
&lt;p>Go ahead, try it. Try to create a burrito using the &lt;code>FluentBuilder&lt;/code> methods without adding a protein. That&amp;rsquo;s right, you can&amp;rsquo;t without directly accessing the builder (which is totally cheating)&lt;/p>
&lt;h2 id="i-love-it-how-can-i-_use_-it">I love it, how can I &lt;em>use&lt;/em> it?&lt;/h2>
&lt;p>Personally I&amp;rsquo;ve been using Fluent Builders to constrain the construction of &lt;a href="https://martinfowler.com/eaaCatalog/dataTransferObject.html">DTO&lt;/a>s in tests and the application layer.&lt;/p>
&lt;h2 id="feedback">Feedback&lt;/h2>
&lt;p>Yes please &lt;a href="https://twitter.com/teh2mas">@teh2mas&lt;/a>&lt;/p>
&lt;hr>
&lt;p>[1] &lt;a href="https://en.wikipedia.org/wiki/Design_Patterns">https://en.wikipedia.org/wiki/Design_Patterns&lt;/a>&lt;/p>
&lt;p>[2] A common pattern JavaScript is to pass multiple parameters into a class constructor, method, or function as an object like&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#0087ff">class&lt;/span> Burrito({ carb, protein, salsa, cheese }) { &lt;span style="color:#4e4e4e">/* ... */&lt;/span> }
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Which is a fine way of taking advantage of destructuring. We are also free to pass the parameters in whichever order we&amp;rsquo;d like.&lt;/p>
&lt;p>[3] This can be a &lt;a href="https://martinfowler.com/bliki/CodeSmell.html">code smell&lt;/a> hinting at a chance to decompose our class into smaller components&lt;/p></description></item><item><title>Hello World! The Pythonic way</title><link>https://www.t3h2mas.xyz/post/hi-python/</link><pubDate>Wed, 06 Apr 2016 04:05:25 -0600</pubDate><guid>https://www.t3h2mas.xyz/post/hi-python/</guid><description>&lt;figure>
&lt;img src="https://www.t3h2mas.xyz/images/hi-python/irc-bot.png"
alt="IRC bot in python"/> &lt;figcaption>
&lt;h4>IRC bot in python&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;h2 id="hello-world">Hello world&lt;/h2>
&lt;p>The first program developers are often introduced to is the infamous Hello World. It doesn’t matter what language you’re using, you have probably seen one. If not in a tutorial, than out in the wild.&lt;/p>
&lt;h3 id="the-why">The why&lt;/h3>
&lt;p>This post is to celebrate &lt;a href="https://www.freecodecamp.com">Free Code Camp&lt;/a> expanding towards supporting Python, among other cool languages. Check out the &lt;a href="https://medium.freecodecamp.com/java-ruby-and-go-oh-my-6b5577ba2bc2">announcement&lt;/a> for yourself.&lt;/p>
&lt;p>Note that this post isn’t meant to be a tutorial for brand new programmers. I have included links to help readers get started with Python.&lt;/p>
&lt;h2 id="show-me-some-code">Show me some code&lt;/h2>
&lt;p>Enough talk. Let’s check out the way you would write Hello World in Python. Deep breath now. And off we go.&lt;/p>
&lt;h3 id="python3">Python3&lt;/h3>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-python" data-lang="python">&lt;span style="color:#5f8700">print&lt;/span>(&lt;span style="color:#00afaf">&amp;#39;Hello World!&amp;#39;&lt;/span>)
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Fascinating right? Those of you who are used to JavaScript might not be very impressed. The JS Hello World example wouldn’t be much different.&lt;/p>
&lt;h3 id="javascript">JavaScript&lt;/h3>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">console.log(&lt;span style="color:#00afaf">&amp;#34;Hello World!&amp;#34;&lt;/span>);
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="ruby">Ruby&lt;/h3>
&lt;p>Ruby’s is in the same ballpark&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-rb" data-lang="rb">&lt;span style="color:#0087ff">puts&lt;/span> &lt;span style="color:#00afaf">&amp;#34;Hello World!&amp;#34;&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>To put the simplicity of these into context let’s look at another two examples.&lt;/p>
&lt;h3 id="c">C&lt;/h3>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-c" data-lang="c">&lt;span style="color:#5f8700">#include&lt;/span> &lt;span style="color:#5f8700">&amp;lt;stdio.h&amp;gt;&lt;/span>&lt;span style="color:#5f8700">
&lt;/span>&lt;span style="color:#5f8700">&lt;/span>
&lt;span style="color:#af0000">int&lt;/span> &lt;span style="color:#0087ff">main&lt;/span>(&lt;span style="color:#af0000">int&lt;/span> argc, &lt;span style="color:#af0000">char&lt;/span>* argv[])
{
printf(&lt;span style="color:#00afaf">&amp;#34;Hello World!&lt;/span>&lt;span style="color:#af0000">\n&lt;/span>&lt;span style="color:#00afaf">&amp;#34;&lt;/span>);
&lt;span style="color:#5f8700">return&lt;/span> &lt;span style="color:#00afaf">0&lt;/span>;
}
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="java">Java&lt;/h3>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-java" data-lang="java">&lt;span style="color:#0087ff">public&lt;/span> &lt;span style="color:#0087ff">class&lt;/span> &lt;span style="color:#0087ff">HelloWorld&lt;/span> {
&lt;span style="color:#0087ff">public&lt;/span> &lt;span style="color:#0087ff">static&lt;/span> &lt;span style="color:#af0000">void&lt;/span> &lt;span style="color:#0087ff">main&lt;/span>(String[] args) {
System.out.prinln(&lt;span style="color:#00afaf">&amp;#34;Hello World!&amp;#34;&lt;/span>);
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>There has been a shift in the last few years where the programming community has started to lean towards the prior three languages as introductory languages over the latter two. Perhaps these Hello World’s give you a small taste of why. What do you think?&lt;/p>
&lt;p>Okay, back to Python.&lt;/p>
&lt;h2 id="what-about-this-pythonic-thing">What about this Pythonic thing?&lt;/h2>
&lt;p>I will use this last section to skim the surface of what the word Pythonic is and we will look at a Pythonic Hello World.&lt;/p>
&lt;h3 id="what-the-hell-is-pythonic">What the hell is Pythonic?&lt;/h3>
&lt;p>When people think about this question, they may think of Python’s famous&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-py" data-lang="py">&lt;span style="color:#d75f00">import&lt;/span> this
&lt;/code>&lt;/pre>&lt;/div>&lt;p>example. Which when ran will give you this:&lt;/p>
&lt;pre>&lt;code>Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
&lt;/code>&lt;/pre>
&lt;p>Take from that what you will. Let’s focus on one line from the text.&lt;/p>
&lt;blockquote>
&lt;p>There should be one&amp;ndash; and preferably only one &amp;ndash;obvious way to do it.&lt;/p>
&lt;/blockquote>
&lt;p>To me this line describes the mentality behind the word Pythonic and idiomatic Python.&lt;/p>
&lt;p>If you’re falling asleep at the keyboard, at least &lt;a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html">add this to your reading list&lt;/a>.&lt;/p>
&lt;h3 id="shouldnt-there-always-be-one-way-to-do-it">Shouldn’t there always be ‘one way to do it?’&lt;/h3>
&lt;p>That’s up to you. Despite what language you use. Let’s look at an example from the Perl community (which Ruby has inherited.)&lt;/p>
&lt;blockquote>
&lt;p>&lt;strong>There’s more than one way to do it&lt;/strong> (&lt;strong>TMTOWTDI&lt;/strong> or &lt;strong>TIMTOWTDI&lt;/strong>, pronounced &lt;em>Tim Toady&lt;/em>)
&lt;a href="https://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it">&lt;strong>There&amp;rsquo;s more than one way to do it - Wikipedia, the free encyclopedia&lt;/strong> &amp;gt; *There&amp;rsquo;s more than one way to do it ( TMTOWTDI or TIMTOWTDI, pronounced Tim Toady) is a Perl programming motto. The…*en.wikipedia.org&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>(TIL there’s a pronunciation!)&lt;/p>
&lt;h3 id="back-to-the-code">Back to the code&lt;/h3>
&lt;p>Let’s skip the rest of the philosophy lesson and dive into the Pythonic Hello World code example. I’m going to include a very basic function (&lt;em>oh my!&lt;/em>) so it’s not so confusing when we look at the lines.&lt;/p>
&lt;h1 id="is-how-you-start-a-python-comment">is how you start a Python comment&lt;/h1>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-py" data-lang="py">&lt;span style="color:#4e4e4e"># section one&lt;/span>
&lt;span style="color:#5f8700">def&lt;/span> &lt;span style="color:#0087ff">main&lt;/span>():
&lt;span style="color:#5f8700">print&lt;/span>(&lt;span style="color:#00afaf">&amp;#34;Hello World!&amp;#34;&lt;/span>)
&lt;span style="color:#4e4e4e"># section two&lt;/span>
&lt;span style="color:#5f8700">if&lt;/span> __name__ == &lt;span style="color:#00afaf">&amp;#34;__main__&amp;#34;&lt;/span>:
main()
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Okay?&lt;/p>
&lt;h3 id="tear-it-down">Tear it down&lt;/h3>
&lt;p>&lt;strong>Section one&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-py" data-lang="py">&lt;span style="color:#5f8700">def&lt;/span> &lt;span style="color:#0087ff">main&lt;/span>():
&lt;span style="color:#5f8700">print&lt;/span>(&lt;span style="color:#00afaf">&amp;#34;Hello World!&amp;#34;&lt;/span>)
&lt;/code>&lt;/pre>&lt;/div>&lt;p>define a function that takes no arguments and doesn’t return any value named main&lt;/p>
&lt;p>print Hello World! to the console when main is called&lt;/p>
&lt;p>&lt;strong>Section two&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-py" data-lang="py">&lt;span style="color:#5f8700">if&lt;/span> __name__ == &lt;span style="color:#00afaf">&amp;#34;__main__&amp;#34;&lt;/span>:
main()
&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>name&lt;/strong> is assigned to the calling module…&lt;/p>
&lt;p>In short:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>if the module is imported &lt;strong>name&lt;/strong> will be the set to the importing module&lt;/p>
&lt;/li>
&lt;li>
&lt;p>if the file is directly ran then execute the if statement&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Let’s look at one more modified example before we wrap this up&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-py" data-lang="py">&lt;span style="color:#4e4e4e"># fcc-greet.py&lt;/span>
&lt;span style="color:#5f8700">def&lt;/span> &lt;span style="color:#0087ff">greet&lt;/span>(name):
&lt;span style="color:#5f8700">print&lt;/span>(&lt;span style="color:#00afaf">&amp;#34;Hello {}, welcome to Free Code Camp!&amp;#34;&lt;/span>.format(name))
&lt;span style="color:#5f8700">if&lt;/span> __name__ == &lt;span style="color:#00afaf">&amp;#34;__main__&amp;#34;&lt;/span>:
&lt;span style="color:#d75f00">from&lt;/span> sys &lt;span style="color:#d75f00">import&lt;/span> argv
greet(argv[&lt;span style="color:#00afaf">1&lt;/span>]) &lt;span style="color:#4e4e4e"># first command argument&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>The print statement and the last line may be a little much for some newer users. Instead of explaining them I’m going to show you two different ways to use our new Python program.&lt;/p>
&lt;p>The first is through the terminal/command prompt:&lt;/p>
&lt;pre>&lt;code>$ python fcc-greet.py t3h2mas
&lt;/code>&lt;/pre>
&lt;p>which prints this to the console&lt;/p>
&lt;blockquote>
&lt;p>Hello t3h2mas, welcome to Free Code Camp!&lt;/p>
&lt;/blockquote>
&lt;p>Using &lt;code>fcc-greet.py&lt;/code> as a module:&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-py" data-lang="py">&lt;span style="color:#4e4e4e"># my-program.py&lt;/span>
&lt;span style="color:#d75f00">import&lt;/span> fcc-greet
users = [&lt;span style="color:#00afaf">&amp;#34;t3h2mas&amp;#34;&lt;/span>, &lt;span style="color:#00afaf">&amp;#34;BoilingOil&amp;#34;&lt;/span>, &lt;span style="color:#00afaf">&amp;#34;mamptecnocrata&amp;#34;&lt;/span>]
&lt;span style="color:#0087ff">map&lt;/span>(fcc-greet.greet, users)
&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;em>thank you to the above users for their permission to use their username :+1:&lt;/em>&lt;/p>
&lt;p>which would output&lt;/p>
&lt;blockquote>
&lt;p>Hello t3h2mas, welcome to Free Code Camp!
Hello BoilingOil, welcome to Free Code Camp!
Hello mamptecnocrata, welcome to Free Code Camp!&lt;/p>
&lt;/blockquote>
&lt;p>That last example might have a little much going on. Just focus on the output!&lt;/p>
&lt;p>That completes our example program using Pythonic idioms. We finished with a program that can be called from the prompt with a supplied argument as well being used as a module easily from different programs.&lt;/p>
&lt;h2 id="wrap-up">wrap up&lt;/h2>
&lt;p>This concludes our small taste of idiomatic Python. This post was intended to be supplementary reading rather than a full scope tutorial. The Python community sure knows what it likes. See&lt;/p>
&lt;p>&lt;em>pep8 Python style guide&lt;/em>
&lt;a href="https://www.python.org/dev/peps/pep-0008/">&lt;strong>Welcome to Python.org&lt;/strong>
*This document gives coding conventions for the Python code comprising the standard library in the main Python…*www.python.org&lt;/a>&lt;/p>
&lt;p>&lt;em>pep257&lt;/em>
&lt;a href="https://www.python.org/dev/peps/pep-0257/">&lt;strong>Welcome to Python.org&lt;/strong>
*The aim of this PEP is to standardize the high-level structure of docstrings: what they should contain, and how to say…*www.python.org&lt;/a>&lt;/p>
&lt;p>for more on Pythonic guides.&lt;/p>
&lt;h3 id="new-to-python">new to Python?&lt;/h3>
&lt;p>This looks like a good starting point
&lt;a href="http://thepythonguru.com/getting-started-with-python/">&lt;strong>Getting started with python - The Python Guru&lt;/strong>
*Python is a general purpose programming language created by Guido Van Rossum. Python is most praised for its elegant…*thepythonguru.com&lt;/a>&lt;/p>
&lt;p>Here’s a great list of tutorials…&lt;/p>
&lt;p>For programmers:
&lt;a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">&lt;strong>BeginnersGuide/Programmers - Python Wiki&lt;/strong>
*Because this is a Wiki page, users can edit it. You are therefore free to add details of material that other Python…*wiki.python.org&lt;/a>&lt;/p>
&lt;p>For beginners
&lt;a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">&lt;strong>BeginnersGuide/NonProgrammers - Python Wiki&lt;/strong>
*If you&amp;rsquo;ve never programmed before, the tutorials on this page are recommended for you; they don&amp;rsquo;t assume that you have…*wiki.python.org&lt;/a>&lt;/p>
&lt;h3 id="python-communities">Python Communities&lt;/h3>
&lt;p>&lt;strong>Reddit:&lt;/strong>
&lt;a href="http://reddit.com/r/learnpython">&lt;strong>Python Education * /r/learnpython&lt;/strong>
*Subreddit for posting content, questions, and asking for general advice about learning the Python programming language.*reddit.com&lt;/a>
&lt;a href="http://reddit.com/r/python">&lt;strong>Python * /r/Python&lt;/strong>
&lt;em>news about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python&lt;/em>reddit.com&lt;/a>
&lt;a href="http://reddit.com/r/learnprogramming">&lt;strong>learn programming * /r/learnprogramming&lt;/strong>
*A subreddit for all questions related to programming in any language.*reddit.com&lt;/a>&lt;/p>
&lt;p>&lt;strong>Gitter:&lt;/strong>
&lt;a href="http://gitter.im/FreeCodeCamp/FreeCodeCamp">&lt;strong>FreeCodeCamp/FreeCodeCamp&lt;/strong>
*Welcome to our main chat room. We have many official chat rooms for hanging out and getting help. Here&amp;rsquo;s the list…*gitter.im&lt;/a>
&lt;a href="http://gitter.im/FreeCodeCamp/Python">&lt;strong>FreeCodeCamp/python&lt;/strong>
*This is the best place to discuss Python and get help with it. Be sure to check out https://github.com/freecodecamp…*gitter.im&lt;/a>&lt;/p>
&lt;p>IRC:
&lt;a href="https://www.python.org/community/irc/">&lt;strong>Python.org -IRCGuide&lt;/strong>
&lt;em>The official home of the Python Programming Language&lt;/em>www.python.org&lt;/a>&lt;/p></description></item><item><title>Applying JavaScript: User Scripts</title><link>https://www.t3h2mas.xyz/post/user-scripts/</link><pubDate>Fri, 01 Apr 2016 04:05:25 -0600</pubDate><guid>https://www.t3h2mas.xyz/post/user-scripts/</guid><description>&lt;p>&lt;figure>
&lt;img src="https://www.t3h2mas.xyz/images/user-scripts/browsers.jpeg"
alt="different browser icons"/>
&lt;/figure>
&lt;a href="http://incautusofficial.deviantart.com/art/Flat-Browsers-391657414">Art credit&lt;/a>&lt;/p>
&lt;p>Writing a Userscript is a fun way to use your JavaScript skills. It takes you out of the editor into the browser with real time feedback and validation.&lt;/p>
&lt;h3 id="what-is-a-user-script">What is a user script?&lt;/h3>
&lt;blockquote>
&lt;p>Userscripts &lt;em>(a.k.a User Scripts, User scripts, or .user.js)&lt;/em> are open-source licensed add-ons for web browsers that change web pages as they are loaded. They give users the power to make websites do what they want them to, rather than what was originally intended.&lt;/p>
&lt;/blockquote>
&lt;p>User scripts are written in JavaScript and allow you to tweak the look and feel of a webpage on your browser. The scripts only effect your browser, not the actual webpage.&lt;/p>
&lt;h3 id="a-quick-warning">A quick warning&lt;/h3>
&lt;blockquote>
&lt;p>You should be aware of privacy issues when using userscripts, and should not install them from sources you do not trust. Userscripts can carry out actions on your behalf and can potentially access any information on a website you have access to, or that you enter into a website. They are often permitted to carry out functions that scripts on normal websites cannot, such as storing information on your computer and sharing it between website. Badly written userscripts could potentially also be exploited by malicious websites.&lt;/p>
&lt;/blockquote>
&lt;p>&lt;em>explanations taken from&lt;/em> &lt;a href="https://github.com/OpenUserJs/OpenUserJS.org/wiki/Userscript-Beginners-HOWTO">https://github.com/OpenUserJs/OpenUserJS.org/wiki/Userscript-Beginners-HOWTO&lt;/a>&lt;/p>
&lt;h3 id="why-user-scripts">Why user scripts?&lt;/h3>
&lt;p>Free Code Camp has a lot of great real world projects that will enrich your skill set and portfolio. I personally like using the skills I have learned in JavaScript, jQuery, and CSS to modify my day to day browsing.&lt;/p>
&lt;p>Some User Scripts have been extremely popular and were made into browser extensions. An example of one would be the Reddit Enhancement Suite found at &lt;a href="http://redditenhancementsuite.com/">http://redditenhancementsuite.com/&lt;/a>.&lt;/p>
&lt;p>You too could use your user script as a base of a browser extension!&lt;/p>
&lt;h2 id="getting-started">Getting started&lt;/h2>
&lt;p>User scripts are run from browser extensions themselves. Grease Monkey (FireFox) was the pioneer add on to allow people to customize their browsing experience. Install the appropriate plug in for your browser.&lt;/p>
&lt;p>For FireFox: &lt;strong>&lt;em>Grease Monkey&lt;/em>&lt;/strong>
&lt;a href="https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/">&lt;strong>Greasemonkey&lt;/strong>
*The Development Channel lets you test an experimental new version of this add-on before it&amp;rsquo;s released to the general…*addons.mozilla.org&lt;/a>&lt;/p>
&lt;p>For Chrome: &lt;strong>&lt;em>Tamper Monkey&lt;/em>&lt;/strong>
&lt;a href="https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en">&lt;strong>Tampermonkey&lt;/strong>
&lt;em>The most popular userscript manager for Blink-based browsers&lt;/em>chrome.google.com&lt;/a>&lt;/p>
&lt;p>For this tutorial I will be using Chrome with Tamper Monkey.&lt;/p>
&lt;p>There shouldn’t be any significant differences with the process after installing either Grease Monkey or Tamper Monkey.&lt;/p>
&lt;p>Just in case, here is a quick link to installing Grease Monkey (as well as making a few things with it.)
&lt;a href="http://hayageek.com/greasemonkey-tutorial/#install-greasemonkey">&lt;strong>Greasemonkey Tutorial for Beginners&lt;/strong>
*In Greasemonkey tutorial, I have covered how to write Greasemonkey user scripts. After this tutorial,you will be able…*hayageek.com&lt;/a>&lt;/p>
&lt;h2 id="the-project">the project&lt;/h2>
&lt;p>We are going to be making a slight change to the home page on Hacker News &lt;a href="http://news.ycombinator.com">http://news.ycombinator.com&lt;/a>.&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2378/1*Gk3SbYr0W3BF85PEd937tw.jpeg" alt="HackerNews homepage">&lt;em>HackerNews homepage&lt;/em>&lt;/p>
&lt;p>We will be using jQuery to make alternating links background colors slightly different to improve readability.&lt;/p>
&lt;h3 id="start-a-new-script">start a new script&lt;/h3>
&lt;p>Click on the Tamper Monkey icon in the top right and select ‘&lt;em>Add a new script’&lt;/em> from the dialog box.&lt;/p>
&lt;p>You should be brought to a new tab that looks like this&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2722/1*gR0YqSsl6jLKi2Q0CHTo2w.jpeg" alt="new script page">&lt;em>new script page&lt;/em>&lt;/p>
&lt;h3 id="fill-in-the-information">Fill in the information&lt;/h3>
&lt;p>After starting a new script the first thing we want to do is fill in the script information at the top. Go ahead and fill in the following attributes how ever you want&lt;/p>
&lt;ul>
&lt;li>
&lt;p>name&lt;/p>
&lt;/li>
&lt;li>
&lt;p>description&lt;/p>
&lt;/li>
&lt;li>
&lt;p>author&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>I will show you what mine looks like as well.&lt;/p>
&lt;h3 id="add-jquery">Add jQuery&lt;/h3>
&lt;p>right before the line&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#4e4e4e">// ==/UserScript==
&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>add a line with the text of&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">&lt;span style="color:#4e4e4e">// @require http://code.jquery.com/jquery-latest.js
&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Think of this as importing/requiring jQuery for a JS project.&lt;/p>
&lt;h3 id="here-is-mine">here is mine&lt;/h3>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2454/1*FUvxUM49iidNY9inCu5-eQ.jpeg" alt="script info filled out">&lt;em>script info filled out&lt;/em>&lt;/p>
&lt;h2 id="hello-scriptjs">Hello script.js!&lt;/h2>
&lt;p>Let’s see if it our script loads on &lt;a href="http://news.ycombinator.com">http://news.ycombinator.com&lt;/a> and jQuery is good to go.&lt;/p>
&lt;p>After the // *your code here *line add the following code&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">$(&lt;span style="color:#0087ff">document&lt;/span>).ready(&lt;span style="color:#0087ff">function&lt;/span> () {
alert(&lt;span style="color:#00afaf">&amp;#34;WINNING&amp;#34;&lt;/span>);
});
&lt;/code>&lt;/pre>&lt;/div>&lt;p>and enter **Ctrl + s **or click on the save button on the left&lt;/p>
&lt;p>You may be brought to the following page. If not, click on the Installed userscripts tab.&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2720/1*3tUeAEJdd8KIFEsRKcSISQ.jpeg" alt="installed userscripts page">&lt;em>installed userscripts page&lt;/em>&lt;/p>
&lt;p>Awesome! Out script is loaded into Tamper Monkey. The green dot on the left means that the script is turned on. You can even see the Hacker News logo in the screenshot.&lt;/p>
&lt;h3 id="execute-the-script">execute the script&lt;/h3>
&lt;p>Visit &lt;a href="http://news.ycombinator.com">Hacker News&lt;/a> in your browser and if you’ve been following along and everything went well, you should see&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2706/1*0r0uIH20bDZQEcFgIAjBrg.jpeg" alt="working alert dialog">
&lt;em>working alert dialog&lt;/em>&lt;/p>
&lt;h2 id="fire-up-the-debugger">Fire up the debugger&lt;/h2>
&lt;p>It’s time to find the post elements we want to modify. Enter &lt;strong>&lt;em>Ctrl + Shift + i&lt;/em>&lt;/strong> to bring up the browser debugger.&lt;/p>
&lt;p>Now we want to select an element to take a closer look. Clicking on the blue square with the mouse in it at the top left of the debugger will open the element selector. You can also use the key command &lt;strong>&lt;em>Ctrl + Shift + c&lt;/em>&lt;/strong>.&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2000/1*N9b1EQ2E5ss_FMxCCj7k8Q.jpeg" alt="element selector">&lt;em>element selector&lt;/em>&lt;/p>
&lt;p>As you can see I found an element called &lt;em>td.title&lt;/em>. After clicking on it the element is highlighted in the elements tab of the debugger (also shown above.)&lt;/p>
&lt;p>Highlighting the element above our title called&lt;/p>
&lt;pre>&lt;code>&amp;lt;tr class=&amp;quot;athing&amp;quot;&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>selects this in the browser&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2000/1*UGSrDikAyEtako1hlS-vRw.jpeg" alt="BINGO">&lt;em>BINGO&lt;/em>&lt;/p>
&lt;p>Bingo. It looks like we found the element we want. Hacker News has a clean HTML layout so it wasn’t too difficult to find our target element.&lt;/p>
&lt;p>If you remember your jQuery, all you have to do to find all of the post elements is use the selector&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">$(&lt;span style="color:#00afaf">&amp;#34;.athing&amp;#34;&lt;/span>);
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="do-something-to-our-post-element">do something to our post element&lt;/h3>
&lt;p>Now that we have a way to select our element with jQuery we can alter our element. Let’s change the background color of the posts using the following code. Change the $(document).ready() code to this&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">$(&lt;span style="color:#0087ff">document&lt;/span>).ready(&lt;span style="color:#0087ff">function&lt;/span> () {
$(&lt;span style="color:#00afaf">&amp;#34;.athing&amp;#34;&lt;/span>).css(&lt;span style="color:#00afaf">&amp;#34;background-color&amp;#34;&lt;/span>, &lt;span style="color:#00afaf">&amp;#34;#DDD&amp;#34;&lt;/span>);
});
&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;em>note: &lt;code>#DDD&lt;/code> is shorthand for &lt;code>#DDDDDD&lt;/code>&lt;/em>&lt;/p>
&lt;p>let’s look at the resulting page. Remember to save your userscript then refresh the HackerNews page. You may have to close your debugger to view the whole page.&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2000/1*zGkr4EAf3RvRNYkDHN6YbQ.jpeg" alt="altered Hacker News front page">&lt;/p>
&lt;p>&lt;em>altered Hacker News front page&lt;/em>&lt;/p>
&lt;p>Are we done yet? Not quite. We have changed all of our post elements instead of alternating. It may look like the zebra effect we wanted but that’s only because the score/subinfo element isn’t effected._ If you feel inclined to alter that element as well please do and feel free to post your method in the comments. It’s outside of the scope of this guide._&lt;/p>
&lt;p>&lt;em>Oh no?! What do we do… I don’t want to write any loops!&lt;/em>&lt;/p>
&lt;h3 id="jquery-to-the-rescue">jQuery to the rescue&lt;/h3>
&lt;p>Have no fear, fellow Campers. jQuery has come to the rescue yet again.&lt;/p>
&lt;p>jQuery provides special selectors just for an occasion like this.&lt;/p>
&lt;p>Now introducing &lt;strong>:odd**
&lt;/strong>:odd Selector**
*Description: Selects odd elements, zero-indexed. See also even. In particular, note that the 0-based indexing means…*api.jquery.com&lt;a href="https://api.jquery.com/odd-selector/">&lt;/a>&lt;/p>
&lt;p>all we have to do is add &lt;strong>:odd&lt;/strong> to the end of our selector so that the line looks like this. &lt;strong>&lt;em>note: I have also changed the color to #EEE; to blend in better.&lt;/em>&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#8a8a8a;background-color:#1c1c1c;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-js" data-lang="js">$(&lt;span style="color:#00afaf">&amp;#34;.athing:odd&amp;#34;&lt;/span>).css(&lt;span style="color:#00afaf">&amp;#34;background-color&amp;#34;&lt;/span>, &lt;span style="color:#00afaf">&amp;#34;#EEE&amp;#34;&lt;/span>);
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Save your script and refresh HackerNews and you should see this final product&lt;/p>
&lt;p>&lt;img src="https://cdn-images-1.medium.com/max/2000/1*gwAAteQdQMSoQm3klQHG7Q.jpeg" alt="final product">&lt;/p>
&lt;p>&lt;em>final product&lt;/em>&lt;/p>
&lt;h2 id="wrapping-up">Wrapping up&lt;/h2>
&lt;p>There you have it. Now you have another creative outlet to unleash your budding coding wizardry on! User Scripts can be used to tweak the functionality or look of a site, to add a feature you’ve always wanted, plus much more.&lt;/p>
&lt;h3 id="homework">Homework&lt;/h3>
&lt;p>Write your own User Script to add something to a website you use often. Whether it be styling or a button that can toggle the visibility of certain elements, it’s all up to you. Provide your product in the comments here!&lt;/p>
&lt;p>Go forth and conquer Campers!&lt;/p></description></item></channel></rss>