<?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[Stories by Will Ulman on Medium]]></title>
        <description><![CDATA[Stories by Will Ulman on Medium]]></description>
        <link>https://medium.com/@will.ulman?source=rss-8a690b3f75c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_6U8irK5_H_-JnBw-RhcVg.jpeg</url>
            <title>Stories by Will Ulman on Medium</title>
            <link>https://medium.com/@will.ulman?source=rss-8a690b3f75c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 08:10:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@will.ulman/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[Routes and Props in React]]></title>
            <link>https://medium.com/@will.ulman/routes-and-props-in-react-aec3a65c60d8?source=rss-8a690b3f75c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/aec3a65c60d8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react-router]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Will Ulman]]></dc:creator>
            <pubDate>Tue, 16 Apr 2019 14:10:40 GMT</pubDate>
            <atom:updated>2019-04-16T14:10:40.093Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://reacttraining.com/react-router/">React Router</a> is a powerful library that allows React developers to intuitively and quickly handle navigation within their web applications. No longer confined to a single URL, an app using React Router can in fact be composed of different HTML pages that, in the source code, are written almost as if they were plain React components.</p><p>One important nuance of React Routes, however, is often glossed over in tutorials, and even the official documentation is not terribly clear about it: How do we pass props into our routes? It&#39;s a very basic question, and the unsuspecting newcomer to React Router (myself included) could reasonably expect something like the following to work:</p><pre>/* WARNING: BROKEN CODE */</pre><pre>import React, { Component } from &#39;react&#39;<br>import { BrowserRouter as Router, Route } from &#39;react-router-dom&#39;<br>import Login from &#39;./components/Login.js&#39;</pre><pre>class App extends Component {</pre><pre>  [. . .]</pre><pre>  render() {<br>    return (<br>      &lt;Router&gt;<br>        &lt;React.Fragment&gt;<br>          &lt;Route exact path=&quot;/login&quot;<br>                 component={Login}<br>                 handleLogin={this.handleLogin}<br>                 handleSignup={this.handleSignup} /&gt;<br>        &lt;/React.Fragment&gt;<br>      &lt;/Router&gt;<br>    )<br>  }<br>}</pre><pre>export default App</pre><p>Here we have an App component that has a child component of Login, which we are trying to assign to the /login route. Login takes two props, handleLogin and handleSignup, which are functions that will be called back up to App when a user tries to log in or sign up, respectively.</p><p>The problem here is that the Login component isn&#39;t actually receiving the handleLogin or handleSignup props. Instead, the Route component itself is receiving the props, which promptly disappear into the ether when the /login page is rendered.</p><p>If we are trying to pass props into our React Routes in the above way, we must make two changes:</p><ol><li>Use the render prop instead of the component prop.</li><li>Wrap our components as if they were functional components.</li></ol><p>Here would be the easiest way to make these changes:</p><pre>&lt;Route exact path=&quot;/login&quot;<br>       render={ (props) =&gt; (&lt;Login {...props}<br>                  handleLogin={this.handleLogin}<br>                  handleSignup={this.handleSignup} /&gt;) } /&gt;</pre><p>What&#39;s happening here? Because render allows (expects, actually) a functional component, we can wrap our actual Login component inside of an arrow function that takes in some props as an argument. By passing those props into a spread operator, we can not only add in our custom handleLogin and handleSignup props, but we also have access to the <a href="https://reacttraining.com/react-router/web/api/Route/route-props">three default Route props</a> (match, location, and history), which can aid further in our navigation.</p><p>Alternatively, we can write a separate function that handles the props for us. We can then call this function inside our Route component a bit more cleanly, without having to define our custom props inline:</p><pre>import React, { Component } from &#39;react&#39;<br>import { BrowserRouter as Router, Route } from &#39;react-router-dom&#39;<br>import Login from &#39;./components/Login.js&#39;</pre><pre>class App extends Component {</pre><pre>/* . . . */</pre><pre>const Login = (props) =&gt; {<br>  return (<br>    &lt;Login<br>      {...props}<br>      handleLogin={this.handleLogin.bind(this)}<br>      handleSignup={this.handleSignup.bind(this)}<br>    /&gt;<br>  )<br>}</pre><pre>render() {<br>    return (<br>      &lt;Router&gt;<br>        &lt;React.Fragment&gt;<br>          &lt;Route exact path=&quot;/login&quot; render={Login} /&gt;<br>        &lt;/React.Fragment&gt;<br>      &lt;/Router&gt;<br>    )<br>  }<br>}</pre><pre>export default App</pre><p>In addition to allowing us to pass our own props into our components, the render prop also has a performance edge over the component prop. With component, the component is instantiated each and every time that the route corresponding to that component is rendered. In other words, the mounting → unmounting → remounting life cycle is executed over and over while our user is navigating our React app. The render prop, by contrast, takes in a functional component, which is merely <em>evaluated</em> on each render, <em>sans </em>lifecycle methods.</p><p>Because the render prop is less expensive computationally and allows the passage of custom props into our components, I recommend using render whenever you are incorporating React Router into your project.</p><h4>Further reading</h4><ul><li><a href="https://medium.com/alturasoluciones/how-to-pass-props-to-routes-components-29f5443eee94">How to pass props to React routes components</a></li><li><a href="https://tylermcginnis.com/react-router-pass-props-to-components/">Pass props to a component rendered by React Router</a></li><li><a href="https://github.com/ReactTraining/react-router/issues/4105#issuecomment-287262726">How to pass props to components when you use &lt;Route components={{}} /&gt; attribute ?</a></li><li><a href="https://www.robinwieruch.de/react-pass-props-to-component/">How to pass props to components in React</a> (especially &quot;Pass props with React Router&quot;)</li><li><a href="https://stackoverflow.com/questions/48150567/react-router-difference-between-component-and-render">react router difference between component and render</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aec3a65c60d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JSX and Ternary Expressions]]></title>
            <link>https://medium.com/@will.ulman/jsx-and-ternary-expressions-55085c10255f?source=rss-8a690b3f75c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/55085c10255f</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jsx]]></category>
            <dc:creator><![CDATA[Will Ulman]]></dc:creator>
            <pubDate>Thu, 11 Apr 2019 14:34:57 GMT</pubDate>
            <atom:updated>2019-04-16T11:37:19.896Z</atom:updated>
            <content:encoded><![CDATA[<p>Web developers who are using React.js for the first time as their front-end library of choice may be alarmed by <a href="https://reactjs.org/docs/introducing-jsx.html">JSX</a>, an unfamiliar syntax that looks like the product of a mad scientist who fused HTML and JavaScript together in the laboratory. Although you aren&#39;t obligated to use it in your React application, JSX comes with a number of benefits. Not only are there performance optimizations that happen under the hood in JSX, but code written in JSX more closely corresponds syntactically to what your application will actually look like. Once you get comfortable with reading and writing JSX, you will likely appreciate it despite its quirks.</p><p>One of those quirks has to do with conditional rendering. Whenever I have to handle conditional logic in JavaScript, the tool I tend to reach for first is an if-else statement. I usually favor legibility over concision as a developer, and even if I know my audience will be intimately familiar with switch cases, ternaries, and the logical &amp;&amp;, I can rest assured that everyone will instantly recognize what an if-else is doing, without having to pause for any mental translation.</p><p>In JSX, however, there is a major barrier that you should be aware of before you reach for if-else syntax in your conditional rendering. To use a simple example, let&#39;s say that we have an app that displays a list of user profile cards. Each card has a front side, displaying the user&#39;s profile picture, and a back side, displaying the user&#39;s name. Depending on whether or how many times a card has been clicked, our app displays either the front or back of the card.</p><p>Because we are handling some conditional logic in this process, you may immediately think to use an if-else. In JSX, however, the following syntax is not valid:</p><pre>/* WARNING: INVALID CODE */</pre><pre>import React from ‘react’</pre><pre>const UserCard = (props) =&gt; {<br>  return(<br>    &lt;div className=&quot;userCard&quot;<br>      onClick={ () =&gt; {props.handleClick(props.user.id)} } &gt;<br>      { if (props.user.clicked) { &lt;BackContent user={props.user.name}/&gt;<br>        } else { &lt;FrontContent user={props.user.image}/&gt; } }<br>    &lt;/div&gt;<br>  )<br>}</pre><pre>export default UserCard</pre><p>Putting if-else logic inside your return statement will yield an error, but putting ternary logic inside your return statement will work beautifully. Behold:</p><pre>import React from ‘react’</pre><pre>const UserCard = (props) =&gt; {<br>  return(<br>    &lt;div className=&quot;userCard&quot;<br>      onClick={ () =&gt; {props.handleClick(props.user.id)} } &gt;<br>      { props.user.clicked <br>          ? &lt;BackContent user={props.user.name}/&gt;<br>          : &lt;FrontContent user={props.user.image}/&gt; }<br>    &lt;/div&gt;<br>  )<br>}</pre><pre>export default UserCard</pre><p>Why is this the case? The difference hinges on the difference in JavaScript between an expression and a statement. An expression is any piece of code that can be immediately evaluated to a value. A statement is an instruction to perform a series of one or more steps. A ternary expression is an expression and an if-else statement is, well, a statement. Because JSX expects <em>expressions </em>inside each pair of curly brackets, only ternary logic would work in this situation.</p><p>If a component in your React application calls for very complex conditional rendering, you can still use an if-else statement, but its result must be assigned to a value, which is then used inside the return statement:</p><pre>import React from ‘react’</pre><pre>const UserCard = (props) =&gt; {<br>  let content<br>  if (props.user.clicked) {<br>    content = &lt;BackContent user= {props.user.name} /&gt;<br>  } else {<br>    content = &lt;FrontContent user={props.user.image} /&gt;<br>  }</pre><pre>  return(<br>    &lt;div className=&quot;userCard&quot;<br>      onClick={ () =&gt; {props.handleClick(props.user.id)} } &gt;<br>      { content }<br>    &lt;/div&gt;<br>  )<br>}</pre><pre>export default UserCard</pre><p>You can follow a similar principle if you need to use a switch statement or declare an enum to handle your conditional rendering.</p><p>All of this goes to show that although JSX is technically a syntactic extension of JavaScript, its quirks can end up clarifying basic features of the language and strengthen your understanding of it.</p><h4>Further reading</h4><ul><li><a href="https://react-cn.github.io/react/tips/if-else-in-JSX.html">If-Else in JSX</a></li><li><a href="http://www.hackingwithreact.com/read/1/7/how-to-write-if-else-conditional-statements-in-jsx">How to Write if/else Conditional Statements in JSX</a></li><li><a href="https://medium.freecodecamp.org/conditional-rendering-in-react-using-ternaries-and-logical-and-7807f53b6935">Conditional Rendering in React using Ternaries and Logical AND</a></li><li><a href="https://www.robinwieruch.de/conditional-rendering-react/">All React Conditional Rendering Techniques</a></li><li><a href="https://medium.com/launch-school/javascript-expressions-and-statements-4d32ac9c0e74">JavaScript Expressions and Statements</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55085c10255f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[form_for: What’s it for?]]></title>
            <link>https://medium.com/@will.ulman/form-for-whats-it-for-abadac60872?source=rss-8a690b3f75c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/abadac60872</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Will Ulman]]></dc:creator>
            <pubDate>Thu, 07 Mar 2019 15:52:45 GMT</pubDate>
            <atom:updated>2019-03-08T15:07:34.879Z</atom:updated>
            <content:encoded><![CDATA[<p>Out of the many useful tools that Ruby on Rails affords a web developer, form_for is perhaps one of the most magical. But it can be a tricky tool to use when the associations between your models are complicated. In this short article, I&#39;ll explain when to use form_for and how to use it in a couple of different scenarios.</p><h4>form_tag vs. form_for</h4><p>Along with the newer and shinier <a href="https://medium.com/@tinchorb/form-with-building-html-forms-in-rails-5-1-f30bd60ef52d">form_with</a>, form_tag and form_for are the standard tools for generating HTML forms in Rails. The same form can often be created in a Rails app using either form_tag or form_for. We can see this in an example of a basic workout application, in which a user can create a new exercise with just a name:</p><pre>&lt;% # app/views/exercises/new.html.erb %&gt;</pre><pre>&lt;h3&gt;Create a New Exercise&lt;/h3&gt;</pre><pre>&lt;%= form_tag exercises_path do %&gt;<br>  &lt;label&gt;Exercise name:&lt;/label&gt;&lt;br&gt;<br>  &lt;%= text_field_tag :&#39;exercise[name]&#39; %&gt;&lt;br&gt;<br>  &lt;%= submit_tag &quot;Submit Exercise&quot; %&gt;<br>&lt;% end %&gt;</pre><p>The form_tag syntax can be refactored into the following form_for syntax:</p><pre>&lt;% # app/views/exercises/new.html.erb %&gt;</pre><pre>&lt;h3&gt;Create a New Exercise&lt;/h3&gt;</pre><pre>&lt;%= form_for(@exercise) do |f| %&gt;<br>  &lt;label&gt;Exercise name:&lt;/label&gt;&lt;br&gt;<br>  &lt;%= f.text_field :name %&gt;&lt;br&gt;<br>  &lt;%= f.submit %&gt;<br>&lt;% end %&gt;</pre><p>Each ERB file will generate the exact same HTML:</p><pre>&lt;h3&gt;Create a New Exercise&lt;/h3&gt;</pre><pre>&lt;form action=&quot;/exercises&quot; accept-charset=&quot;UTF-8&quot; method=&quot;post&quot;&gt;</pre><pre>&lt;input name=&quot;utf8&quot; type=&quot;hidden&quot; value=&quot;&amp;#x2713;&quot; /&gt;</pre><pre>&lt;input type=&quot;hidden&quot; name=&quot;authenticity_token&quot;<br>value=&quot;vq9SMVNk0CjwgZmYomFRhwbo5dfu7tI/2FiR7jOtlVgbj8r/zOO5oL+arU9N4PMm7WqxbUbXg4wqneW02ZfpMw==&quot; /&gt;</pre><pre>&lt;label&gt;Exercise name:&lt;/label&gt;&lt;br&gt;<br>&lt;input type=&quot;text&quot; name=&quot;exercise[name]&quot; id=&quot;exercise_name&quot; /&gt;&lt;br&gt;</pre><pre>&lt;input type=&quot;submit&quot; name=&quot;commit&quot; value=&quot;Submit Exercise&quot; /&gt;</pre><pre>&lt;/form&gt;</pre><p>So when should you use form_for instead of form_tag? form_for is perfect when:</p><ol><li>The form is manipulating your models directly.</li><li>Your application follows RESTful conventions.</li></ol><p>When you have many fields in your form, form_for prevents you from having to make multiple calls to the instance variable associated with the form. This benefit is not obvious in the above example, but when your form is editing several attributes of an object at once, form_for only makes a call to the instance variable once at the top of the loop, which will shave some running time off your application. And as long as your application uses standard RESTful routes, form_for is smart enough to take care of the routing for you, so that you can devote more attention to the form itself.</p><p>When your situation does not meet the two conditions listed above, form_tag is probably a better choice. form_tag offers you the flexibility to specify your own routing, and it is especially appropriate for things like search forms or contact forms (i.e., when there is no associated model).</p><h4>Complex associations with form_for</h4><p>When the relationships between your models become more complicated, using form_for can become tricky. For example, let&#39;s say that in our workout application, we want to be able to create a workout and list its associated exercises in a single form. A workout has_many workout exercises, and has_many exercises through: workout exercises. For a has_many, through: relationship, the form_for can become quite complex. Here is the view for a new workout form and the corresponding controller methods:</p><pre>&lt;% # app/views/workouts/new.html.erb %&gt;</pre><pre>&lt;%=form_for(@workout) do |f| %&gt;<br>  &lt;%= f.label :name %&gt;<br>  &lt;%= f.text_field :name%&gt;&lt;br&gt; &lt;br&gt;</pre><pre>  &lt;%= f.fields_for :workout_exercises,   5.times{@workout.workout_exercises.build} do |we| %&gt;</pre><pre>    &lt;%= we.label :exercise %&gt;<br>    &lt;%= we.collection_select :exercise_id, <br>      Exercise.all,<br>      :id,<br>      :name,<br>      { prompt: true } %&gt; &lt;br&gt;</pre><pre>    &lt;%= we.label :sets %&gt;<br>    &lt;%= we.number_field :sets %&gt;&lt;br&gt;</pre><pre>    &lt;%= we.label :reps %&gt;<br>    &lt;%= we.number_field :reps %&gt;&lt;br&gt;</pre><pre>    &lt;%= we.label :weight %&gt;<br>    &lt;%= we.text_field :weight %&gt;&lt;br&gt; &lt;br&gt;</pre><pre>    &lt;% end %&gt;<br>  <br>  &lt;%= f.submit %&gt;</pre><pre>&lt;% end %&gt;</pre><pre># app/controllers/workouts_controller.rb</pre><pre>class WorkoutsController &lt; ApplicationController</pre><pre>  def new<br>    @workout = Workout.new<br>  end</pre><pre>  def create<br>    @workout = Workout.new(workout_params)<br>    @workout.user_id = session[:user_id]<br>    if @workout.valid?<br>      @workout.save<br>      redirect_to workout_path(@workout)<br>    else<br>      render :new<br>    end<br>  end</pre><pre>end</pre><p>Creating new workout exercises at the same time as a workout is a bit complicated. Here, we have to call the <a href="https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for">fields_for</a> method on f (the <a href="https://apidock.com/rails/ActionView/Helpers/FormBuilder">FormBuilder object</a> generated by form_for) in order to say that the following fields are for :workout_exercises, not the workout itself. Crucially, fields_for takes a second argument of 5.times{@workout.workout_exercises.build}. What this argument does is tell FormBuilder to generate the fields five times, and each time, the workout_exercise is tied to the @workout instance. Without that instance variable, form_for will not know to associate the new workout exercises with the new workout.</p><p>In order for the attributes of the workout exercise to be passed back into the controller, we must do two things. First, the workout model must be told to accepted the nested attributes for workout exercises:</p><pre># app/models/workout.rb</pre><pre>class Workout &lt; ApplicationRecord</pre><pre>  accepts_nested_attributes_for :workout_exercises, :reject_if =&gt; :all_blank, :allow_destroy =&gt; true</pre><pre>end</pre><p>Second, the strong params in the workouts controller must include a hash with a key of workout_exercises_attributes and a value of an array containing their attribute names (as well as the id of the exercise object that it&#39;s connected to):</p><pre># app/controllers/workouts_controller.rb</pre><pre>class WorkoutsController &lt; ApplicationController</pre><pre>  private</pre><pre>  def workout_params<br>    params.require(:workout).permit(:name, :user_id,    workout_exercises_attributes: [:exercise_id, :sets, :reps, :weight, :id])<br>  end</pre><pre>end</pre><p>form_for can get finicky when your modeling becomes complex, but once you get familiar with its quirks, form_for can be a concise and powerful tool to help get your Rails app off the ground quickly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abadac60872" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[" cubes\nIce"; or, Why Beginning Programmers Should Learn to Stop Worrying and Love APIs]]></title>
            <link>https://medium.com/@will.ulman/cubes-nice-or-why-beginning-programmers-should-learn-to-stop-worrying-and-love-apis-85241e1c0267?source=rss-8a690b3f75c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/85241e1c0267</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Will Ulman]]></dc:creator>
            <pubDate>Tue, 19 Feb 2019 17:56:17 GMT</pubDate>
            <atom:updated>2019-02-19T17:56:17.561Z</atom:updated>
            <content:encoded><![CDATA[<p>When first learning a programming language, you tend to focus on the syntactical and conceptual basics: how to write a function, where semicolons go, what a loop does. Eventually, though, you have to move on from the cut-and-dry fundamentals to the tasks that occupy actual programmers—that is, designing and writing programs. Embarking on your very first projects as a programmer can be exciting, but if you’re anything like me, it can also be daunting. After a certain point, though, you have to venture away from the treadmill of tutorials and exercises and into the jungle of real software development. After undertaking my first project, I&#39;ve discovered that <a href="https://medium.freecodecamp.org/what-is-an-api-in-english-please-b880a3214a82">web APIs</a> can offer the beginner a perfect point of entry.</p><p>Why? Because APIs provide access to gobs of authentic data, much of which is intrinsically interesting or useful. From the <a href="https://metmuseum.github.io/">works of art in the Metropolitan Museum</a> to <a href="https://datahelpdesk.worldbank.org/knowledgebase/articles/902061-climate-data-api">decades of world climate data</a>, from the <a href="https://github.com/alexwohlbruck/cat-facts">entire repository of Cat Facts</a> to a <a href="https://developer.twitter.com/en/docs/tweets/search/overview/enterprise">corpus of tweets circa 2006</a>, an astounding amount of publicly available data is out there to play with, thanks to APIs.</p><p>Think of APIs like the authentic materials you might encounter while learning a foreign language. The grammar lessons, flashcards, and textbook exercises are a practical and necessary vacuum in which to hone the basics, but the best way to get fluent quickly is to get out there and talk to native speakers. Just as watching foreign soap operas and writing to international pen pals can get you further than doing classroom drills alone, using APIs can put you in the mindset of a programmer by giving you a taste of the kind of data that they might actually work with.</p><p>There are <a href="https://www.programmableweb.com/category/all/apis?order=field_popularity">many</a>, <a href="https://github.com/toddmotto/public-apis">many</a> APIs out there, and some are better for beginners than others. When I was looking for data to analyze in my first Ruby project, many of the intriguing APIs either cost money, required a lengthy approval process, or organized their data in ways complicated enough to forbid me from getting my project off the ground in less than a day or two. Luckily, I stumbled upon <a href="https://www.thecocktaildb.com/api.php">TheCocktailDB</a>, a database of drink recipes that provided free access in a very friendly <a href="http://www.json.org/">JSON</a> format—no API key required. With a set of real-life data that I could interact with using a few basic commands (and the indispensable json and rest-client gems), I was suddenly able to hone my new Ruby skills against an exciting dataset.</p><p>One terrific benefit of APIs, I learned, is that you’re suddenly thrust into dealing with someone else’s code, which might be opaque or inconsistent or incomplete. You start running into real problems that curricular exercises, by nature, tend to abstract away. To return to the foreign language analogy, you’re no longer learning from universal linguistic rules, but rather dealing with the quirks of someone’s idiolect. It&#39;s precisely through dealing with those quirks, though, that you can begin to think through and solve problems like a programmer. And APIs provide programmers with a strict enough standard of communication so that a beginner won&#39;t be completely discouraged.</p><p>One important thing I learned in the course of my project was how to tame an unruly dataset. Most of the cocktail API&#39;s recipes were consistent enough, but there were holes: drink recipes sometimes listed ingredients with no amounts; some drinks had duplicate recipes due to discrepant capitalizations (e.g., &quot;Tequila Sunrise&quot; and &quot;Tequila sunrise&quot;); some ingredients had descriptions, but others did not. I discovered one of the most baffling problems when I was testing my .recipe function, which collects and prints to the command line a given drink&#39;s name, category, ingredients, and instructions. Here&#39;s the recipe card I generated for something I scraped called &quot;Arctic Mouthwash&quot;:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*8-YZFZPG14K6EriDaGFhPw.png" /></figure><p>I cannot vouch for the mixological quality of these recipes, but I <em>can </em>say that a recipe should never list &quot;cubes ice&quot; as an ingredient. What was happening? One look at the JSON file told me everything:</p><pre><strong>{<br>  </strong>&quot;drinks&quot;: <strong>[<br>    {<br>      </strong>&quot;idDrink&quot;: &quot;17118&quot;,<br>      &quot;strDrink&quot;: &quot;Arctic Mouthwash&quot;,<br>      &quot;strDrinkES&quot;: <strong>null</strong>,<br>      &quot;strDrinkDE&quot;: <strong>null</strong>,<br>      &quot;strDrinkFR&quot;: <strong>null</strong>,<br>      &quot;strDrinkZH-HANS&quot;: <strong>null</strong>,<br>      &quot;strDrinkZH-HANT&quot;: <strong>null</strong>,<br>      &quot;strVideo&quot;: <strong>null</strong>,<br>      &quot;strCategory&quot;: &quot;Punch / Party Drink&quot;,<br>      &quot;strIBA&quot;: <strong>null</strong>,<br>      &quot;strAlcoholic&quot;: &quot;Alcoholic&quot;,<br>      &quot;strGlass&quot;: &quot;Cocktail glass&quot;,<br>      &quot;strInstructions&quot;: &quot;Blend all ingredients in a blender on high until ice is finely crushed. It should be of a slushy consistency. Pour immediately and serve.&quot;,<br>      &quot;strInstructionsES&quot;: <strong>null</strong>,<br>      &quot;strInstructionsDE&quot;: <strong>null</strong>,<br>      &quot;strInstructionsFR&quot;: <strong>null</strong>,<br>      &quot;strInstructionsZH-HANS&quot;: <strong>null</strong>,<br>      &quot;strInstructionsZH-HANT&quot;: <strong>null</strong>,<br>      &quot;strDrinkThumb&quot;: &quot;<a href="https://www.thecocktaildb.com/images/media/drink/wqstwv1478963735.jpg">https://www.thecocktaildb.com/images/media/drink/wqstwv1478963735.jpg</a>&quot;,<br>      &quot;strIngredient1&quot;: &quot;Maui&quot;,<br>      &quot;strIngredient2&quot;: &quot;Mountain Dew&quot;,<br>      &quot;strIngredient3&quot;: &quot;Ice&quot;,<br>      &quot;strIngredient4&quot;: &quot;&quot;,<br>      &quot;strIngredient5&quot;: &quot;&quot;,<br>      &quot;strIngredient6&quot;: &quot;&quot;,<br>      &quot;strIngredient7&quot;: &quot;&quot;,<br>      &quot;strIngredient8&quot;: &quot;&quot;,<br>      &quot;strIngredient9&quot;: &quot;&quot;,<br>      &quot;strIngredient10&quot;: &quot;&quot;,<br>      &quot;strIngredient11&quot;: &quot;&quot;,<br>      &quot;strIngredient12&quot;: &quot;&quot;,<br>      &quot;strIngredient13&quot;: &quot;&quot;,<br>      &quot;strIngredient14&quot;: &quot;&quot;,<br>      &quot;strIngredient15&quot;: &quot;&quot;,<br>      &quot;strMeasure1&quot;: &quot;5 oz blue &quot;,<br>      &quot;strMeasure2&quot;: &quot;5 oz &quot;,<br>      &quot;strMeasure3&quot;: &quot; cubes\n&quot;,<br>      &quot;strMeasure4&quot;: &quot;\n&quot;,<br>      &quot;strMeasure5&quot;: &quot;\n&quot;,<br>      &quot;strMeasure6&quot;: &quot;\n&quot;,<br>      &quot;strMeasure7&quot;: &quot;\n&quot;,<br>      &quot;strMeasure8&quot;: &quot;\n&quot;,<br>      &quot;strMeasure9&quot;: &quot;&quot;,<br>      &quot;strMeasure10&quot;: &quot;&quot;,<br>      &quot;strMeasure11&quot;: &quot;&quot;,<br>      &quot;strMeasure12&quot;: &quot;&quot;,<br>      &quot;strMeasure13&quot;: &quot;&quot;,<br>      &quot;strMeasure14&quot;: &quot;&quot;,<br>      &quot;strMeasure15&quot;: &quot;&quot;,<br>      &quot;dateModified&quot;: &quot;2016-11-12 15:15:35&quot;<br>    <strong>}<br>  ]<br>}</strong></pre><p>&quot;Cubes&quot; was listed as a measurement! Or, &quot; cubes\n&quot;, to be more precise. The way I was interpolating the ingredients into a string was clearly not going to work: &quot;#{ingredient_amounts[i]} #{ingredient_names[i]}&quot;. I ended up having to check whether a given amount was simply &quot;cubes,&quot; and then reformat that line accordingly.</p><p>While it was annoying, handling all of the edge cases really strengthened my skills as a programmer. I became aware of how thoroughly I should test my data, and how carefully I should be thinking about how I&#39;m scraping the data. I also had to think about how to normalize the data as elegantly and legibly as I could. In fact, it was mostly through refactoring my code that I discovered many of these bugs in the first place. Much less than a stumbling block, the API&#39;s unruliness turned out to be one of its virtues.</p><p>So, don&#39;t be afraid of working with authentic datasets as a beginning programmer. Their quirks may be frustrating, but they will only bolster your skills in the end.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85241e1c0267" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>