<?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 Christian Petersen on Medium]]></title>
        <description><![CDATA[Stories by Christian Petersen on Medium]]></description>
        <link>https://medium.com/@cbp?source=rss-5b711761a18d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j6DkccnUTZ6KNSGy0-tEyQ@2x.png</url>
            <title>Stories by Christian Petersen on Medium</title>
            <link>https://medium.com/@cbp?source=rss-5b711761a18d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 21:52:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cbp/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 2: Improving guards]]></title>
            <link>https://medium.com/@cbp/swift-2-improving-guards-8a84c709df9a?source=rss-5b711761a18d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a84c709df9a</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[os-x]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Christian Petersen]]></dc:creator>
            <pubDate>Tue, 18 Aug 2015 00:25:27 GMT</pubDate>
            <atom:updated>2015-08-18T17:18:04.853Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the many exciting announcements at WWDC for the new version of Swift were <strong>guards</strong>. They make it less cumbersome to check for <strong>Optional</strong> types, and keeps your code more concise.</p><p>Guards lets you turn this</p><pre>func search(string: String?) -&gt; Int? {<br>  var result: Int?</pre><pre>  if let match = self.indexOf(string) {<br>    result = match<br>  }</pre><pre>  return result<br>}</pre><p>Into this</p><pre>func search(string: String?) -&gt; Int? {<br>  guard let match = self.indexOf(string) else {<br>    return nil<br>  }</pre><pre>  return match<br>}</pre><p>That saves a bit of keystrokes and a lot of headaches — especially with more than just a couple of Optionals.</p><p>However, I noted quite quickly an unnecessary part of guards, which would otherwise have made it even more concise and trivial.</p><p>Specifically the part of explicitly returning <strong>nil</strong>. It is well-known fact that Optionals can hold either a value or nil, so when the return type of a function conforms to an Optional it would make much sense that a guard clause should return nil by default.</p><pre>guard let match = self.indexOf(string)<br>return match</pre><p>This saves a great amount of characters, and also makes your a lot cleaner — a few of those wouldn’t hurt as much.</p><p>But what about functions, that does return Optionals? Take this example</p><pre>func getRange(s: String) -&gt; Int {<br>  guard let range = self.rangeOfString(s)<br>  where range.count &gt; 0 else {<br>    return 0<br>  }</pre><pre>  return range.count<br>}</pre><p>Here we could get rid of the brackets</p><pre>guard let range = self.rangeOfString(s)<br>  where range.count &gt; 0 else return 0</pre><p>Not a huge change, but enough to make it worth it — and a bit more verbal too.</p><p>While we are at this topic, there’s a lot of improvements I’d like to see in Swift to save a few keystrokes and still keep the code readable and coherent. This would definitely make it a lot more fun to write Swift code, without having to move your hands in weird positions for trivial operations like this.</p><p>What do you think about this? Would it benefit you, or don’t you mind the extra keystrokes? I’d love to hear your thoughts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a84c709df9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>