<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Op-Ed on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/categories/op-ed/</link>
    <description>Recent content about iOS development with Swift in Op-Ed  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 21 Nov 2014 01:58:04 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/categories/op-ed/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Agile Blogging</title>
      <link>https://www.andrewcbancroft.com/2014/11/20/agile-blogging/</link>
      <pubDate>Fri, 21 Nov 2014 01:58:04 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/11/20/agile-blogging/</guid>
      <description>&lt;p&gt;After I wrote my post on &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/23/7-reasons-you-should-write-about-swift/&#34; title=&#34;7 Reasons You Should Write About Swift&#34;&gt;7 Reasons You Should Write About Swift&lt;/a&gt;, I began to think about some of the roadblocks that hinder blogging.&lt;/p&gt;
&lt;h3 id=&#34;blog-blockers&#34;&gt;Blog Blockers&lt;/h3&gt;
&lt;p&gt;I started to think about some of my &lt;em&gt;own&lt;/em&gt; reservations about blogging, and boiled it down to a couple of big blockers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Time&lt;/li&gt;
&lt;li&gt;Perfectionism&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;My problems with blogging in the past often came down to the fact that &lt;strong&gt;it takes time&lt;/strong&gt;, and that if was going to publish something, &lt;strong&gt;it had to be perfect&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Of course, every one of us is time-bound, and every one of us has the potential to improve, no matter how &amp;ldquo;perfect” we think we&amp;rsquo;ve made something.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s interesting to me, because these issues come up in software development a &lt;em&gt;lot&lt;/em&gt;, and one of the tools that much of the software development community has embraced are &amp;ldquo;Agile” practices for software development.&lt;/p&gt;
&lt;p&gt;I really enjoy many of aspects of Agile (&lt;a href=&#34;http://www.scrum.org&#34;&gt;Scrum&lt;/a&gt; being the specific flavor of Agile practices that I&amp;rsquo;ve had experience with), particularly as it relates to enabling the rapid delivery of a useful product that satisfies my customers, and the ability to adapt to change and make iterative improvements on that product.&lt;/p&gt;
&lt;h3 id=&#34;agile-_blogging_&#34;&gt;Agile &lt;em&gt;Blogging&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;I began to think, &amp;ldquo;What if some of the things that I enjoy about Agile could be applied to my blogging routine?” How could that positively impact my experience, so as to motivate me to blog more and remove those road blocks that hindered me before?&lt;/p&gt;
&lt;p&gt;If I had to pick two key phrases from what I like about Scrum that could apply to blogging, they&amp;rsquo;d be &amp;ldquo;&lt;strong&gt;rapid delivery of a useful product&lt;/strong&gt;” and &amp;ldquo;&lt;strong&gt;iterative improvements&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Rapid, because what I really want is to produce &lt;em&gt;useful&lt;/em&gt; content &lt;em&gt;quickly&lt;/em&gt;. (Notice I said &amp;ldquo;useful”, not &amp;ldquo;perfect”.) And that&amp;rsquo;s where &amp;ldquo;iterative improvements” comes into play. Since anything I publish will &lt;em&gt;always&lt;/em&gt; have the potential to be improved, it makes better sense for me to prioritize publishing something that has potential to be &lt;em&gt;useful&lt;/em&gt;, with the mindset of cycling back around to improve that publication later over time.&lt;/p&gt;
&lt;h3 id=&#34;getting-practical&#34;&gt;Getting Practical&lt;/h3&gt;
&lt;p&gt;Over the past several months, I&amp;rsquo;ve been working to set blogging goals for myself using Agile principles to help me meet those goals and improve my content over time. Here&amp;rsquo;s how I, personally, get pragmatic with Agile blogging:&lt;/p&gt;
&lt;h4 id=&#34;set-a-goal&#34;&gt;Set a goal&lt;/h4&gt;
&lt;p&gt;Without a goal, I find it very easy to not blog, even though I enjoy it.&lt;/p&gt;
&lt;p&gt;My goal right now is simple but sustainable: I shoot for producing one blog post a week.&lt;/p&gt;
&lt;h4 id=&#34;block-off-time&#34;&gt;Block off time&lt;/h4&gt;
&lt;p&gt;I set aside chunks of time throughout my week to blog. Each time block is about 30 minutes in duration. To produce a post that I&amp;rsquo;m pretty satisfied with (ie, it&amp;rsquo;s useful and contributes something meaningful to my audience), I use about 2 to 3 of those time blocks per week, but almost never all in one sitting.&lt;/p&gt;
&lt;h4 id=&#34;write-publish-improve&#34;&gt;Write, publish, improve&lt;/h4&gt;
&lt;p&gt;My first-priority goal with each post is to get across that main point as clearly as possible. After a quick proofread, I schedule version 1.0 to publish.&lt;/p&gt;
&lt;p&gt;Of course there are going to be mistakes with that initial publish. I almost immediately circle back around and view the live post, noting areas of potential improvement. If I still have time in my 30-minute block, I make the improvements I can and publish them. If I don&amp;rsquo;t, I wait until my next blogging time-block, and make the improvements then.&lt;/p&gt;
&lt;p&gt;Since no post will ever be perfect, I work to make things as useful and meaningful as possible, minimizing error as best I can, of course. Sometimes feedback from my readers helps me find places where I can improve. I value that feedback, and often bump that advice to the top of my priority list for a given post. The point is that I write, publish, then &lt;em&gt;improve iteratively&lt;/em&gt;.&lt;/p&gt;
&lt;h4 id=&#34;examples-and-supplementary-content&#34;&gt;Examples and supplementary content&lt;/h4&gt;
&lt;p&gt;I write a lot about programming, and there are often times where a post could be enhanced with an example. I try my best not to pressure myself to build &lt;em&gt;everything&lt;/em&gt; for version 1.0 of the post. If I think a full-fledged example that someone can compile and run on their own machine would enhance things and provide additional value to my content, I&amp;rsquo;ll usually add that &amp;ldquo;feature” to a post &lt;em&gt;after&lt;/em&gt; the initial version in most cases.&lt;/p&gt;
&lt;p&gt;Doing it this way does a couple of things for me:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It lets me get the main substance of the post out there sooner.&lt;/li&gt;
&lt;li&gt;It provides an opportunity to inform folks on my social media outlets one more time about the post, along with the enhancement.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;Blogging with Agile in mind has been a significant help for my motivation to blog and has served to keep me on track with my one post a week goal. My hope is that by sharing how Agile principles have impacted me, you, too, will be inspired and empowered to start creating great content for us to enjoy! Happy (Agile) blogging!&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/23/7-reasons-you-should-write-about-swift/&#34; title=&#34;7 Reasons You Should Write About Swift&#34;&gt;7 Reasons You Should Write About Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Replace Magic Strings with Enumerations in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/</link>
      <pubDate>Wed, 03 Sep 2014 01:55:54 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/</guid>
      <description>&lt;p&gt;&amp;ldquo;What can I do to avoid these ‘magic strings&amp;rsquo; in my code?” – This was the question I asked myself recently as I found myself in &lt;code&gt;prepareForSegue&lt;/code&gt; comparing &lt;code&gt;segue.segueIdentifier&lt;/code&gt; to in-line hard-coded Strings.  This kind of in-line hard-coding of a String for comparison purposes is what I mean by &amp;ldquo;magic strings” in this article.  I knew this felt like a bad idea, but the solution to a cleaner option wasn&amp;rsquo;t readily apparent to me.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;used&lt;/em&gt; to do things like create static string constants, or &lt;code&gt;#define&lt;/code&gt; expressions so that I could easily change these values if I ever needed to update them for some reason.  You know… back in a former Objective-C developer life when these tactics were available to me.  But these options don&amp;rsquo;t exist in Swift.  What to do??&lt;/p&gt;
&lt;h2 id=&#34;enumerations-to-the-rescue&#34;&gt;Enumerations to the rescue!&lt;/h2&gt;
&lt;p&gt;Specifically, &lt;a title=&#34;Apple Developer Documentation - Enumerations with Raw Values&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_228&#34; target=&#34;_blank&#34;&gt;Enumerations with pre-populated default values (called &lt;em&gt;raw values&lt;/em&gt;)&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By creating an Enumeration that stores raw String values, I was able to encapsulate what would otherwise be &amp;ldquo;magic strings” in a type-safe construct for easier, cleaner use in my code.&lt;/p&gt;
&lt;h2 id=&#34;thegist&#34;&gt;The Gist&lt;/h2&gt;
&lt;p&gt;Consider this fabricated example:&lt;/p&gt;
&lt;p&gt;I have a storyboard with one main View Controller that connects to three other View Controllers through three segues:  &amp;ldquo;otherScreenSegue1”, &amp;ldquo;otherScreenSegue2”, and &amp;ldquo;otherScreenSegue3” as defined in the utilities panel in Xcode.&lt;/p&gt;
&lt;p&gt;An Enumeration encapsulating those segue identifiers might look something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue1&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OtherScreenSegue2&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue2&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OtherScreenSegue3&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue3&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With this Enumeration defined (perhaps in its own .swift file – wherever you deem would be strategic and findable again), the &lt;code&gt;prepareForSegue&lt;/code&gt; override can become &amp;ldquo;magic string”-free:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;prepareForSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIStoryboardSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 2&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 3&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going somewhere else&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Alternatively, if you prefer to compare the enum values themselves, you could do the following (thank you to &lt;a title=&#34;Twitter - Brandon Knope&#34; href=&#34;https://twitter.com/bknope&#34; target=&#34;_blank&#34;&gt;Brandon Knope&lt;/a&gt; for pointing this out – I think it looks even cleaner than my original code above!):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;prepareForSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIStoryboardSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;identifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fromRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;identifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 2&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 3&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going somewhere else&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This strategy of encapsulating my various segue identifiers in an Enumeration provides me a one-stop-shop for reviewing, and if need-be, updating the String values to match what I&amp;rsquo;ve set up in my storyboard.&lt;/p&gt;
&lt;h2 id=&#34;the-details&#34;&gt;The Details&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve chosen an &lt;a title=&#34;Apple Developer Documentation - Enumerations with Raw Values&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_228&#34; target=&#34;_blank&#34;&gt;Enumeration with Raw Values&lt;/a&gt;, because the other two kinds (&lt;a title=&#34;Apple Developer Documentation - Enumerations as Inherent Types&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_224&#34; target=&#34;_blank&#34;&gt;Enumerations as Inherent Types&lt;/a&gt;, or &lt;a title=&#34;Apple Developer Documentation - Enumerations with Associated Values&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_227&#34; target=&#34;_blank&#34;&gt;Enumerations with Associated Values&lt;/a&gt;) don&amp;rsquo;t allow me to do String comparison, or don&amp;rsquo;t allow me to define a value at declaration-time, respectively.&lt;/p&gt;
&lt;p&gt;Notice one critical aspect of the Enumeration:  because default raw values are defined, &lt;em&gt;all&lt;/em&gt; of the raw values must be of the same Type, as explicitly specified in the declaration line:  &lt;code&gt;enum SegueIdentifier: String // All of the enum cases must be Strings&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The next important thing to understand is that in order to do actual comparisons with the raw value itself (see the &lt;code&gt;switch&lt;/code&gt; statement in my code example above), I needed to call &lt;code&gt;toRaw()&lt;/code&gt; on the Enumeration value being used (first code example), or call &lt;code&gt;fromRaw()&lt;/code&gt; to convert the &lt;code&gt;segue.identifier&lt;/code&gt; string to an Enumeration value (second code example):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Enum value of type SegueIdentifier&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// String value, &amp;#34;otherScreen1Segue&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fromRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Unwrapped Enum value of type SegueIdentifier&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In addition to segue identifiers, I&amp;rsquo;m considering using raw value Enumerations to wrap &lt;code&gt;NSNotificationCenter&lt;/code&gt; keys as well.  Share if you find other nice uses of raw value Enumerations!&lt;/p&gt;
&lt;p&gt;So far, this solution has provided me a nice, straight-forward, type-safe way to encapsulate groups of Strings where the urge to fall back to &amp;ldquo;magic strings” would otherwise be high.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Pick a Delegate… Any Delegate… On Clean View Controllers in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/</link>
      <pubDate>Wed, 27 Aug 2014 04:43:29 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/</guid>
      <description>&lt;p&gt;The delegation pattern is ubiquitous in iOS development – the pattern is a &amp;ldquo;&lt;a title=&#34;Cocoa Core Competencies&#34; href=&#34;https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Delegation.html&#34; target=&#34;_blank&#34;&gt;core competency&lt;/a&gt;” for developing in Cocoa, and if you program with the iOS SDK for any length of time and you&amp;rsquo;ll end up writing some code that resembles &lt;code&gt;someInstance.delegate = someDelegate&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;One of the toughest things that I&amp;rsquo;ve experienced is choosing what &lt;code&gt;someDelegate&lt;/code&gt; is.  All too often, a View Controller ends up being assigned the responsibility of being the delegate for &lt;em&gt;everything&lt;/em&gt; in its hierarchy.  My question is:  Is there a cleaner way?&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s pick up on the example I proposed in my &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/&#34; title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34;&gt;recent post about sending e-mails in-app&lt;/a&gt;.  For &amp;ldquo;quick and dirty” pragmatism, I just crammed everything into the View Controller with the promise of coming back and (hopefully) showing a cleaner way.  &lt;a title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift#//acbref-MFMailComposeViewControllerExample&#34; target=&#34;_blank&#34;&gt;Here is a quick link to example posed before&lt;/a&gt; if you&amp;rsquo;d like to review it before proceeding.&lt;/p&gt;
&lt;h2 id=&#34;_what-if8230_&#34;&gt;&lt;em&gt;What if…&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;What if we could make some adjustments so that the View Controller was trimmed down to the example on the &lt;em&gt;right (click for larger view)&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison.png&#34;&gt;&lt;img class=&#34;alignnone wp-image-4321 size-large&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-1024x258.png&#34; alt=&#34;Clean View Controller Comparison&#34; width=&#34;730&#34; height=&#34;183&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-1024x258.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-300x75.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-1200x303.png 1200w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve created a &lt;a title=&#34;Swift Email Composer - GitHub&#34; href=&#34;https://github.com/andrewcbancroft/SwiftEmailComposer&#34; target=&#34;_blank&#34;&gt;fully-working example on GitHub&lt;/a&gt; if you&amp;rsquo;d like to download it and play.&lt;/p&gt;
&lt;p&gt;So the question at hand:  Is the class labeled &amp;ldquo;Clean Example” &lt;em&gt;preferable (&lt;em&gt;ie&lt;/em&gt;, better)&lt;/em&gt;?  First, let&amp;rsquo;s explore how I accomplished the &amp;ldquo;clean” View Controller.  Then I&amp;rsquo;ll tip my hand on and share what I like about this approach…&lt;/p&gt;
&lt;h2 id=&#34;emailcomposer&#34;&gt;EmailComposer&lt;/h2&gt;
&lt;p&gt;In order to accomplish the self-declared Clean View Controller above, I placed all of the configuration processes and the delegate method for the &lt;code&gt;MFMailComposeViewController&lt;/code&gt; in a &lt;em&gt;new&lt;/em&gt; class called &lt;code&gt;EmailComposer&lt;/code&gt;.  It should look familiar if you recall the &lt;a title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift#//acbref-MFMailComposeViewControllerExample&#34; target=&#34;_blank&#34;&gt;previous example&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MessageUI&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;EmailComposer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewControllerDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Did this in order to mitigate needing to import MessageUI in my View Controller&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;canSendMail&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;canSendMail&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;configuredMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mailComposerVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mailComposeDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setToRecipients&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;someone@somewhere.com&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setSubject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Sending you an in-app e-mail...&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setMessageBody&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Sending e-mail in-app is not so bad!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;isHTML&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// &lt;/span&gt;&lt;span class=&#34;cs&#34;&gt;MARK:&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt; MFMailComposeViewControllerDelegate Method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;mailComposeController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishWithResult&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeResult&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dismissViewControllerAnimated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So literally, the only thing I did is&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cut the function definitions for &lt;code&gt;configuredMailComposeViewController&lt;/code&gt;, and the &lt;code&gt;MFMailComposeViewControllerDelegate&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;Paste them into the new &lt;code&gt;EmailComposer&lt;/code&gt;  class, which inherits from &lt;code&gt;NSObject&lt;/code&gt;  (a requirement for this particular delegate protocol&amp;rsquo;s conformity), and conforms to the &lt;code&gt;MFMailComposeViewControllerDelegate&lt;/code&gt;  protocol.&lt;/li&gt;
&lt;li&gt;Adjust my View Controller to create an instance of &lt;code&gt;EmailComposer&lt;/code&gt; , obtain a configured &lt;code&gt;MFMailComposeViewController&lt;/code&gt;, and present it whenever the user taps on a button in my UI.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;conclusions&#34;&gt;Conclusions&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The View Controller in its final version is &lt;em&gt;focused&lt;/em&gt;.  It&amp;rsquo;s primary concern is presentation and handling of user interaction with the View itself, rather than needing to worry with configuring an &lt;code&gt;MFMailComposeViewController&lt;/code&gt; and its delegate callback.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;EmailComposer&lt;/code&gt; is less of a hassle to test, in the sense that &lt;span style=&#34;line-height: 1.5;&#34;&gt;I no longer need to instantiate a View Controller in my &lt;/span&gt;&lt;code&gt;XCTestCase&lt;/code&gt;&lt;span style=&#34;line-height: 1.5;&#34;&gt; class just to test my &lt;/span&gt;&lt;code&gt;MFMailComposeViewController&lt;/code&gt;&lt;span style=&#34;line-height: 1.5;&#34;&gt; stuff&lt;/span&gt;&lt;span style=&#34;line-height: 1.5;&#34;&gt;.  It&amp;rsquo;s a real pain to test an actual View Controller instance, so I like that I can easily create an instance of &lt;code&gt;EmailComposer&lt;/code&gt; and test away without the bulk.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;No need to import MessageUI in my View Controller.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All in all, this is the cleanest, simplest, most balanced solution (that I could think of) to factoring out some logic to another class, so as to make my View Controller as clean as possible.&lt;/p&gt;
&lt;p&gt;The goal was to make sure the appropriate responsibilities are assigned to the right classes.  Presentation logic is all in the View Controller.  Configuration and delegate callback implementation is done in &lt;code&gt;EmailComposer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m thinking through applying this same idea to other more complicated examples (UITableViewDataSource and UITableViewDelegate come to mind), and I think it would do us a &lt;em&gt;lot&lt;/em&gt; of good to strategize on how to avoid making the View Controller the &amp;ldquo;catch-all” delegate / data source class for everything that&amp;rsquo;s currently on the screen_._&lt;/p&gt;
&lt;p&gt;Hopefully these thoughts spark some ideas in the Swift community.  This post has already been revised slightly based on feedback that I&amp;rsquo;ve received from folks on Twitter.  If you have additional ideas in regards to choosing the right delegate, holler my way!  I&amp;rsquo;d love to hear from you.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34;&gt;Clean Coding in Swift – Functions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34;&gt;Clean Coding in Swift – Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34;&gt;Expanded Thoughts on Swift’s Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/&#34;&gt;Send Email In-App – Using MFMailComposeViewController with Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Send Text Message In-App – Using MFMessageComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/10/28/send-text-message-in-app-using-mfmessagecomposeviewcontroller-with-swift/&#34;&gt;Send Text Message In-App – Using MFMessageComposeViewController with Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Expanded Thoughts on Swift’s Type Inference</title>
      <link>https://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/</link>
      <pubDate>Thu, 21 Aug 2014 02:19:45 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/</guid>
      <description>&lt;p&gt;In my recent &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;op-ed on clean coding in Swift focused on Type Inference&lt;/a&gt;, I began by saying,&lt;/p&gt;
&lt;p&gt;Quick!  Tell me!  What is the Type of the &lt;code&gt;birdDetails&lt;/code&gt; constant in this code example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;let birdDetails = birdDetailsFromStorage()&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;With no additional context to glean information from, the correct answer to the question is, &amp;ldquo;I have absolutely no clue…”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But is that concluding assertion &lt;em&gt;true&lt;/em&gt;?  Hmm…&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I&amp;rsquo;m learning&lt;/em&gt;, and as I&amp;rsquo;ve weighed a recent Twitter conversation and thought on a &lt;a title=&#34;Rob Napier Comment&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/#comment-1551252721&#34; target=&#34;_blank&#34;&gt;comment thread that Rob Napier made&lt;/a&gt; on the post quoted above, I&amp;rsquo;m compelled to expand a little on my first post on Type Inference as it relates to clean code in Swift.&lt;/p&gt;
&lt;p&gt;Something struck me as I read &lt;a title=&#34;Rob&#39;s Comment&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/#comment-1551252721&#34; target=&#34;_blank&#34;&gt;Rob&amp;rsquo;s comment&lt;/a&gt;:  Why &lt;em&gt;wouldn&amp;rsquo;t&lt;/em&gt; I know the Type that would be inferred from what is returned by &lt;code&gt;birdDetailsFromStorage()&lt;/code&gt; and assigned to &lt;code&gt;birdDetails&lt;/code&gt;?  Presumably, I named the function what I named it intentionally.  The part I missed, was that if I had &lt;em&gt;designed&lt;/em&gt; well and created a Type called &lt;code&gt;BirdDetails&lt;/code&gt; (say, a Struct as Rob proposed), then all of a sudden, an inference can be made by both the compiler &lt;em&gt;and&lt;/em&gt; a human that the &lt;code&gt;birdDetails&lt;/code&gt; constant is… well… an instance of &lt;code&gt;BirdDetails&lt;/code&gt;.  Imagine that!&lt;/p&gt;
&lt;p&gt;To quote Rob (emphasis added):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #3f4549;&#34;&gt;Type inference should only be used when the result is unambiguous, but &lt;strong&gt;the best solution is to&lt;/strong&gt; &lt;strong&gt;make the result unambiguous&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Bingo.  The &lt;em&gt;best&lt;/em&gt; solution is to &lt;em&gt;make&lt;/em&gt; the result unambiguous.  You and I as code authors are in charge of the clarity or ambiguity of our code – it&amp;rsquo;s up to us to &lt;em&gt;make&lt;/em&gt; the results of our function evaluations unambiguous.&lt;/p&gt;
&lt;p&gt;When I named the function &lt;code&gt;birdDetailsFromStorage()&lt;/code&gt;, I heavily implied something about its return Type in the name.  I even implied it in the name of the &lt;em&gt;constant&lt;/em&gt;.  I was expecting the return Type to be something that encapsulated whatever &amp;ldquo;bird details” are – I just didn&amp;rsquo;t realize it at the time (although it&amp;rsquo;s super obvious now)!&lt;/p&gt;
&lt;p&gt;The very &lt;em&gt;name&lt;/em&gt; of a thing sets expectations for you and the readers of your code.  It&amp;rsquo;s our job to set ourselves up for that expectation to be &lt;em&gt;fulfilled&lt;/em&gt;!  B&lt;span style=&#34;color: #3f4549;&#34;&gt;e predictable with the return Type of your functions for your own sake.  A function signature should be such that when you run across &lt;code&gt;birdDetailsFromStorage()&lt;/code&gt;in some piece of code, you are able to legitimately expect it to return a &lt;code&gt;BirdDetails&lt;/code&gt;.  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&#34;color: #3f4549;&#34;&gt;Using this predictable, &lt;/span&gt;convention-based approach to give humans the right clues about what the compiler is going to compute a Type to be makes Type Inference a totally legitimate language feature to embrace for the sake of your code&amp;rsquo;s clarity and simplicity.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll leave you with a final quote from Rob&amp;rsquo;s comments:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #3f4549;&#34;&gt;Don&amp;rsquo;t go implying that you&amp;rsquo;re returning one thing when you&amp;rsquo;re returning another. If you must, then yes, explicit types are your punishment. 😀&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thank you Rob for your insight and feedback.  You can read more of Rob&amp;rsquo;s work at &lt;a title=&#34;Rob Napier&#39;s Blog&#34; href=&#34;http://robnapier.net&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;http://robnapier.net&#34;&gt;http://robnapier.net&lt;/a&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift – Functions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift – Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Clean Coding in Swift – Type Inference</title>
      <link>https://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/</link>
      <pubDate>Wed, 13 Aug 2014 04:22:25 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/</guid>
      <description>&lt;p&gt;Quick!  Tell me!  What is the Type of the &lt;code&gt;birdDetails&lt;/code&gt;constant in this code example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;birdDetailsFromStorage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With no additional context to glean information from, the correct answer to the question is, &amp;ldquo;I have absolutely no clue…”  &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;&lt;em&gt;Or is it?&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Not fair!”, you say.  &amp;ldquo;In the real world, I&amp;rsquo;d have the ability to option-click and learn the type from a pop-up tooltip, or learn this information by inspection from Xcode&amp;rsquo;s utilities panel.”&lt;/p&gt;
&lt;p&gt;Truth.  We would.  &lt;em&gt;But…&lt;/em&gt; should we &lt;em&gt;have&lt;/em&gt; to for an example like the one above?  Could we have helped ourselves out a bit by being explicit about the type of &lt;code&gt;birdDetails&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;This was the question I asked myself as I set out to determine how and when I prefer to explicitly specify the Types of my variables and constants at declaration-time, rather than letting the compiler &lt;em&gt;infer&lt;/em&gt; the Type for me.&lt;/p&gt;
&lt;p&gt;&lt;a title=&#34;Apple Documentation - Type Inference&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_468&#34; target=&#34;_blank&#34;&gt;Type Inference&lt;/a&gt; is powerful and convenient.  It enables us to leave off explicit type specifications when we declare a variable or constant, leaving us with nice, terse, clean-&lt;em&gt;looking&lt;/em&gt; code.  But in reality, for examples like the one posed above, is clean-&lt;em&gt;looking&lt;/em&gt; truly &amp;ldquo;clean code”?&lt;/p&gt;
&lt;p&gt;From Apple&amp;rsquo;s documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #414141;&#34;&gt;Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The key phrase that stuck out to me was this:  &amp;ldquo;Type inference enables a &lt;strong&gt;&lt;em&gt;compiler&lt;/em&gt;&lt;/strong&gt; to deduce the type of a particular expression…”.  Humans are not compilers!&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t know about you, but I could use all the help I can get when it comes to figuring out simple things like the Type resulting from an expression.  Sure, I could rely on the IDE, but in debugging, or in trying to simply read and understand what the intention of my code is when I come back to it sometime later, I want to focus on the &lt;em&gt;code&lt;/em&gt;, not on pop-up tool tips or inspector panels.&lt;/p&gt;
&lt;p&gt;For instances where deducing the type is not simple for a human to do (and I mean &lt;em&gt;really&lt;/em&gt; simple), I&amp;rsquo;m getting to where I prefer to specify the Type up-front.  We&amp;rsquo;re used to doing this in Objective-C, and I even do it in C# when using &lt;span class=&#34;lang:c# decode:true  crayon-inline &#34;&gt;var&lt;/span&gt; would obscure things.  When a language gives me the option to be clear about Types, I try to take advantage of that valuable language feature for all but the simplest of situations.&lt;/p&gt;
&lt;h3 id=&#34;the-simplest-of-situations&#34;&gt;The Simplest of Situations&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s think for a moment about this so-called &amp;ldquo;simplest of situations”.  I would define such a situation to be when the Type resulting from an expression can easily, at a glance, be inferred by a human being &lt;em&gt;without&lt;/em&gt; assistance from an IDE.  It&amp;rsquo;s all about context here, and for these simplest of situations, I love Type Inference.&lt;/p&gt;
&lt;p&gt;Compare the following two lines of code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Swift&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Swift&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the example just presented, typing &amp;ldquo;&lt;code&gt;: String&lt;/code&gt; ” to explicitly specify &lt;code&gt;birdDetails&lt;/code&gt;‘ type is superfluous in my opinion (and I prefer not to be repetitively redundant when I can).  It&amp;rsquo;s crystal clear that &lt;code&gt;birdDetails&lt;/code&gt; in this example is a &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;However&lt;/em&gt;, in situations like the one at the beginning of the article where, by simply &lt;em&gt;looking,&lt;/em&gt; I would have to answer, &amp;ldquo;I have no clue what the Type of this is”, my preference / proposal for your consideration would be to go ahead and specify the Type at declaration time.  Consider:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;genusSpecies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;commonName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;birdDetailsFromStorage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//... somewhere else in a Swift file far, far away ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;birdDetailsFromStorage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;genusSpecies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;commonName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Apus apus&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Swift&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When I&amp;rsquo;m writing this the first time, I can spend the time looking up the function&amp;rsquo;s return Type and specify it when I declare my constant.  It will make my life so much easier down the road.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t stumble over the fact that &lt;code&gt;birdDetails&lt;/code&gt; is a tuple type &lt;code&gt;(String, String)&lt;/code&gt;.  The point is that the function could have returned &lt;em&gt;anything&lt;/em&gt;, and it would still have been impossible for me to tell you what the Type of the constant was just by looking, had I not specified it upon its declaration.&lt;/p&gt;
&lt;p&gt;Being explicit about the type in the declaration has great potential to immediately help us get our bearings around a particular set of code when we return to it after any length of time.  And it seems to me that one of the principal goals of writing clean code is to help ourselves and our teams make sense of code quicker so that everyone&amp;rsquo;s happier and more productive.&lt;/p&gt;
&lt;p&gt;Whew.  That was a long-winded exploration of some things to think about when relying on (or avoiding) Type Inference in your own code.  Thanks for reading!&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Since publishing this op-ed, I&amp;rsquo;ve &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;expanded my thoughts on Type Inference&lt;/a&gt; as it relates to clean coding practices in Swift.  I highly recommend that this post be read and considered alongside my latest musings,which were heavily influenced by Rob Napier&amp;rsquo;s comments below.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift – Functions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;Expanded Thoughts on Swift&#39;s Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Clean Coding in Swift – Functions</title>
      <link>https://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/</link>
      <pubDate>Thu, 07 Aug 2014 18:16:23 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve been thinking a lot about how the principles of clean coding (Bob Martin&amp;rsquo;s &amp;ldquo;&lt;a title=&#34;Amazon - Clean Code&#34; href=&#34;http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882&#34; target=&#34;_blank&#34;&gt;Clean Code&lt;/a&gt;&amp;rdquo;) apply in Swift. How do I express clean code in this language? Conversely, how do I &lt;em&gt;avoid&lt;/em&gt; writing &lt;em&gt;cryptic&lt;/em&gt; code in Swift? What language features help me write clear and self-explanatory code and what language features present the potential for tempting me to write obscure code in Swift?&lt;/p&gt;
&lt;p&gt;I am beginning a commentary series that I hope will encourage clean coding practice in myself and in the Swift developer community. Dialog on these topics is welcomed – I have not &amp;ldquo;arrived”, so please – help me help the Swift community for the better!&lt;/p&gt;
&lt;p&gt;The first in the series is on writing clean functions in Swift. As it turns out, Swift provides some really great mechanisms for defining self-explanatory, clear-purposed functions. We&amp;rsquo;ll start by analyzing the features around naming functions and their parameters, and will conclude on thinking through function decomposition.&lt;/p&gt;
&lt;h5 id=&#34;function-names&#34;&gt;Function Names&lt;/h5&gt;
&lt;p&gt;Something that I really enjoyed about Objective-C was that while method names were often long and verbose, they were extremely descriptive. My code often read like a narrative, and I enjoyed that. Some may have hated it, but I found it extremely helpful in facilitating my recollection of a method&amp;rsquo;s intended purpose and the expectations around its argument requirements.&lt;/p&gt;
&lt;p&gt;I do my best to think hard about the names that I give my functions, ascribing to them a name that is specific, targeted, and focused on the single &amp;ldquo;thing” that each one does. Nothing in Swift mandates that we leave the verbosity of Objective-C naming conventions behind. In fact, the exact opposite is true!  One example is that swift intentionally provides us with the ability to add external parameter names &lt;em&gt;so that&lt;/em&gt; we can be as descriptive as we need to be about the names of our functions.&lt;/p&gt;
&lt;h5 id=&#34;parameter-names&#34;&gt;Parameter Names&lt;/h5&gt;
&lt;p&gt;&lt;a title=&#34;Apple Developer Documentation - External Parameter Names&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_255&#34; target=&#34;_blank&#34;&gt;Swift&amp;rsquo;s external parameter names&lt;/a&gt;, in my opinion, are wonderful for helping write self-documenting code. They simply help guide my mind back to what a given function does and needs in order to do its job. Anything to help me get back in the zone is worth spending a few extra keystrokes on, especially since Xcode&amp;rsquo;s auto-completion assists me so well when calling functions with long signatures.&lt;/p&gt;
&lt;p&gt;Apple recommends the following:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #414141;&#34;&gt;Consider using external parameter names whenever the purpose of a function’s arguments would be unclear to someone reading your code for the first time.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I would only add that sometimes, that &lt;em&gt;someone&lt;/em&gt; &amp;ldquo;reading your code for the first time” is &lt;em&gt;you&lt;/em&gt;. Not technically, of course, but think about this: Write some code, leave it, and come back to it some time later. Will it make sense? Will you immediately go, &amp;ldquo;Ah, I know what I meant there”, or will you do like I&amp;rsquo;ve often done and say, &amp;ldquo;&lt;em&gt;WHAT&lt;/em&gt; in the &lt;em&gt;WORLD&lt;/em&gt; was I &lt;em&gt;THINKING&lt;/em&gt;??!?”.&lt;/p&gt;
&lt;p&gt;Spending a few seconds on typing a few more words to save brain power and potentially &lt;em&gt;more&lt;/em&gt; seconds/minutes some time later is a worthy investment. And if you work on a team, your teammates will appreciate the extra care you put in to providing as many hints as possible through inventing good names. Good naming includes parameter names. Why &lt;em&gt;not&lt;/em&gt; take advantage of the fact that Swift provides you this opportunity to write self-documenting code?&lt;/p&gt;
&lt;h5 id=&#34;function-decomposition&#34;&gt;Function Decomposition&lt;/h5&gt;
&lt;blockquote&gt;
&lt;p&gt;In order to make sure our functions are doing &amp;ldquo;one thing”, we need to make sure that the statements within our function are all at the same level of abstraction. -Bob Martin, Clean Code pg. 36&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is just general advice without respect to a specific language. A couple of things to think about:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; Watch out for key words like &amp;ldquo;and” / &amp;ldquo;or” in your function names. These red-flag words often indicate that your function is doing more than one thing and can be further decomposed. Consider:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washAndDryCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Logic to wash and dry a car&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A preferred decomposition could look something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Logic to wash a car&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;dryCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Logic to dry a car&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol&gt;
&lt;li&gt; Doing one thing can still involve multiple steps. But if each step takes a few steps of its own, that group of steps can be extracted out into another function. Consider:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Pre-rinse code (example implementation - 4 lines)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Soap code (example implementation - 5 lines)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Rinse code (exmple implementation - 4 lines)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A preferred decomposition might look something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;rinse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;durationInSeconds&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;25.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;soapCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;rinse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;durationInSeconds&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;60.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What&amp;rsquo;s neat about the final result of the decomposition is that not only is my &lt;code&gt;washCar&lt;/code&gt; function shorter and more readable (from 13 lines to 3 lines), but I got code re-use by making the &lt;code&gt;rinse&amp;lt;&lt;/code&gt; function take a duration argument.&lt;/p&gt;
&lt;p&gt;Hopefully these thoughts spark a few ideas in you.  Constructive feedback is welcome!  How are &lt;em&gt;you&lt;/em&gt; thinking about writing clean code in Swift?&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;Expanded Thoughts on Swift&#39;s Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Contributing My Share of Bytes</title>
      <link>https://www.andrewcbancroft.com/2012/06/03/contributing-my-share-of-bytes/</link>
      <pubDate>Mon, 04 Jun 2012 03:16:33 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2012/06/03/contributing-my-share-of-bytes/</guid>
      <description>&lt;p&gt;There is a lot to know. A &lt;em&gt;lot&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I happen to be one of those individuals who gets geeked out very, very often over very, very nerdy things.&lt;/p&gt;
&lt;p&gt;As of late I&amp;rsquo;ve been really wanting an outlet to share what I&amp;rsquo;ve learned / am learn_ing._ The world doesn&amp;rsquo;t need another blog detailing the same things that are already out there – at least not a verbatim copy of information that&amp;rsquo;s available already.&lt;/p&gt;
&lt;p&gt;But the thing I&amp;rsquo;ve figured out is that everybody has a slant on how they glean understanding on something that&amp;rsquo;s unique to them. I can&amp;rsquo;t tell you how many times I&amp;rsquo;ve read multiple blogs full of code or with a perspective on an idea and sort of used the combination of them all to come up with a better understanding on ___ topic that ends up working for me.&lt;/p&gt;
&lt;p&gt;I recently read a tweet from Scott Hanselman&lt;br&gt;
&lt;a href=&#34;https://twitter.com/shanselman/status/208607212594147328&#34;&gt;here&lt;/a&gt; that caused something to just click in my mind: he encouraged blogging rather than emailing. This makes sense – emails and other forms of direct communication have limited potential for helping effect. A blog post and an email with a link to the blog post has much greater potential for impact.&lt;/p&gt;
&lt;p&gt;And then I took that and theorized about what could be possible if I just had an outlet to share what I&amp;rsquo;ve run across, because you never know when you might have the answer someone was looking for – they just needed you to share it in your own unique way that spoke to their particular scheme of processing information.&lt;/p&gt;
&lt;p&gt;So here&amp;rsquo;s to hoping that maybe possibly somehow someday someone will find something of interest somewhere in this blog of mine. For now i foresee posts about some of the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Thoughts on code that I&amp;rsquo;m learning / writing&lt;/li&gt;
&lt;li&gt;Stuff about SQL Server and Business Intelligence&lt;/li&gt;
&lt;li&gt;Discoveries from other realms of study&lt;/li&gt;
&lt;li&gt;Tutorials on things I&amp;rsquo;ve figured out and think might help someone someday&lt;/li&gt;
&lt;li&gt;Thoughts / reactions / responses to news&lt;/li&gt;
&lt;li&gt;Who knows what else…&lt;/li&gt;
&lt;/ol&gt;
</description>
    </item>
    
  </channel>
</rss>