<?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 Mark Robinson on Medium]]></title>
        <description><![CDATA[Stories by Mark Robinson on Medium]]></description>
        <link>https://medium.com/@SparkyRobinson?source=rss-1f5bb4f033a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6-Cp8Uh5RuX1jdJjWO3zFw.jpeg</url>
            <title>Stories by Mark Robinson on Medium</title>
            <link>https://medium.com/@SparkyRobinson?source=rss-1f5bb4f033a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 23:32:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@SparkyRobinson/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[Swift: Optionally Do — Re-thinking If Let]]></title>
            <link>https://medium.com/jtribe/swift-optionally-do-re-thinking-if-let-c12a4fe3882f?source=rss-1f5bb4f033a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c12a4fe3882f</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Mark Robinson]]></dc:creator>
            <pubDate>Thu, 08 Sep 2016 05:25:35 GMT</pubDate>
            <atom:updated>2016-09-08T05:25:35.213Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yOomG3agqNl0Bao0lmcrOA.png" /><figcaption>Optionally Do !?</figcaption></figure><h4>Sometimes I wish there was a shorthand version.</h4><p>I know, I know, it’s short enough. That’s what people keep telling me, but hear me out for a second.</p><h4>??</h4><p>We have the<strong> ??</strong> operator in Swift, and it’s great because it’s a shorthand way of supplying a default value to an optional if it happens to be <em>.None.</em> For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b36465ff610f0623b98e33f3bfb82642/href">https://medium.com/media/b36465ff610f0623b98e33f3bfb82642/href</a></iframe><p>What are the advantages? Well, it’s succinct (which isn’t <em>always</em> better but in this case I think we can call it a plus point), it’s elegant, it reduces noise, and it’s also clear to the programmer what is happening given they’re aware of the operator.</p><p>I think it’s safe to say that using <strong>??</strong> is strictly better than the alternative, so with that thought in mind, I began to wonder “what can we do with <strong>if let</strong>…?”</p><figure><a href="http://jtribe.com.au/?utm_source=Medium&amp;utm_medium=Blog&amp;utm_term=SwiftOptionallyDo&amp;utm_content=InlineImage&amp;utm_campaign=jtribe%2520Medium%2520CTR"><img alt="" src="https://cdn-images-1.medium.com/max/289/1*mjU1hvlqXtQulKCEQvut6Q.png" /></a><figcaption>jtribe — Love Your Apps!</figcaption></figure><h4>What’s Wrong With If Let?</h4><p>Listen, I love <strong>if let</strong> and <strong>guard let</strong> as much as the next person. I think they’re great and all, but sometimes I just wish I didn’t need to write the full <strong>if let</strong> statement to unwrap and make sure the property I’m using is a non-optional.</p><p>In fact, sometimes before using a function there can be a number of optionals that I need to <strong>if let</strong> — which may bring flashbacks of the pyramid of doom to some of us — before I know it’s safe to use them. Why can’t we have a short-hand notation?</p><p>I’m not promising what i’ve got here is thorough in any way, shape or form and I can break it quite easily, so lets call this a thought experiment. Maybe you guys can think of some improvements or why it’s a good or bad idea, so please let me know your thoughts by commenting below.</p><h4>Operator Abuse</h4><p>Now before I go introducing you to this operator, I just have to prefix this by saying…</p><blockquote>I really dislike the abuse of operators in programming where it compromises the readability of the code.</blockquote><p>For example, take the JSON parsing library <a href="https://github.com/thoughtbot/Argo">Argo</a> from the guys at <a href="https://thoughtbot.com/">thoughtbot</a>:</p><pre>extension User: Decodable {<br>  static func decode(j: JSON) -&gt; Decoded&lt;User&gt; {<br>    return curry(User.init)<br>      &lt;^&gt; j &lt;| &quot;id&quot;<br>      &lt;*&gt; j &lt;| &quot;name&quot;<br>      &lt;*&gt; j &lt;|? &quot;email&quot; // Use ? for parsing optional values<br>      &lt;*&gt; j &lt;| &quot;role&quot; // Custom types that also conform to Decodable just work<br>      &lt;*&gt; j &lt;| [&quot;company&quot;, &quot;name&quot;] // Parse nested objects<br>      &lt;*&gt; j &lt;|| &quot;friends&quot; // parse arrays of objects<br>  }<br>}</pre><p><strong>U WOT M8!? </strong>😦</p><p>Good luck understanding anything about how that library works or what the code is doing from their “Usage tl;dr” at the bottom of the Github README. I’m sure it’s all sunshine and rainbows when you figure out what all the hieroglyphs mean but I’m not ancient Egyptian my friends, I’m not doing it.</p><p>On the other hand, take a look at the awesome <a href="https://github.com/SwiftyJSON/SwiftyJSON">SwiftyJSON</a>:</p><pre>//Getting a double from a JSON Array<br>let name = json[0].double</pre><pre>//Getting a string from a JSON Dictionary<br>let name = json[&quot;name&quot;].stringValue</pre><pre>//Getting a string using a path to the element<br>let path = [1,&quot;list&quot;,2,&quot;name&quot;]<br>let name = json[path].string</pre><pre>//Just the same<br>let name = json[1][&quot;list&quot;][2][&quot;name&quot;].string</pre><pre>//Alternatively<br>let name = json[1,&quot;list&quot;,2,&quot;name&quot;].string</pre><p>You can almost instantly tell <em>exactly </em>what it is doing <em>with very little investment.</em></p><p>Alright, I got that out of my system. We’ve well and truly established that I don’t like operator abuse which compromises readability. So why on earth am I making an operator that obfuscates the <strong>if let</strong>?</p><p>Well… I don’t think it does. Much. Allow me to explain.</p><h4>The Optionally Do— ?!</h4><p>I came up with the idea of the <strong>Optionally Do</strong> operator — <strong>?!</strong></p><pre>(left hand side) ?! (right hand side)<br>lhs ?! rhs</pre><blockquote>This operator should be read as <strong>lhs</strong> optionally do what is on the <strong>rhs</strong>, given that the <strong>lhs</strong> is a non-optional</blockquote><p>The <strong>?</strong> stipulates that the left-hand side (<strong>lhs</strong>) of the expression is some kind of Optional value. The succeeding <strong>!</strong> means that the right-hand side will only execute should the <strong>lhs</strong> be successfully unwrapped.</p><p>Ok, so you might not like the operator or the name, but lets put that aside for a second. We can change that easily. Lets see what i’d love to be able to do.</p><p>I’ll highlight the examples below with comments at the top of the code block where the code will compile, and i’ll let you know when I get to theoretical land (given my current definition of the operator above).</p><p>Here’s my really basic definition:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2fd88a709d349b1431ee6c75a7c1721/href">https://medium.com/media/b2fd88a709d349b1431ee6c75a7c1721/href</a></iframe><p>Yeah I know, I said it was basic. Sometimes we want to call a function, even if the value we’re checking isn’t going to be used. Wouldn’t it be great that instead of having to write something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a53b5969a3c9585876744ccd0531736/href">https://medium.com/media/8a53b5969a3c9585876744ccd0531736/href</a></iframe><p>We could just write this instead?</p><pre>mark ?! printHello // prints HELLO</pre><blockquote>i.e. print HELLO if <strong>mark</strong> is non optional</blockquote><p>Just like <strong>??</strong>, it’s succinct, it’s elegant, it reduces noise and it’s also clear to the programmer what is happening given they’re (now) aware of the operator. What if we do need to use the value in the function?</p><pre>// compiles</pre><pre>func sayHi(to someone: String) {<br>    print(“Hello \(someone)”)<br>}</pre><pre>mark ?! sayHi // prints Hello mark</pre><p>I think the above is cool in the sense that I don’t even need to say that I want to use <strong>mark</strong> as an argument explicitly by calling <strong>sayHi(mark)</strong> and that the compiler can infer it, but it’s not immediately obvious to the programmer so I don’t think it can be held in the same regard as the advantages of the previous example or the <strong>??</strong> operator. However, what we can write instead is</p><pre>// compiles</pre><pre>mark ?! { sayHi(to: $0) }</pre><p>Which is better, because now I know what’s happening as the programmer. It’s a lot more readable <em>but</em> i’d rather we didn’t have to have the <strong>{ }</strong> at all and could just write</p><pre>// theoretical</pre><pre>mark ?! sayHi(to: $0)</pre><pre>// OR</pre><pre>mark ?! sayHi(to: mark)</pre><p>…which achieves the same purpose and is a lot cleaner than before.</p><p>Now that’s great and all, but I think the real value would be if we have a number of optionals, of different types, that we need to unwrap before using a particular function. I’ve made this rubbish definition that’ll take a tuple of two different (or the same) typed optionals, just for demonstrative purposes. The entire tuple of optionals need to be evaluated and successfully unwrapped before the right-hand side of the expression is executed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc3ec7a5ac32abc5dae72e490529d8a5/href">https://medium.com/media/dc3ec7a5ac32abc5dae72e490529d8a5/href</a></iframe><p>Now I don’t know about you, but I think that looks quite a bit cleaner. Although I appreciate we might not see the advantages fully with only two optional values and a generic <strong>print</strong> function, but what if we had something like:</p><pre>// theoretical</pre><pre>// BEFORE<br>if let mark = mark, _ = thomas, something = something, yo = yo {<br>    doSomething(mark, something, yo)<br>}</pre><pre>// AFTER<br>(mark, thomas, something, yo) ?! doSomething($0, $2, $3)</pre><pre>// OR</pre><pre>(mark, thomas, something, yo) ?! doSomething(mark, something, yo)</pre><p>You can see how it can be a lot more elegant than a traditional <strong>if let</strong> by now while still having perfect readability. The before example actually gets <em>even worse</em> in Swift 3 (or better, depending on your perspective) because you explicitly have to prefix every <strong>x = x</strong> with <strong>let</strong> or <strong>var</strong>. It’s just so needlessly long.</p><p>Though there is a scenario (many actually, this is a thought experiment, remember?) — where we’re doing multiple unwrapping and there is a dependency between the properties — that I can’t really think of how this new operator would tidy up the code:</p><pre>// theoretical</pre><pre>// BEFORE<br>if let urlString = json[“webAddress”].string,<br>    url = NSURL(string: urlString),<br>    target = Target(url: url),<br>    ultimateCoolModel = UltimateCoolModel(some: urlString) {<br>        use(ultimateCoolModel)<br>        useOther(target)<br>}</pre><pre>// AFTER ?<br>json[“webAddress”].string ?! { urlString in <br>    NSURL(string: urlString) <br>    ?! Target(url: $0) <br>    ?! UltimateCoolModel($0) <br>    ?! {<br>        use($0)<br>        userOther(urlString)<br>    }<br>}</pre><p>And then it just looks <strong>way</strong> worse. It starts to look too close to what I was complaining about with Argo Vs SwiftyJSON and it hinders readability in a big way.</p><h4>Closing Comments</h4><p>Anyway, as I said before, this was just me messing around in XCode Playgrounds as a thought exercise to try to get some discussion going on how we can make <strong>if let</strong> even better.</p><p>If you have any suggestions for how we can improve the idea of the <strong>Optionally Do ?!</strong> operator then i’d be glad to hear it. If you don’t like the idea and think it’s rubbish, then that’s cool too, i’d like to hear that also so please leave comments below.</p><blockquote>If you’ve gotten this far and enjoyed the article, please take a second to click the recommend and share buttons below. It would be much appreciated. Cheers — Mark</blockquote><figure><a href="http://jtribe.com.au/?utm_source=Medium&amp;utm_medium=Blog&amp;utm_term=SwiftOptionallyDo&amp;utm_content=ImageFooter&amp;utm_campaign=jtribe%2520Medium%2520CTR"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XN9R_I5VGw0jOgob3MyMHg.png" /></a></figure><h4>About Us</h4><blockquote><em>At </em><a href="http://jtribe.com.au/?utm_source=Medium&amp;utm_medium=Blog&amp;utm_term=SwiftOptionallyDo&amp;utm_content=TextFooter&amp;utm_campaign=jtribe%2520Medium%2520CTR"><strong><em>jtribe</em></strong></a><em>, we proudly craft software solutions for iOS, Android and Web and are passionate about our work. We’ve been working with the iOS and Android platforms since day one, and are one of the most experienced mobile development teams in Australia. We measure success on the impact we have, and with over six-million end users we know our work is meaningful, and this continues to be our driving force.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c12a4fe3882f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jtribe/swift-optionally-do-re-thinking-if-let-c12a4fe3882f">Swift: Optionally Do — Re-thinking If Let</a> was originally published in <a href="https://medium.com/jtribe">jtribe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>