<?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 Travis Elnicky on Medium]]></title>
        <description><![CDATA[Stories by Travis Elnicky on Medium]]></description>
        <link>https://medium.com/@_elnicky?source=rss-c9d7134fb1ea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4LV8eQJDhmKr8psEgZ6ksA.jpeg</url>
            <title>Stories by Travis Elnicky on Medium</title>
            <link>https://medium.com/@_elnicky?source=rss-c9d7134fb1ea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 20:39:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@_elnicky/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[Using Control Flow in Elixir to Improve Discoverability]]></title>
            <link>https://medium.com/podium-engineering/using-control-flow-in-elixir-to-improve-discoverability-21341b27d851?source=rss-c9d7134fb1ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/21341b27d851</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[code-quality]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[discoverability]]></category>
            <category><![CDATA[control-flow]]></category>
            <dc:creator><![CDATA[Travis Elnicky]]></dc:creator>
            <pubDate>Wed, 06 Mar 2019 20:01:22 GMT</pubDate>
            <atom:updated>2019-03-14T12:13:31.328Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tAijf8qwFsyB9vRn" /><figcaption>Photo by <a href="https://unsplash.com/@adamjang?utm_source=medium&amp;utm_medium=referral">Adam Jang</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>At Podium we focus on writing discoverable code. Code is read many more times than it is written, so it is important to us that our code is understandable. With all the different ways to branch control flow in Elixir, we’ve come to realize that the method you choose can convey meaning to the reader. Here are a couple of guidelines we tend to follow in order to boost the discoverability and convey purpose to the reader.</p><h3><strong>IF</strong></h3><p>With all the more flashy ways to branch in Elixir, if statements are often left under-utilized. However, if statements are the most universally familiar type of control flow, and almost every language implements them. Given that, we try to use them frequently. Our guideline has been, when branching on a condition that does not benefit from pattern matching, and you only have one or two possible outcomes, use an if statement.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66fda30b189c438a51c47f2021ca19b2/href">https://medium.com/media/66fda30b189c438a51c47f2021ca19b2/href</a></iframe><h3><strong>CASE</strong></h3><p>case statements are typically the go-to conditional when a pattern match leads to two or more branches. The most common use case for us is matching status tuples like {:ok, “success&quot;} and {:error, “failure&quot;}. These types of branches around static pattern matching are quick for a reader to pick up on, and they support a lot of flexibility for the writer.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1def3d662b2ed90ebc970720bedc8f9e/href">https://medium.com/media/1def3d662b2ed90ebc970720bedc8f9e/href</a></iframe><h3><strong>COND</strong></h3><p>This one is typically not used often, but it may come up if you have many conditional expressions you’d like to check against.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/32eb32a8081a9416617dc92b11fabd74/href">https://medium.com/media/32eb32a8081a9416617dc92b11fabd74/href</a></iframe><h3><strong>WITH</strong></h3><p>Here at Podium, we’ve been writing in elixir since before with came into existence. We were thrilled to see this new control flow option land. Before with we had a lot of code that contained nested branching case statements, that quickly became hard to follow. with is great at describing a control flow that contains multiple steps and branching. That said, if you are not too careful, with statements can become overly complex and hard to follow. We try to limit our branching logic inside a with statement to help reduce that complexity. Once a with statement reaches more than three or four branches, we consider further abstraction of the behavior.</p><p>Another thing to be careful about is ensuring your with statement starts and ends with &lt;-. You can put assignments in the middle, but they are only necessary if you’d like to bind the variable inside the with statement. Take this example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ec260e1458b85d6206c1beffbaceb1f/href">https://medium.com/media/8ec260e1458b85d6206c1beffbaceb1f/href</a></iframe><p>This can be rewritten like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45bb6e86a25584dbf28ff5ffbb55d8bd/href">https://medium.com/media/45bb6e86a25584dbf28ff5ffbb55d8bd/href</a></iframe><p>Here is a real world example of a with statement we refactored to use if statements instead. The code below takes a changeset that may contain an image_url. If that image url is a url that is cached in our cdn, then we update the image_url to the cdn location and set the image_source_url to the original url. Here is the original code that was written with the with statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eead075f18de2647719968f1a3804eb0/href">https://medium.com/media/eead075f18de2647719968f1a3804eb0/href</a></iframe><p>Here is the same behavior refactored to improve the readability of the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cb5c17e86d8a652fd2a8995d08e4f2f/href">https://medium.com/media/0cb5c17e86d8a652fd2a8995d08e4f2f/href</a></iframe><p>After exploring both options for this behavior, we found the second to be the most discoverable for future readers.</p><p>We have also found with statements to be useful for returning meaningful errors, and executing code after we know something has been successful. If you are familiar with Ruby, it feels similar to the tap method, but the “block” in this case conditionally executes. A great use case for example would be writing something to the database and then if it was successful publish the new resource to any clients subscribed on a websocket.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adfde807ac78a405501baa155d59f6dd/href">https://medium.com/media/adfde807ac78a405501baa155d59f6dd/href</a></iframe><p>A nice benefit of the above pattern is that when an error occurs, it is returned from the with statement. We have established a useful pattern around this: when an error occurs, we return an error tuple with a changeset. Our controllers then handle rendering the invalid changeset as an error response.</p><h3><strong>Functional pattern matching</strong></h3><p>As a unique property of Elixir, pattern matching at the function level tailors functions to specific concerns, making certain functions more clear and readable. This control flow can become less discoverable when you have a lot of pattern matching happening or many branches of logic. When we use this technique here at Podium, we try to limit the pattern matching to specific cases like base cases in recursion. We find that by adhering to this guideline we are able to keep our functions focused but also readable.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ff44160767035407547a35771ec1e45/href">https://medium.com/media/0ff44160767035407547a35771ec1e45/href</a></iframe><h3><strong>In Conclusion</strong></h3><p>From the beginning of our Elixir journey here at Podium, we’ve found the language to be expressive and a true joy to work in on a daily basis. When it comes to control flow and branching logic, we’ve had a lot of great opportunities to iterate on best practices, maximizing the readability and discoverability of our code. The net result of this has been increased velocity for our engineering team. As mentioned at the beginning of this article, code is read many more times than it is written, and hopefully this synopsis on Elixir control flow and our learnings over time can help other teams decide how to structure their code in the future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21341b27d851" width="1" height="1" alt=""><hr><p><a href="https://medium.com/podium-engineering/using-control-flow-in-elixir-to-improve-discoverability-21341b27d851">Using Control Flow in Elixir to Improve Discoverability</a> was originally published in <a href="https://medium.com/podium-engineering">podium-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Raw queries in Elixir with Ecto]]></title>
            <link>https://medium.com/@_elnicky/raw-queries-in-elixir-with-ecto-3240a5892f4f?source=rss-c9d7134fb1ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/3240a5892f4f</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[ecto]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Travis Elnicky]]></dc:creator>
            <pubDate>Sat, 10 Sep 2016 19:36:40 GMT</pubDate>
            <atom:updated>2016-09-10T19:36:40.177Z</atom:updated>
            <content:encoded><![CDATA[<p>I have really enjoyed learning Ecto lately. That said, when you come across a query expression that the API does not support yet, you need a lower level way of executing SQL. Awhile back, I needed to reach for this while attempting to use the <em>UNION</em> keyword.</p><h4>Here’s how you execute a raw query in Ecto</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/347f84c02cb8e97d3185140ec1c303b1/href">https://medium.com/media/347f84c02cb8e97d3185140ec1c303b1/href</a></iframe><h4>Inserting Variables</h4><p>One thing I’d like to point out here is that we are able to insert a list of variables that will be sanitized and included in the query by passing them as the third argument to <em>query!</em>. Inside the query itself you reference the variables using a <em>$1</em> notation. Where <em>1</em> is referring to the first element in the list. If I had a second variable to pass, I would refer to it as <em>$2</em> in the query.</p><h4>Return Value</h4><p>The return value of this function is not your typical Ecto model struct. It returns a Map which at the very least contains the two keys <em>:num_rows</em>, referring to the number of rows affected, and <em>:rows</em> which is a List of Lists representing the result set.</p><p>Ecto is a pretty powerful library and this was the first time I had ever needed to fallback to a raw SQL query. It certainly is not ideal to have to do this, however, when working in such a new ecosystem it is comforting to know that escape hatches like these exist for times when the API doesn’t support what you are needing to accomplish.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3240a5892f4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>