<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[miqu.me]]></title>
  <link href="http://miqu.me/atom.xml" rel="self"/>
  <link href="http://miqu.me/"/>
  <updated>2018-11-21T10:05:53+00:00</updated>
  <id>http://miqu.me/</id>
  <author>
    <name><![CDATA[Miguel Angel Quiñones]]></name>
    <email><![CDATA[miguel@miqu.me]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[My top 5 must-have technical books]]></title>
    <link href="http://miqu.me/blog/2018/01/28/top-5-must-have-technical-books/"/>
    <updated>2018-01-28T19:06:09+00:00</updated>
    <id>http://miqu.me/blog/2018/01/28/top-5-must-have-technical-books</id>
    <content type="html"><![CDATA[<p>Quite recently a team member wanted to suggest interesting technical books to read for a junior engineer. I figured I might as well write my top 5
books here, in no particular order. I believe every Software Engineer should own these books, but reading them is a very good start ;)</p>

<!-- more -->


<h1>The Pragmatic Programmer: From journeyman to master</h1>

<p>If you have to read just one book, specially when starting to write software, it should be <a href="https://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X">The Pragmatic Programmer</a>. It covers ideas, always
focused on self-improvement, mastery of the craft, and professinalism. I see this book as an extended list of ideas and suggestions for self-improvement,
similar to the <a href="http://www.starling-software.com/employment/programmer-competency-matrix.html">programmer competency matrix</a>. While reading it, it&rsquo;s better to keep growing a list of topics to investigate
further, as the book covers wildly different practices, ideas, and techniques.</p>

<h1>The Art of Unit Testing</h1>

<p><a href="https://www.amazon.com/Art-Unit-Testing-examples/dp/1617290890">This book</a> opened my eyes into the world of unit testing, from a practical (not philosophical) point of view. It introduced me to practical
techniques to use in unit testing. Most imporptantly, it helps understand the crucial understanding of the necessity of maintainable test code, and &lsquo;good&rsquo; tests. I think it&rsquo;s the best introductory book on unit testing. Period.</p>

<h1>Seven Languages in Seven Weeks</h1>

<p>More than a book, <a href="https://www.amazon.com/Seven-Languages-Weeks-Programming-Programmers/dp/193435659X">&ldquo;Seven Languages in Seven Weeks&rdquo;</a> is an exploration into wildly different concepts across programming languages.
It&rsquo;s a book about paradigms, and crucially, sparks the curiosity of why these exist at all. The way that the concepts
are introduced, focusing on the reasons, advantages, disadvantages and design decisions of a language were very insightful for me. If you don&rsquo;t try
the proposed challenges the book still will give you a lot of material to think about and to take ideas from.</p>

<h1>Domain-Driven Design</h1>

<p>Most software projects are about tackling complexity, and I must say many projects fail in this regards. <a href="https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">The book</a> taught me how to think about
the act of writing code in a more abstracted way, as a modelling problem. Eric Evans introduces many concepts that in
my opinion should be as ubiquitous as the OO Design patterns of the <a href="https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612">Gang of Four book</a>.</p>

<h1>The Algorithm Design Manual</h1>

<p>I prefer <a href="https://www.amazon.com/Algorithm-Design-Manual-Steven-Skiena/dp/1848000693">&ldquo;The Algorithm Design Manual&rdquo;</a> over the canonical <a href="https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844">&ldquo;Introduction to Algorithms&rdquo;</a>, because of the &lsquo;Catalog&rsquo; section. The section focuses on problems first, and acts as an index to get ideas for solutions. This section alone is very helpful, not only for practical reasons, but also to understand motivations and see the breath
of problems tackled by algorithms.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fixing autocompletion on mixed Objective-C and Swift projects]]></title>
    <link href="http://miqu.me/blog/2017/06/16/fixing-autocompletion-on-mixed-objective-c-and-swift-projects/"/>
    <updated>2017-06-16T22:55:27+00:00</updated>
    <id>http://miqu.me/blog/2017/06/16/fixing-autocompletion-on-mixed-objective-c-and-swift-projects</id>
    <content type="html"><![CDATA[<p>This year I had the privilege of attending WWDC for the first time. I knew the labs were very important, and I want to share a resolution to fix a problem for our big project at <a href="http://peak.net">Peak</a>: Autocompletion on Swift code was not working most of the time in the IDE, and with the help of an Apple engineer in the labs we got it sorted.</p>

<p><strong>Update on 30/10/2017:</strong></p>

<p><em>The issues described here seem to be fixed with latest release version of Xcode 9.0.1 (9A1004). Also the &lsquo;internal&rsquo; menu doesn&rsquo;t seem to be accessible anymore. If you know how to access it I&rsquo;d love to hear from you!</em></p>

<!-- more -->


<h1>The issue</h1>

<p>If you are using Xcode 8 or Xcode 9 (beta 1 as of this writing), when you have a project with mixed Objective-C and Swift, you might encounters problems with autocompletion inside the Swift code.</p>

<p>We&rsquo;ve had this intermitently and most recent IDE versions made work very difficult as we would not have any autocompletion for our Swift code, or it would work intermitently.</p>

<p>Our gut feeling was that the bridging header had some classes that were causing SourceKit to fail, but we didn&rsquo;t know how to debug the issue nor provide good feedback for Apple to fix it. Our chance came while visiting the labs at the conference, and a very helpful and patient Apple engineer worked with our project to understand what was wrong.</p>

<h1>Bridging header imports</h1>

<p>It turns out that if you import classes from libraries (in our case using CocoaPods) omitting the path may result in Sourcekit unable to index properly.</p>

<p>For example, a class in some library:</p>

<p><code>#import "MyClass.h"</code></p>

<p>Code will compile fine but your code completion in Swift side might break because the indexer fails when encountering this file. I&rsquo;m still unsure if it&rsquo;s because of importing the file somewhere else differently or if it&rsquo;s just a bug in the indexer. Anyway, the way to work around it (Apple is supposedly aware of this) is to import with the framework or library style, including the path:</p>

<p><code>#import "path-to-library/MyClass.h"</code></p>

<h1>How to debug</h1>

<p>The engineer kindly made a wrote up for us the steps to be able to debug this in the future. He also shared how to start Xcode in debug mode. I knew that would be possible, but didn&rsquo;t know how to do it before.</p>

<p>To open Xcode with debug menu, open it from command line with the argument:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>/Applications/Xcode.app/Contents/MacOS/Xcode -ShowDVTDebugMenu YES
</span></code></pre></td></tr></table></div></figure>


<p>There&rsquo;s many debugging entries enabled! For our exact problem we&rsquo;re interested in Sourcekit, and the menu entry is i &ldquo;Xcode -> Internal -> SourceKit -> Service Log&rdquo;.</p>

<p>To investigate the code completion problem:</p>

<ul>
<li>Invoke code-completion, then close the log window</li>
<li>Open /tmp/sourcekit-service-log.txt</li>
<li>Search for &ldquo;key.request: source.request.codecomplete&rdquo;</li>
<li>Get the compiler arguments from &ldquo;key.compilerargs:&rdquo; and create a swift invocation with them:

<ul>
<li>Add &ldquo;DEVELOPER_DIR=/Applications/Xcode.app xcrun swift -frontend &rdquo; in front of the arguments</li>
<li>Add &ldquo;-primary-file&rdquo; for the file mentioned in &ldquo;key.sourcefile:&rdquo;</li>
<li>Remove &ldquo;-Xfrontend&rdquo; flags</li>
<li>Remove &ldquo;-j<N>&rdquo; flags</li>
</ul>
</li>
</ul>


<p>Juggling the arguments is easier to handle if you copy all the argument list to a file and make it executable by bash, as you would run the script in the file instead of pasting arguments into command line.</p>

<p>Here&rsquo;s example of how the command would look like, for a file &ldquo;/project/path/myfile.swift&rdquo;. I&rsquo;m omitting whole argument list, just ilustrating how it would look like.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">DEVELOPER_DIR</span><span class="o">=</span>/Applications/Xcode.app xcrun swift -frontend -primary-file <span class="s2">&quot;/project/path/myfile.swift&quot;</span> <span class="s2">&quot;-module-name&quot;</span> <span class="s2">&quot;Peak&quot;</span> <span class="s2">&quot;-Onone&quot;</span> <span class="s2">&quot;-DDEBUG&quot;</span> <span class="s2">&quot;-sdk&quot;</span> <span class="s2">&quot;/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk&quot;</span> <span class="s2">&quot;-target&quot;</span> <span class="s2">&quot;arm64-apple-ios8.0&quot;</span> &lt;many more&gt;
</span></code></pre></td></tr></table></div></figure>


<p>After running the command, you&rsquo;ll see errors like this one:
<em>/project/path/My-Bridging-Header.h:10:9: note: in file included from /project/path/My-Bridging-Header.h:10:9: #import &ldquo;MyClass.h&rdquo;</em></p>

<p>You now know the headers that need fixing and now can change in the briding header.</p>

<h1>Conclusion</h1>

<p>We got a functional IDE once more! Apple is very helpful at the WWDC and it is very good for these and many other questions to be answered or investigated. If you attend the conference anytime, be sure to bring your questions, code, and issues to talk directly with Apple engineers.</p>

<p>I hope this helps anybody that is asking &lsquo;why my autocompletion in Swift broke in Xcode&rsquo;. This might be cause of the issue. The debug menu can be helpful to poke into the internals of the IDE, or just to help Apple fix bugs by providing more detailed logs.</p>

<p>Happy bug hunting!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Automatic bridging from Swift to Objective-C using Sourcery]]></title>
    <link href="http://miqu.me/blog/2017/05/21/automatic-bridging-from-swift-to-objective-c-using-sourcery/"/>
    <updated>2017-05-21T21:30:00+00:00</updated>
    <id>http://miqu.me/blog/2017/05/21/automatic-bridging-from-swift-to-objective-c-using-sourcery</id>
    <content type="html"><![CDATA[<p>I recently read a <a href="http://blog.benjamin-encz.de/post/bridging-swift-types-to-objective-c/">great post</a> by <a href="https://twitter.com/benjaminencz">Benjamin Encz</a> about bridging Swift to Objective-C by creating bridging types that can be exposed to the older language. I wondered if his idea can be improved and automated using meta-programming with <a href="https://github.com/krzysztofzablocki/Sourcery">Sourcery</a> by <a href="https://twitter.com/merowing_">Krzystof Zablocki</a>. The answer is a very impressive ObjC-like uppercase <strong>YES</strong>.</p>

<!-- more -->


<p>First of all, you should read <a href="http://blog.benjamin-encz.de/post/bridging-swift-types-to-objective-c/">Benjamin&rsquo;s post</a>. He proposes to use a bridging class that can be instantiated from Objective-C and Swift because it inherits from <code>NSObject</code>. The bridging class has properties to get and set the properties of the bridged type.</p>

<p>In this post I&rsquo;m just going to give an overview of the template and highlight the simpler parts. If you want to jump directly in the code, here&rsquo;s the <a href="https://github.com/DarthMike/AutoObjCBridgeable">project</a>, and the <a href="https://github.com/DarthMike/AutoObjCBridgeable/blob/master/Templates/AutoObjCBridgeable.stencil">template</a>. The example template bridges struct and enum, but doesn&rsquo;t handle custom protocols to keep it as simple as possible.</p>

<h2>About Sourcery</h2>

<p><a href="https://github.com/krzysztofzablocki/Sourcery">Sourcery</a> is a tool to generate code, using your written templates, using metadata from your production code. In other words, it&rsquo;s a tool to do <a href="https://en.wikipedia.org/wiki/Metaprogramming">metaprogramming</a> in Swift. If you are not familiar with Sourcery I encourage you to go read the documentation and at least understand the simple examples like generating <code>Equatable</code> implementations for your types. Getting familiar with <a href="https://github.com/kylef/Stencil">Stencil</a>, one of the supported template languages is also fundamental.</p>

<h2>Building the template</h2>

<p>We&rsquo;ll need to generate a new class for every struct and enum that we bridge. In order to opt-in to the feature, we use <a href="https://www.objc.io/blog/2014/12/29/functional-snippet-13-phantom-types/">phantom types</a> in order to mark swift types (our custom structs or enums) that can be bridged to Objective-C. Our template will use this protocol to which types to introspect:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">protocol</span> <span class="n">AutoObjCBridgeable</span> <span class="p">{}</span>
</span></code></pre></td></tr></table></div></figure>


<p>For example, take Benjamin&rsquo;s example code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">enum</span> <span class="n">CheckoutOption</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">creditCard</span><span class="p">(</span><span class="nl">given</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">paypal</span><span class="p">(</span><span class="n">String</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">ShoppingCart</span>  <span class="p">{</span>
</span><span class='line'>    <span class="k">var</span> <span class="nl">items</span><span class="p">:</span> <span class="p">[</span><span class="n">String</span><span class="p">]</span>
</span><span class='line'>    <span class="k">var</span> <span class="nl">checkoutOption</span><span class="p">:</span> <span class="n">CheckoutOption</span><span class="o">?</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">items</span><span class="p">:</span> <span class="p">[</span><span class="n">String</span><span class="p">],</span> <span class="nl">checkoutOption</span><span class="p">:</span> <span class="n">CheckoutOption</span><span class="o">?</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">checkoutOption</span> <span class="o">=</span> <span class="n">checkoutOption</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>We just need to adopt the protocol in an extension for the types, in order to opt-in to code generation with Sourcery:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">extension</span> <span class="nl">ShoppingCart</span> <span class="p">:</span> <span class="n">AutoObjCBridgeable</span> <span class="p">{}</span>
</span><span class='line'><span class="k">extension</span> <span class="nl">CheckoutOption</span> <span class="p">:</span> <span class="n">AutoObjCBridgeable</span> <span class="p">{}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Structs vs enums</h3>

<p>Structs are straightforward to bridge, as we only need to handle properties. On the other hand, enums are much more involved because we need to take care of handling an arbitrary number of associated values. Fear not, as Sourcery provides all the necessary metadata for our nefarious needs!</p>

<p>We will divide the template in two parts: one loop for structs, one for enums.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// Bridging for structs</span>
</span><span class='line'><span class="p">{</span><span class="o">%</span> <span class="k">for</span> <span class="nx">type</span> <span class="k">in</span> <span class="nx">types</span><span class="p">.</span><span class="nx">implementing</span><span class="p">.</span><span class="nx">AutoObjCBridgeable</span><span class="o">|</span><span class="nx">struct</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'><span class="p">{</span><span class="o">%</span> <span class="nx">endfor</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Bridging of enums</span>
</span><span class='line'><span class="p">{</span><span class="o">%</span> <span class="k">for</span> <span class="kr">enum</span> <span class="k">in</span> <span class="nx">types</span><span class="p">.</span><span class="nx">implementing</span><span class="p">.</span><span class="nx">AutoObjCBridgeable</span><span class="o">|</span><span class="kr">enum</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'><span class="p">{</span><span class="o">%</span> <span class="nx">endfor</span> <span class="o">%</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>For structs, we only generate one class that contains the Swift native type. We wrap the type with API to set and get properties, following Benjamin&rsquo;s design. Enumerations will require more classes (see below).</p>

<p>Also bear in mind that when bridging properties in either an enum or a struct, we need to handle differently types that are bridged by ourselves (our custom struct and enums).</p>

<h3>Bridging an empty struct</h3>

<p>This is how we start generating classes for every bridgeable struct:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">{</span><span class="o">%</span> <span class="k">for</span> <span class="nx">type</span> <span class="k">in</span> <span class="nx">types</span><span class="p">.</span><span class="nx">implementing</span><span class="p">.</span><span class="nx">AutoObjCBridgeable</span><span class="o">|</span><span class="nx">struct</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'><span class="err">@</span><span class="nx">objc</span><span class="p">(</span><span class="nx">XYZ</span><span class="p">{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="p">}})</span>
</span><span class='line'><span class="kr">class</span> <span class="nx">_ObjC</span><span class="p">{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="p">}}</span> <span class="o">:</span> <span class="nx">NSObject</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// TODO</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="p">{</span><span class="o">%</span> <span class="nx">endfor</span> <span class="o">%</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Given a simple, empty struct:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">struct</span> <span class="nl">EmptyStruct</span> <span class="p">:</span> <span class="n">AutoObjCBridgeable</span> <span class="p">{</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Let&rsquo;s look at what a the resulting bridging code for the simplest struct would look like; An empty struct should only wrap the type in an Objective-C class, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="p">@</span><span class="n">objc</span><span class="p">(</span><span class="n">XYZEmptyStruct</span><span class="p">)</span>
</span><span class='line'><span class="k">class</span> <span class="nl">_ObjCEmptyStruct</span> <span class="p">:</span> <span class="bp">NSObject</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">private</span> <span class="p">(</span><span class="kr">set</span><span class="p">)</span> <span class="k">var</span> <span class="nl">emptyStruct</span><span class="p">:</span> <span class="n">EmptyStruct</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Initializer to be used from Swift code</span>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">emptyStruct</span><span class="p">:</span> <span class="n">EmptyStruct</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">emptyStruct</span> <span class="o">=</span> <span class="n">emptyStruct</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Initializer to be used from ObjC code</span>
</span><span class='line'>    <span class="kr">override</span> <span class="k">init</span><span class="p">(){</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">emptyStruct</span> <span class="o">=</span> <span class="n">EmptyStruct</span><span class="p">()</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now for a more useful type, say a struct with some values, like this one:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">struct</span> <span class="nl">StructWithSwiftProperties</span> <span class="p">:</span> <span class="n">AutoObjCBridgeable</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">let</span> <span class="nl">name</span><span class="p">:</span> <span class="n">String</span>
</span><span class='line'>    <span class="k">let</span> <span class="nl">count</span><span class="p">:</span> <span class="n">Int</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The bridging code looks like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="p">@</span><span class="n">objc</span><span class="p">(</span><span class="n">XYZStructWithSwiftProperties</span><span class="p">)</span>
</span><span class='line'><span class="k">class</span> <span class="nl">_ObjCStructWithSwiftProperties</span> <span class="p">:</span> <span class="bp">NSObject</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">private</span> <span class="p">(</span><span class="kr">set</span><span class="p">)</span> <span class="k">var</span> <span class="nl">structWithSwiftProperties</span><span class="p">:</span> <span class="n">StructWithSwiftProperties</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Initializer to be used from Swift code</span>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">structWithSwiftProperties</span><span class="p">:</span> <span class="n">StructWithSwiftProperties</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">structWithSwiftProperties</span> <span class="o">=</span> <span class="n">structWithSwiftProperties</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Initializer to be used from ObjC code</span>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span>
</span><span class='line'>        <span class="nl">name</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span>
</span><span class='line'>        <span class="nl">count</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span>
</span><span class='line'>        <span class="nl">valid</span><span class="p">:</span> <span class="n">Bool</span><span class="p">,</span>
</span><span class='line'>        <span class="nl">mutableValid</span><span class="p">:</span> <span class="n">Bool</span><span class="p">,</span>
</span><span class='line'>        <span class="nl">someStuff</span><span class="p">:</span> <span class="p">[</span><span class="n">String</span><span class="p">]</span>
</span><span class='line'>    <span class="p">){</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">structWithSwiftProperties</span> <span class="o">=</span> <span class="n">StructWithSwiftProperties</span><span class="p">(</span>
</span><span class='line'>                <span class="nl">name</span><span class="p">:</span> <span class="n">name</span><span class="p">,</span>
</span><span class='line'>                <span class="nl">count</span><span class="p">:</span> <span class="n">count</span><span class="p">,</span>
</span><span class='line'>                <span class="nl">valid</span><span class="p">:</span> <span class="n">valid</span><span class="p">,</span>
</span><span class='line'>                <span class="nl">mutableValid</span><span class="p">:</span> <span class="n">mutableValid</span><span class="p">,</span>
</span><span class='line'>                <span class="nl">someStuff</span><span class="p">:</span> <span class="n">someStuff</span>
</span><span class='line'>            <span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">var</span> <span class="nl">name</span> <span class="p">:</span> <span class="n">String</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nb">self</span><span class="p">.</span><span class="n">structWithSwiftProperties</span><span class="p">.</span><span class="n">name</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">var</span> <span class="nl">count</span> <span class="p">:</span> <span class="n">Int</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nb">self</span><span class="p">.</span><span class="n">structWithSwiftProperties</span><span class="p">.</span><span class="n">count</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The generated code will need to provide read-only properties for the inmutable properties, and read-write properties for the mutable ones. This example only contains simple types, but not other structs or enumerations. Generating those will require to nest bridged classes. You can check the <a href="https://github.com/DarthMike/AutoObjCBridgeable/blob/master/Templates/AutoObjCBridgeable.stencil">template code</a> by yourself for the rest of it.</p>

<p>Generating properties for standard Swift types is simple:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">{</span><span class="o">%</span> <span class="k">if</span> <span class="nx">variable</span><span class="p">.</span><span class="nx">type</span><span class="p">.</span><span class="nx">kind</span> <span class="o">!=</span> <span class="s2">&quot;struct&quot;</span> <span class="nx">and</span> <span class="nx">variable</span><span class="p">.</span><span class="nx">type</span><span class="p">.</span><span class="nx">kind</span> <span class="o">!=</span> <span class="s2">&quot;enum&quot;</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>    <span class="c1">// Forwarding property for native types</span>
</span><span class='line'>    <span class="kd">var</span> <span class="p">{{</span><span class="nx">variable</span><span class="p">.</span><span class="nx">name</span><span class="p">}}</span> <span class="o">:</span> <span class="p">{{</span><span class="nx">variable</span><span class="p">.</span><span class="nx">typeName</span><span class="p">}}</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nx">get</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="nx">self</span><span class="p">.{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="o">|</span><span class="nx">lowerFirstWord</span><span class="p">}}.{{</span><span class="nx">variable</span><span class="p">.</span><span class="nx">name</span><span class="p">}}</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="p">{</span><span class="o">%</span> <span class="k">if</span> <span class="nx">variable</span><span class="p">.</span><span class="nx">isMutable</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>        <span class="nx">set</span> <span class="p">{</span>
</span><span class='line'>            <span class="nx">self</span><span class="p">.{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="o">|</span><span class="nx">lowerFirstWord</span><span class="p">}}.{{</span><span class="nx">variable</span><span class="p">.</span><span class="nx">name</span><span class="p">}}</span> <span class="o">=</span> <span class="nx">newValue</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="p">{</span><span class="o">%</span> <span class="nx">endif</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">{</span><span class="o">%</span> <span class="nx">endif</span> <span class="o">%</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Generating the swift initializer and the wrapped property is also simple:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kr">private</span> <span class="p">(</span><span class="nx">set</span><span class="p">)</span> <span class="kd">var</span> <span class="p">{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="o">|</span><span class="nx">lowerFirstWord</span><span class="p">}}</span><span class="o">:</span> <span class="p">{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="p">}}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Initializer to be used from Swift code</span>
</span><span class='line'><span class="nx">init</span><span class="p">({{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="o">|</span><span class="nx">lowerFirstWord</span><span class="p">}}</span><span class="o">:</span> <span class="p">{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="p">}})</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">self</span><span class="p">.{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="o">|</span><span class="nx">lowerFirstWord</span><span class="p">}}</span> <span class="o">=</span> <span class="p">{{</span><span class="nx">type</span><span class="p">.</span><span class="nx">name</span><span class="o">|</span><span class="nx">lowerFirstWord</span><span class="p">}}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Generating the initializer from Objective-C will require to handle structs and enumerations specially, so you can check it yourself in the <a href="https://github.com/DarthMike/AutoObjCBridgeable/blob/master/Templates/AutoObjCBridgeable.stencil">template</a>.</p>

<h3>Bridging simple enumerations</h3>

<p>The template code to bridge enumerations is a bit more complex. We&rsquo;re just going to review what the generated code would look like in this case. Take a simple example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">enum</span> <span class="nl">OtherEnum</span> <span class="p">:</span> <span class="n">AutoObjCBridgeable</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">a</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">b</span><span class="p">(</span><span class="n">EmptyStruct</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The generated bridging code should contain a class for the enum, and separate classes for every case. We initialize from Objective-C casting from <code>Any</code> to the concrete class corresponding to every case. Take note we&rsquo;ll need to handle associated values and because of this the template code is a bit messy.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="p">@</span><span class="n">objc</span><span class="p">(</span><span class="n">XYZOtherEnum</span><span class="p">)</span>
</span><span class='line'><span class="k">class</span> <span class="nl">_ObjCOtherEnum</span> <span class="p">:</span> <span class="bp">NSObject</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">private</span> <span class="p">(</span><span class="kr">set</span><span class="p">)</span> <span class="k">var</span> <span class="nl">otherEnum</span><span class="p">:</span> <span class="n">OtherEnum</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Initializer for Swift code</span>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">value</span><span class="p">:</span> <span class="n">OtherEnum</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">otherEnum</span> <span class="o">=</span> <span class="n">value</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Initializer for Objective-C code</span>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">caseValue</span><span class="p">:</span> <span class="n">Any</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="k">let</span> <span class="n">_</span> <span class="o">=</span> <span class="n">caseValue</span> <span class="kt">as</span><span class="o">?</span> <span class="n">_ObjCOtherEnumA</span> <span class="p">{</span>
</span><span class='line'>            <span class="nb">self</span><span class="p">.</span><span class="n">otherEnum</span> <span class="o">=</span> <span class="p">.</span><span class="n">a</span>
</span><span class='line'>        <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="k">let</span> <span class="n">caseValue</span> <span class="o">=</span> <span class="n">caseValue</span> <span class="kt">as</span><span class="o">?</span> <span class="n">_ObjCOtherEnumB</span> <span class="p">{</span>
</span><span class='line'>            <span class="nb">self</span><span class="p">.</span><span class="n">otherEnum</span> <span class="o">=</span> <span class="p">.</span><span class="n">b</span><span class="p">(</span><span class="n">caseValue</span><span class="p">.</span><span class="n">value1</span><span class="p">.</span><span class="n">emptyStruct</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>            <span class="n">preconditionFailure</span><span class="p">(</span><span class="s">&quot;Value \(caseValue) is not compatible with cases of OtherEnum&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="c1">// A case of OtherEnum</span>
</span><span class='line'><span class="p">@</span><span class="n">objc</span><span class="p">(</span><span class="n">XYZOtherEnumA</span><span class="p">)</span>
</span><span class='line'><span class="k">class</span> <span class="nl">_ObjCOtherEnumA</span> <span class="p">:</span> <span class="bp">NSObject</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// A case of OtherEnum</span>
</span><span class='line'><span class="p">@</span><span class="n">objc</span><span class="p">(</span><span class="n">XYZOtherEnumB</span><span class="p">)</span>
</span><span class='line'><span class="k">class</span> <span class="nl">_ObjCOtherEnumB</span> <span class="p">:</span> <span class="bp">NSObject</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">let</span> <span class="nl">value1</span> <span class="p">:</span> <span class="n">_ObjCEmptyStruct</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">value1</span> <span class="p">:</span> <span class="n">_ObjCEmptyStruct</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">value1</span> <span class="o">=</span> <span class="n">value1</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Conclusion</h2>

<p>Automating the generation of complex things like bridging is possible with Sourcery. The template I&rsquo;ve written doesn&rsquo;t handle all cases of possible code; For example, the template doesn&rsquo;t handle private properties, doesn&rsquo;t take default values for initializers, we don&rsquo;t forward methods, and it doesn&rsquo;t handle properly structs as associated values of enums. The template also doesn&rsquo;t bridge protocols.</p>

<p>I stopped here on purpose, as this generated code should be temporary until a migration eventually happens. In general, interaction from Objective-C to Swift should be kept to a minimum.</p>

<p>I&rsquo;m very surprised that such complexity can be automated using Sourcery, and I really love the concept of the tool. If you never used it, it&rsquo;s a very useful addition for your Swift toolbelt.</p>

<p>If you have any feedback, please open an issue in the <a href="https://github.com/DarthMike/AutoObjCBridgeable">example repository</a>, or reach me on <a href="https://twitter.com/miguelquinon">twitter</a>. I&rsquo;d love to hear your thoughts!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Build settings depending on Xcode version]]></title>
    <link href="http://miqu.me/blog/2017/01/29/temporary-build-settings/"/>
    <updated>2017-01-29T23:26:54+00:00</updated>
    <id>http://miqu.me/blog/2017/01/29/temporary-build-settings</id>
    <content type="html"><![CDATA[<p>This weekend I wanted to setup <a href="https://swift.org/blog/bridging-pch/">Precompiled Bridging Headers</a> for my project. This setting is available in the new Xcode 8.3 beta.  Then building for latest official XCode (8.2.1) the code will not compile because the Swift compiler doesn&rsquo;t recognize it. How do we use different build settings for different Xcode versions? <strong>Only possible with xcconfig files</strong>.</p>

<p>There&rsquo;s an undocumented build setting called <code>XCODE_VERSION_MINOR</code> that we can use here. And after some variable substitution we&rsquo;ll make it work. Here&rsquo;s the gist:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>// Setting <span class="k">for</span> last Xcode
</span><span class='line'><span class="nv">OTHER_SWIFT_FLAGS_XCODE_0820</span> <span class="o">=</span> //&lt;previous flags, <span class="k">if</span> any&gt;
</span><span class='line'>// Setting <span class="k">for</span> Xcode Beta
</span><span class='line'><span class="nv">OTHER_SWIFT_FLAGS_XCODE_0830</span> <span class="o">=</span> -enable-bridging-pch
</span><span class='line'>// Switch configuration based on Xcode version
</span><span class='line'><span class="nv">OTHER_SWIFT_FLAGS</span> <span class="o">=</span> <span class="k">$(</span>OTHER_SWIFT_FLAGS_XCODE_<span class="k">$(</span>XCODE_VERSION_MINOR<span class="k">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Full credit to Samantha Marshall&rsquo;s <a href="https://pewpewthespells.com/blog/migrating_code_signing.html#working-in-both-worlds">excellent writeups of xcconfig files</a>. Take a read, very useful to refer to.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reducing the pain of git bisect with Xcode]]></title>
    <link href="http://miqu.me/blog/2017/01/22/reducing-the-pain-of-git-bisect-with-xcode/"/>
    <updated>2017-01-22T11:49:01+00:00</updated>
    <id>http://miqu.me/blog/2017/01/22/reducing-the-pain-of-git-bisect-with-xcode</id>
    <content type="html"><![CDATA[<p>There&rsquo;s times when you need to investigate regressions in your project, and you don&rsquo;t know have any clue as why something is happening. <a href="https://git-scm.com/docs/git-bisect">Git bisect</a> is the best tool for this cases, but it can be painful to use in non small projects using CocoaPods and Xcode. I want to share what I&rsquo;ve been doing to ease the pain.</p>

<!-- more -->


<p>If you never used Git bisect, check this <a href="https://git-scm.com/book/en/v2/Git-Tools-Debugging-with-Git">introduction</a>. The git command performs a <a href="https://en.wikipedia.org/wiki/Binary_search_algorithm">binary search</a> across your repository history, starting from two known commits you provide to the algorithm: &lsquo;Good&rsquo; and &lsquo;Bad&rsquo;. &lsquo;Bad&rsquo; will usually be your last commit, and &lsquo;Good&rsquo; will be a commit back in the history of your repository, when you know the code was working as expected. For every step of the search you need to tell git if a commit is good or bad, either manually or automatically by running a script.</p>

<h1>Pain points</h1>

<p>When we enter the realm of iOS development, there&rsquo;s a some factors that make bisect tedious to use.</p>

<p>If you use CocoaPods, and if you don&rsquo;t check in the &lsquo;Pods&rsquo; directory in source control, every time you change branch or bisect selects a new commit, you might need to run <code>pod install</code> in order to have all dependencies available and compile correctly.</p>

<p>If you are searching for a regression, chances are you don&rsquo;t have an automated test suite, so you&rsquo;ll need to run your app manually and reproduce the issue you are searching for. There will be cases when you&rsquo;re dealing with a bug that needs an app reinstall or needs to run on device to reproduce. To top all this, Xcode will start the long indexing process every time there&rsquo;s changes to the sources due to change of commit, so your computer will start doing lot of work.</p>

<p>All of these nuances makes the process of searching for issues using bisect very tedious and not so &lsquo;magical&rsquo; as it is supposed to be. I&rsquo;ve nevertheless used bisect many times to save time otherwise wasted reading code and navigating breakpoints during long debugging sessions. You just need some patience and the will to automate some stuff.</p>

<h1>Reducing the pain</h1>

<h2>Automate pod install</h2>

<p>If you don&rsquo;t check in the &lsquo;Pods&rsquo; directory in source control, you might want to run <code>pod install</code> after every bisect step, to refresh all dependencies. Make use of git&rsquo;s <code>post-checkout</code> <a href="https://git-scm.com/docs/githooks">hook</a> and just perform the operation automatically. I like to close Xcode while updating pods so it doesn&rsquo;t turn crazy and slows down computer even more than usual:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/sh</span>
</span><span class='line'><span class="c"># Script for .git/hooks/post-checkout</span>
</span><span class='line'><span class="nb">set</span> -e
</span><span class='line'>osascript -e <span class="s1">&#39;quit app &quot;Xcode&quot;&#39;</span>
</span><span class='line'>pod install
</span><span class='line'>open -a Xcode
</span></code></pre></td></tr></table></div></figure>


<p>After you say &lsquo;good&rsquo; or &lsquo;bad&rsquo;, bisect chooses another commit, and automatically close Xcode, update dependencies and open Xcode again. You can be more sophisticated and open the current project directly. See this <a href="https://github.com/robbyrussell/oh-my-zsh/blob/57fcee0f1c520a7c5e3aa5e2bde974154cdaf0c3/plugins/xcode/xcode.plugin.zsh">example</a>.</p>

<h2>Automate build and run</h2>

<p>As I&rsquo;ve mentioned before, if you end up doing a bisect in the codebase you don&rsquo;t have unit tests covering the issue, or any other kind of regression suite covering the issue. You&rsquo;ll need to manually run the project and reproduce the bug. You can still automate the building and running, so you just need to wait to reproduce, and tell git if the commit was good or bad.</p>

<p>Combine <code>xcodebuild</code> with <a href="https://github.com/phonegap/ios-sim">ios-sim</a> in a <code>post-checkout</code> script:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/sh</span>
</span><span class='line'><span class="nb">set</span> -e
</span><span class='line'>xcodebuild -arch x86_64 -sdk iphonesimulator10.2 -derivedDataPath <span class="s2">&quot;.build&quot;</span> -scheme myScheme
</span><span class='line'>
</span><span class='line'>ios-sim launch --devicetypeid com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus .build/Build/Products/Debug-iphonesimulator/myapp.app
</span></code></pre></td></tr></table></div></figure>


<p>Here we force a simulator-only build and run it straight away so you can do your painful manual testing. You can combine the update of dependencies to just leave reproducing the issue and feedback as manual steps in the process.</p>

<h2>Repository discipline</h2>

<p>In order to be able to bisect faster and achieve meaningful results there&rsquo;s some practices to follow in your commits:</p>

<ul>
<li><strong>Granular commits are better</strong>: Smaller commits will make results of bisect more useful as the commit will have small changes, and will be easier to determine the source of the problem.</li>
<li><strong>Ensure every commit compiles</strong>: It will make a potential bisect easier by not struggling with commits that need to be fixed before tested</li>
<li><strong>Check in CocoaPods</strong>: There&rsquo;s many <a href="https://www.dzombak.com/blog/2014/03/including-pods-in-source-control.html">advantages some disadvantages</a> of keeping Pods in the repository. And one advantage will be the code compiles as-is without need of fetching dependencies.</li>
<li><strong>Write meaningful commits</strong>: This should be general practice in the team, but a meaningful commit might tell you what went wrong without even looking at the code, and will help teammates understand your changes.</li>
</ul>


<h1>Use bisect</h1>

<p>Everybody makes mistakes, and there will be the time when you&rsquo;ll be the one tasked to find out why something is not working, with no clue whatsoever of why. This is the time to think about using <code>git bisect</code>. It can be tedious under iOS projects, but you can ease the pain by automating some bits of the process and some patience.</p>

<p>And if you find finally find a commit of your 1-month younger self, you&rsquo;ll thank yourself if you wrote a meaningful commit message.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[App extensions, Xcode and Cocoapods, OMG!]]></title>
    <link href="http://miqu.me/blog/2016/11/28/app-extensions-xcode-and-cocoapods-omg/"/>
    <updated>2016-11-28T09:44:54+00:00</updated>
    <id>http://miqu.me/blog/2016/11/28/app-extensions-xcode-and-cocoapods-omg</id>
    <content type="html"><![CDATA[<p>Have you encountered this error when upgrading to the latest CocoaPods (1.1.0), or sharing a library between your iOS App and your extension?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="s1">&#39;sharedApplication&#39;</span> is unavailable: not available on iOS <span class="o">(</span>App Extension<span class="o">)</span> - Use view controller based solutions where appropriate instead.
</span></code></pre></td></tr></table></div></figure>


<p>If yes, continue reading, as you might have encountered same issue as myself. I&rsquo;ve recently had to upgrade a project to using Cocoapods 1.1.0. Things stopped compiling, and I had to investigate the root cause of the problem. It has to do with iOS App extensions, unavailable APIs and how fragile our tooling is ;).</p>

<!-- more -->


<h1>TL;DR</h1>

<ul>
<li>The first cause of error can be fixed by conditionally compiling with a macro. See example <a href="https://github.com/snowplow/snowplow-objc-tracker/blob/86c1049e960f72966ed61faa8824dbf1a73840f4/Snowplow/OpenIDFA.m#L48-L52">here</a></li>
<li>It&rsquo;s better if you define whole classes or API unavailable using <code>NS_EXTENSION_UNAVAILABLE_IOS</code></li>
<li>If you had this error in a 3rd party library you&rsquo;ll need it to be fixed by the author (see below)</li>
<li>If you are a library author and need to have different code paths via preprocessor macros, read <a href="cocoapods-issue">this thread</a>, and follow the recommendation to create a separated subspec for an extension target</li>
</ul>


<h1>Unavailable API for App extensions</h1>

<p>Since the introduction of <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/index.html">Application extensions</a> several years ago, Apple has marked some API as unavailable for these targets. For example, <code>sharedApplication</code> from <code>UIApplication</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">@property</span><span class="p">(</span><span class="k">class</span><span class="p">,</span> <span class="k">nonatomic</span><span class="p">,</span> <span class="k">readonly</span><span class="p">)</span> <span class="bp">UIApplication</span> <span class="o">*</span><span class="n">sharedApplication</span> <span class="n">NS_EXTENSION_UNAVAILABLE_IOS</span><span class="p">(</span><span class="s">&quot;Use view controller based solutions where appropriate instead.&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>or in Swift:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="p">@</span><span class="n">available</span><span class="p">(</span><span class="n">iOSApplicationExtension</span><span class="p">,</span> <span class="n">unavailable</span><span class="p">)</span>
</span><span class='line'><span class="n">open</span> <span class="k">class</span> <span class="k">var</span> <span class="nl">shared</span><span class="p">:</span> <span class="bp">UIApplication</span> <span class="p">{</span> <span class="kr">get</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Apple is using a new macro, <code>NS_EXTENSION_UNAVAILABLE_IOS</code> to mark API as unavailable. There&rsquo;s a new setting on Xcode, <code>APPLICATION_EXTENSION_API_ONLY</code>, and if set, the code will not compile if it contains a call to <code>sharedApplication</code>. This setting is automatically enabled for extension targets so you get the error in your code when you are writing it.</p>

<p>Writing separate code for an App target and and extension target is not an issue. You just don&rsquo;t use the unavailable API in the extension. But what about reusable libraries?</p>

<h1>Libraries using unavailable API for extensions</h1>

<p>You might be using several libraries, and some of these might be using unavailable API for extensions. Notably AFNetworking is one of these, check the <a href="https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/AFNetworkActivityIndicatorManager.h#L44">sources</a>. The developer of the library must take care of this in their code, and mark API that is unavailable for extensions because of the usage of restricted system API.</p>

<p>But then, there might be code that is executing a different code path when compiled against an extension, for example <a href="https://github.com/pinterest/PINCache/pull/72">PinCache</a>. If this is the case, the library is forcing you to decide between using unsafe API or not using it. You&rsquo;ll need to define the designated macro in your project to achieve this. See an example <a href="https://github.com/snowplow/snowplow-objc-tracker/blob/86c1049e960f72966ed61faa8824dbf1a73840f4/Snowplow/OpenIDFA.m#L48-L52">here</a>.</p>

<h1>Enter CocoaPods</h1>

<p>The problem comes with the integration with CocoaPods, as they (correctly) deduplicate targets. This means that you&rsquo;ll compile the library once (say without unavailable API) and link it to your targets. But sometimes you want to compile with usage of unavailable APIs against your main app, and removing unavailable usage on the extension. If you want this, you&rsquo;re out of luck as it&rsquo;s not directly supported by Cocoapods.</p>

<h2>What changed?</h2>

<p>Since <a href="http://blog.cocoapods.org/CocoaPods-1.1.0/">Cocoapods 1.1.0</a>, they improved integration with App extensions, and the generated project will enable the flag <code>APPLICATION_EXTENSION_API_ONLY</code> for libraries linking against an extension target. This is correct, but then you&rsquo;ll start seeing compilation errors that can be a bit puzzling. Bear in mind that the code will not compile even if you don&rsquo;t use the offending API. The compiler just complains that there is code that uses unavailable API.</p>

<h1>Solutions</h1>

<p>So you can&rsquo;t compile, you can&rsquo;t just disable all unavailable API for your main App target. What do you do? There is hope! See <a href="cocoapods-issue">this discussion</a>, and you&rsquo;ll see <a href="https://twitter.com/neonacho">neonacho</a> suggests to use a subspec to duplicate the targets. This is a very practical solution, but it requires the library author to modify their podspec.</p>

<h2>Example</h2>

<p>Let&rsquo;s see an example for a specific library. I had to fork Snowplow (the library we&rsquo;re using) and add the subspec. If you want to check the real changes, a PR is <a href="https://github.com/snowplow/snowplow-objc-tracker/pull/303">here</a>. Now let&rsquo;s work it out with a fictional example replicating what&rsquo;s required. Say you&rsquo;re owner of <code>MyLibrary</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Pod</span><span class="o">::</span><span class="no">Spec</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">s</span><span class="o">|</span>
</span><span class='line'>  <span class="n">s</span><span class="o">.</span><span class="n">name</span>             <span class="o">=</span> <span class="s2">&quot;MyLibrary&quot;</span>
</span><span class='line'>  <span class="c1"># Omitting metadata stuff and deployment targets</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">s</span><span class="o">.</span><span class="n">source_files</span> <span class="o">=</span> <span class="s1">&#39;MyLibrary/*.{m,h}&#39;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>You use unavailable API, so the code conditionally compiles some parts based on a preprocessor macro called <code>MYLIBRARY_APP_EXTENSIONS</code>. We declare a subspec, called <strong>Core</strong> with all the code, but the flag off. We make that subspec the default one if user doesn&rsquo;t specify one. Then we&rsquo;ll declare an additional subspec, called <strong>AppExtension</strong> including all the code, but setting the preprocessor macro:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Pod</span><span class="o">::</span><span class="no">Spec</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">s</span><span class="o">|</span>
</span><span class='line'>  <span class="n">s</span><span class="o">.</span><span class="n">name</span>             <span class="o">=</span> <span class="s2">&quot;MyLibrary&quot;</span>
</span><span class='line'>  <span class="c1"># Omitting metadata stuff and deployment targets</span>
</span><span class='line'>  <span class="n">s</span><span class="o">.</span><span class="n">default_subspec</span> <span class="o">=</span> <span class="s1">&#39;Core&#39;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">s</span><span class="o">.</span><span class="n">subspec</span> <span class="s1">&#39;Core&#39;</span> <span class="k">do</span> <span class="o">|</span><span class="n">core</span><span class="o">|</span>
</span><span class='line'>    <span class="n">core</span><span class="o">.</span><span class="n">source_files</span> <span class="o">=</span> <span class="s1">&#39;MyLibrary/*.{m,h}&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">s</span><span class="o">.</span><span class="n">subspec</span> <span class="s1">&#39;AppExtension&#39;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ext</span><span class="o">|</span>
</span><span class='line'>    <span class="n">ext</span><span class="o">.</span><span class="n">source_files</span> <span class="o">=</span> <span class="s1">&#39;MyLibrary/*.{m,h}&#39;</span>
</span><span class='line'>    <span class="c1"># For app extensions, disabling code paths using unavailable API</span>
</span><span class='line'>    <span class="n">ext</span><span class="o">.</span><span class="n">pod_target_xcconfig</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">&#39;GCC_PREPROCESSOR_DEFINITIONS&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;MYLIBRARY_APP_EXTENSIONS=1&#39;</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then in your application Podfile you&rsquo;ll link against <strong>Core</strong> in your main app target, and against <strong>AppExtension</strong> in your extension, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">abstract_target</span> <span class="s1">&#39;App&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="c1"># Shared pods between App and extension, compiled with same preprocessor macros</span>
</span><span class='line'>  <span class="n">pod</span> <span class="s1">&#39;AFNetworking&#39;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">target</span> <span class="s1">&#39;MyApp&#39;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">pod</span> <span class="s1">&#39;MyLibrary/Core&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">target</span> <span class="s1">&#39;MyExtension&#39;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">pod</span> <span class="s1">&#39;MyLibrary/AppExtension&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s it! <a href="https://twitter.com/neonacho">neonacho&rsquo;s</a> suggestion works very well and it&rsquo;s kind of simple. Hopefully this writeup will help you find the solution to your problem (and understand it) if you ever face it. Kudos for the CocoaPods team to offer support on these issues. We&rsquo;re always catching up with Apple after they break (again) Xcode.</p>

<h1>A note about Swift</h1>

<p>I&rsquo;ve found an issue created for Swift, <a href="https://bugs.swift.org/browse/SR-1226">SR-1226</a>, that is still unresolved and might cause you problems. It seems that as of now, marking API as unavailable for extensions in Swift still doesn&rsquo;t let you compile for App extensions. So be aware of this limitation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Easy localization management with Google spreadsheets]]></title>
    <link href="http://miqu.me/blog/2016/11/19/easy-localizations-management-with-spreadsheets/"/>
    <updated>2016-11-19T16:50:18+00:00</updated>
    <id>http://miqu.me/blog/2016/11/19/easy-localizations-management-with-spreadsheets</id>
    <content type="html"><![CDATA[<p>Last thursday I gave a talk at <a href="http://www.meetup.com/NSLondon/">NSLondon meetup</a> called &lsquo;Minimum Viable Tooling&rsquo;. The topic was examples of approaching tooling and automation for small teams. I showed real examples of what we do at <a href="http://www.peak.net">Peak</a>, where I currently work. One of the things people showed interest in after the talk was how we deal with localizations.</p>

<p>In this post I will replicate the simple solution from scratch, so you can follow along and get started with your own projects. And we will be using Swift for the scripting, just because we can!</p>

<!-- more -->


<h1>The minimal CMS</h1>

<p>In the talk I showed what we currently use to automate generation of strings at <a href="http://www.peak.net">Peak</a>. The goal of the solution is twofold: The first is to remove manual manipulation of local strings files by developers. The second is to raise visibility of any translation across the company and enable easier collaboration with translators. This is generally achieved by using a CMS, either a built solution or your own. We&rsquo;ll build our own by leveraging Google spreadsheets as a database and web interface.</p>

<p>So we&rsquo;ll need two simple parts: One one side, the strings are hosted in a Google spreasheet. Then on the other side, we&rsquo;ll run a script locally or on our CI every time we want to update strings in the application. The script will load the spreasheet contents, and generate the appropiate <code>.strings</code> file contents.</p>

<h1>The spreadsheet</h1>

<p>I&rsquo;ve created an example spreadsheet <a href="https://docs.google.com/spreadsheets/d/1pduU_ZiMNjPmQVl5_dDjK2XcgacfJkARsA567ddOrGg/edit?usp=sharing">here</a>. It contains rows with keys, and columns with the supported language keys in your app:</p>

<p><img src="http://miqu.me/images/posts/spreadsheet-1.png" alt="spreadsheet1" /></p>

<p>Here you can take two approaches for your script; you can either use any Google spreadsheet library for a popular scripting language like Ruby or Python, or you can leverage yet another service on top of spreasheets: <a href="https://sheetsu.com">Sheetsu</a>. Using the service means you don&rsquo;t need any library because it converts the content to a JSON API that can be easily parsed with traditional code. We&rsquo;ll use this as it&rsquo;s the simplest solution. Note that we would need to pay to have more flexibility as the free plan only allows one spreadsheet without any additional sheets, but this is fine for our example.</p>

<h2>Sheetsu</h2>

<p>Sign up to <a href="https://sheetsu.com">Sheetsu</a>, and give them the spreadsheet you want to use. Once you do this, they&rsquo;ll automatically create an API endpoint:</p>

<p><img src="http://miqu.me/images/posts/sheetsu-1.png" alt="sheetsu1" /></p>

<h1>The script</h1>

<p>We&rsquo;ll need to setup Swift for scripting. Given that we don&rsquo;t use any libraries, we can develop the code in a playground, and when we&rsquo;re done we can move the file to our repository. Here&rsquo;s an example playground code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">import</span> <span class="n">Foundation</span>
</span><span class='line'><span class="k">import</span> <span class="n">PlaygroundSupport</span>
</span><span class='line'>
</span><span class='line'><span class="n">PlaygroundPage</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">needsIndefiniteExecution</span> <span class="o">=</span> <span class="nb">true</span>
</span><span class='line'>
</span><span class='line'><span class="k">typealias</span> <span class="n">RawLocalizationData</span> <span class="o">=</span> <span class="p">[[</span><span class="nl">String</span><span class="p">:</span><span class="n">String</span><span class="p">]]</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">Entry</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">var</span> <span class="nl">key</span><span class="p">:</span> <span class="n">String</span>
</span><span class='line'>    <span class="k">var</span> <span class="nl">value</span><span class="p">:</span> <span class="n">String</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">Localization</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">var</span> <span class="nl">languageKey</span><span class="p">:</span> <span class="n">String</span>
</span><span class='line'>    <span class="k">var</span> <span class="nl">entries</span><span class="p">:</span> <span class="p">[</span><span class="n">Entry</span><span class="p">]</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">extension</span> <span class="n">Localization</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">func</span> <span class="n">export</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="n">String</span><span class="p">]</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">entries</span><span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="n">entry</span> <span class="k">in</span>
</span><span class='line'>            <span class="k">let</span> <span class="n">value</span> <span class="o">=</span> <span class="n">entry</span><span class="p">.</span><span class="n">value</span><span class="p">.</span><span class="n">characters</span><span class="p">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">?</span> <span class="n">entry</span><span class="p">.</span><span class="nl">value</span> <span class="p">:</span> <span class="s">&quot;NOT_TRANSLATED&quot;</span>
</span><span class='line'>            <span class="k">return</span> <span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">\(entry.key)</span><span class="se">\&quot;</span><span class="s"> = </span><span class="se">\&quot;</span><span class="s">\(value)</span><span class="se">\&quot;</span><span class="s">;&quot;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">func</span> <span class="n">findLanguages</span><span class="p">(</span><span class="k">in</span> <span class="nl">data</span><span class="p">:</span> <span class="n">RawLocalizationData</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="n">String</span><span class="p">]</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">guard</span> <span class="k">let</span> <span class="n">firstEntry</span> <span class="o">=</span> <span class="n">data</span><span class="p">.</span><span class="n">first</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="p">[]</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">assert</span><span class="p">(</span><span class="n">firstEntry</span><span class="p">[</span><span class="s">&quot;key&quot;</span><span class="p">]</span> <span class="o">!=</span> <span class="nb">nil</span><span class="p">)</span>
</span><span class='line'>    <span class="k">let</span> <span class="n">languages</span> <span class="o">=</span> <span class="n">firstEntry</span><span class="p">.</span><span class="n">keys</span><span class="p">.</span><span class="n">filter</span> <span class="p">{</span> <span class="err">$</span><span class="mi">0</span> <span class="o">!=</span> <span class="s">&quot;key&quot;</span> <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">languages</span><span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="err">$</span><span class="mi">0</span> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">func</span> <span class="n">parse</span><span class="p">(</span><span class="n">_</span> <span class="nl">data</span><span class="p">:</span> <span class="n">RawLocalizationData</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="n">Localization</span><span class="p">]</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">let</span> <span class="n">languages</span> <span class="o">=</span> <span class="n">findLanguages</span><span class="p">(</span><span class="k">in</span><span class="o">:</span> <span class="n">data</span><span class="p">)</span>
</span><span class='line'>    <span class="k">var</span> <span class="n">localizations</span> <span class="o">=</span> <span class="p">[</span><span class="nl">String</span><span class="p">:</span><span class="n">Localization</span><span class="p">]()</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">for</span> <span class="n">entry</span> <span class="k">in</span> <span class="n">data</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">let</span> <span class="n">key</span> <span class="o">=</span> <span class="n">entry</span><span class="p">[</span><span class="s">&quot;key&quot;</span><span class="p">]</span> <span class="o">??</span> <span class="s">&quot;__ERROR__NO_KEY__&quot;</span>
</span><span class='line'>        <span class="k">let</span> <span class="n">values</span> <span class="o">=</span> <span class="n">languages</span><span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="p">(</span><span class="nl">language</span><span class="p">:</span> <span class="err">$</span><span class="mi">0</span><span class="p">,</span> <span class="nl">entry</span><span class="p">:</span> <span class="n">Entry</span><span class="p">(</span><span class="nl">key</span><span class="p">:</span> <span class="n">key</span><span class="p">,</span> <span class="nl">value</span><span class="p">:</span> <span class="n">entry</span><span class="p">[</span><span class="err">$</span><span class="mi">0</span><span class="p">]</span><span class="o">!</span><span class="p">))</span> <span class="p">}</span>
</span><span class='line'>        <span class="k">for</span> <span class="n">value</span> <span class="k">in</span> <span class="n">values</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">var</span> <span class="n">localization</span> <span class="o">=</span> <span class="n">localizations</span><span class="p">[</span><span class="n">value</span><span class="p">.</span><span class="n">language</span><span class="p">]</span> <span class="o">??</span> <span class="n">Localization</span><span class="p">(</span><span class="nl">languageKey</span><span class="p">:</span> <span class="n">value</span><span class="p">.</span><span class="n">language</span><span class="p">,</span> <span class="nl">entries</span><span class="p">:</span> <span class="p">[])</span>
</span><span class='line'>            <span class="n">localization</span><span class="p">.</span><span class="n">entries</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">entry</span><span class="p">)</span>
</span><span class='line'>            <span class="n">localizations</span><span class="p">[</span><span class="n">value</span><span class="p">.</span><span class="n">language</span><span class="p">]</span> <span class="o">=</span> <span class="n">localization</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="n">localizations</span><span class="p">.</span><span class="n">values</span><span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="err">$</span><span class="mi">0</span> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">func</span> <span class="n">write</span><span class="p">(</span><span class="n">_</span> <span class="nl">localizations</span><span class="p">:</span> <span class="p">[</span><span class="n">Localization</span><span class="p">])</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">localization</span> <span class="k">in</span> <span class="n">localizations</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">print</span><span class="p">(</span><span class="n">localization</span><span class="p">.</span><span class="n">export</span><span class="p">().</span><span class="n">joined</span><span class="p">(</span><span class="nl">separator</span><span class="p">:</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">))</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">let</span> <span class="n">spreadsheetURL</span> <span class="o">=</span> <span class="n">URL</span><span class="p">(</span><span class="nl">string</span><span class="p">:</span> <span class="s">&quot;https://sheetsu.com/apis/v1.0/a96943d37c32&quot;</span><span class="p">,</span> <span class="nl">relativeTo</span><span class="p">:</span> <span class="nb">nil</span><span class="p">)</span><span class="o">!</span>
</span><span class='line'>
</span><span class='line'><span class="k">let</span> <span class="n">session</span> <span class="o">=</span> <span class="n">URLSession</span><span class="p">.</span><span class="n">shared</span>
</span><span class='line'><span class="k">let</span> <span class="n">task</span> <span class="o">=</span> <span class="n">session</span><span class="p">.</span><span class="n">dataTask</span><span class="p">(</span><span class="nl">with</span><span class="p">:</span> <span class="n">spreadsheetURL</span><span class="p">)</span> <span class="p">{</span> <span class="n">data</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">error</span> <span class="k">in</span>
</span><span class='line'>    <span class="n">guard</span> <span class="k">let</span> <span class="n">data</span> <span class="o">=</span> <span class="n">data</span> <span class="k">else</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="s">&quot;Error&quot;</span><span class="p">);</span> <span class="k">return</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">let</span> <span class="n">json</span> <span class="o">=</span> <span class="n">try</span><span class="o">?</span> <span class="n">JSONSerialization</span><span class="p">.</span><span class="n">jsonObject</span><span class="p">(</span><span class="nl">with</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="nl">options</span><span class="p">:</span> <span class="p">[])</span> <span class="kt">as</span><span class="o">!</span> <span class="n">RawLocalizationData</span>
</span><span class='line'>    <span class="k">let</span> <span class="n">localizations</span> <span class="o">=</span> <span class="n">parse</span><span class="p">(</span><span class="n">json</span> <span class="o">??</span> <span class="p">[])</span>
</span><span class='line'>    <span class="n">write</span><span class="p">(</span><span class="n">localizations</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">PlaygroundPage</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">finishExecution</span><span class="p">()</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">task</span><span class="p">.</span><span class="n">resume</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>The only thing worthy of note is using <code>PlaygroundSupport</code> to enable asynchrous execution. All the other code is straightforward use of <code>URLSesssion</code> to parse JSON. All that is left is converting the playground to a script. The script doesn&rsquo;t save contents to any file, because when running in playground we&rsquo;re constrained to the sandbox. When moving to a script we can generate the folders and files as appropiate for our application. We&rsquo;re not going to do all these finishing touches now, but it&rsquo;s very simple to continue from here.</p>

<p>It only took me 1 hours to setup, and considering I had to look up how to use <code>PlaygroundSupport</code>, I would say it&rsquo;s very quick to implement!</p>

<h1>Caveats</h1>

<p>With this approach we can quickly build automation around localizations, and we can share the spreadsheet with our translators. This example spreadsheet does not contain strings with placeholders (for example: <code>"WELCOME_MESSAGE" = "Hello %@!"</code>). This will require handling validation, because the translators can - and will - make mistakes when writing their new strings. In a later post I will elaborate the required validation of strings with placeholders, and how we can reduce errors with human-readable tokens.</p>

<p>I hope this is useful to you.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Controlling time in the app]]></title>
    <link href="http://miqu.me/blog/2016/10/26/controlling-time-in-the-app/"/>
    <updated>2016-10-26T18:38:37+00:00</updated>
    <id>http://miqu.me/blog/2016/10/26/controlling-time-in-the-app</id>
    <content type="html"><![CDATA[<p>Today I want to share a small utility we&rsquo;ve been using for a while at <a href="http://www.peak.net">Peak</a>, my current workplace, to control the system time inside the application and save time while testing or debugging.</p>

<!-- more -->


<p>In most application code you&rsquo;ll eventually end up with tasks that need to execute periodically, or after some time has passed.  The period of time will depend on the actual application requirements, and might change from the order of seconds to days. For example, you might have a data cleanup every 30 days, your application might ping a backend for synchronization every 10 minutes, or by the start of every day, some data needs to be generated and presented to the user.</p>

<h2>Foundation and dates</h2>

<p>You&rsquo;ll eventually use <code>NSDate</code> and <code>NSCalendar</code> (or <code>Date</code> and <code>Calendar</code> in Swift) to calculate when your tasks should be executed. As an example, say you have a task scheduler, and it will run tasks after a specific amount of time passed:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">protocol</span> <span class="n">Task</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">func</span> <span class="n">execute</span><span class="p">()</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="n">Scheduler</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">func</span> <span class="n">schedule</span><span class="p">(</span><span class="n">_</span> <span class="nl">task</span><span class="p">:</span> <span class="n">Task</span><span class="p">,</span> <span class="nl">every</span><span class="p">:</span> <span class="n">TimeInterval</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Schedule and save task to run every x seconds</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">func</span> <span class="n">run</span><span class="p">(</span><span class="n">with</span> <span class="nl">date</span><span class="p">:</span> <span class="n">Date</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Actually perform the calculations and fire due tasks</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>For this code to be testable it&rsquo;s a very common practice to pass the <code>Date</code> in, as we do in the example above. This will improve testability as the date can be controlled from unit tests, thus isolating the system date from the date the component uses to operate.</p>

<p>But what about the times when you want to check the integration between the code and the system date? What if you want to trigger code that reacts to <code>UIApplicationSignificantTimeChange</code>?</p>

<p>The traditional way to test this kind of interactions is to change the date on the device, be it on iOS directly, or you or computer if running the simulator. But it would be very useful to control the system date and time inside the application without having to change the system date in the device or your computer.</p>

<h2>Time travel</h2>

<p>To control the time from a debugging menu in the application, we used a library called <a href="https://github.com/tuenti/TUDelorean">TUDelorean</a>. This library does some runtime method substitutions to trick any user of <code>Date</code> to a date that is no longer tied to the system, but controlled by us. This class is intended to used for unit tests, and that&rsquo;s how I&rsquo;ve used it in the past. But you can also use it inside your app for testing purposes, and offset the time all the code, even system code, sees.</p>

<p>You can build a small in-app debugging menu where you can specify the amount of time to move from the system date, and trigger the update through the library while you run the app.</p>

<p>Shifting the time is very easy:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">let</span> <span class="nl">futureDate</span><span class="p">:</span> <span class="n">Date</span> <span class="c1">// Calculate your future date here</span>
</span><span class='line'>
</span><span class='line'><span class="n">TUDelorean</span><span class="p">.</span><span class="n">timeTravel</span><span class="p">(</span><span class="nl">to</span><span class="p">:</span> <span class="n">futureDate</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>After this, any code calling <code>Date</code> will be handled a fake date not tied to the system anymore. If you build this testing facility, you can also trigger events on behalf of the system, just to test your assumptions. For example, if you move your date a day forward, you can emit <code>UIApplicationSignificantTimeChange</code> as if a real new day passed in the system clock. This is very useful because you&rsquo;ll be able to test integrations otherwise impossible from unit tests.</p>

<p>When you&rsquo;re done with testing, and want to return to your &lsquo;current&rsquo; system date, just reset it with the provided method:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="n">TUDelorean</span><span class="p">.</span><span class="n">backToThePresent</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Conclusion</h2>

<p>Using a tool designed for unit tests, you can build a small utility that can make your day to day development tasks more effective and less tedious. Here we looked at how controlling the time while running your application is useful, and we used a <a href="https://github.com/tuenti/TUDelorean">library</a> designed for use in unit tests for our in-app debugging usage.</p>

<p>I hope you find this as useful as we&rsquo;ve found it. Suffice to say that it should not be used for more than testing and debugging.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Knowing when to delete code]]></title>
    <link href="http://miqu.me/blog/2016/08/16/knowing-when-to-delete-code/"/>
    <updated>2016-08-16T20:04:04+00:00</updated>
    <id>http://miqu.me/blog/2016/08/16/knowing-when-to-delete-code</id>
    <content type="html"><![CDATA[<p>Much of the literature and writing related to sofware development focuses on the creation, new ideas, fresh ideas and things to improve in your next project. But when is it a good moment to delete the old stuff in an long term project? Here&rsquo;s my short rant about this; I recently deleted a chunk of code in my current project and just felt I wanted to write my thoughts.</p>

<!-- more -->


<h1>Short-lived code</h1>

<p>Working on application development field, it is very normal that some code is very short lived. If you&rsquo;re looking to build systems and generic components then you&rsquo;re generally out of luck. Most Application code is generally short lived. In my opinion we just need to know how to deal with it.</p>

<p>Products evolve, ideas and markets change, and the code implementing those concepts has to change to reflect the direction of an application. If your code doesn&rsquo;t closely relate or express the domain of your application, you&rsquo;re bound to have design problems, miscomunication and likely many bugs. Application code is very likely to become legacy in a very short time. That time might be even less than 1 or 2 years.</p>

<h1>Legacy code</h1>

<p>There isn&rsquo;t consensus on what makes code reach the dreaded <em>legacy</em> status. The definition has changed since it&rsquo;s first introduction, and nowadays we generally consider legacy the code that we didn&rsquo;t write. See the <a href="https://en.wikipedia.org/wiki/Legacy_code">wikipedia entry</a>:</p>

<blockquote><p>&hellip;source code inherited from someone else and source code inherited from an older version of the software</p></blockquote>

<p>Or another definition:</p>

<blockquote><p>Michael Feathers introduced a definition of legacy code as code without tests, which reflects the perspective of legacy code being difficult to work with in part due to a lack of automated regression tests</p></blockquote>

<p>Many programmers generally call legacy code anything that they didn&rsquo;t write. While this is a very harsh treament of somebody&rsquo;s effort and time, it will mean that <strong>your</strong> code will become legacy in very short time as well.</p>

<h1>Refactoring vs Rewriting</h1>

<p>Legacy code is often mentioned when refactoring. The effort of writing some piece of functionality again versus the cost of refactoring code is something we all need to consider at some point. I think that you should always evaluate this in the light of your project and always ponder the real end-user value of your rewrite or refactor. Sometimes if something works <a href="http://www.cnbc.com/2016/05/25/us-military-uses-8-inch-floppy-disks-to-coordinate-nuclear-force-operations.html">it&rsquo;s better not to change it</a>.</p>

<p>My view is that we should always try to do small refactorings, even if that implies rewriting small parts of functionality. Big rewrites never pay off in my experience, but hey I might be wrong. Read Martin Fowler&rsquo;s <a href="http://martinfowler.com/articles/workflowsOfRefactoring/">excellent entry</a> on refactoring.</p>

<h1>Clean after yourself</h1>

<p>I&rsquo;m more concerned about dead code than necessary refactoring efforts. We rarely spend the time to analyze our projects and search for dead code. Things we left out after refactoring or an old clean up. The engineering team is responsible to clean the lower decks of the ship, so to speak, and it&rsquo;s our duty to ensure that the environment we work in is clean and up to date. Nobody else will do it otherwise.</p>

<p>I think developers need to emotionally detach themselves from code they wrote, and be ready to delete things that don&rsquo;t work or are no longer relevant. ABTests, temporary user migrations, temporary bridging code between legacy systems and new systems. I think we shouldn&rsquo;t save idle and not used code &lsquo;just in case we need it&rsquo;, as it adds to the cruft of the project. You&rsquo;ll forget after a while and nobody will know what to do with it. Just delete it.</p>

<p>To conclude, if you introduce any kind of prize contest in your engineering team, then please, give a prize to the engineer that deleted the most lines of code, not the one that wrote the most. You can be sure that that engineer didn&rsquo;t introduce any bugs nor regressions.</p>

<p>Ripley signing off.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Xcode 8: New build settings and analyzer improvements]]></title>
    <link href="http://miqu.me/blog/2016/07/31/xcode-8-new-build-settings-and-analyzer-improvements/"/>
    <updated>2016-07-31T20:50:16+00:00</updated>
    <id>http://miqu.me/blog/2016/07/31/xcode-8-new-build-settings-and-analyzer-improvements</id>
    <content type="html"><![CDATA[<p>I always like to check the new build settings and analyzer improvements of every Xcode release. And this year&rsquo;s main release includes a couple of goodies. Let&rsquo;s check them out!</p>

<!-- more -->


<h1>Analyzer improvements</h1>

<p>From the current Xcode 8 beta 3 <a href="http://adcdownload.apple.com/Developer_Tools/Xcode_8_beta_3/Release_Notes_for_Xcode_8_beta_3.pdf">release notes</a>:</p>

<h2>&lsquo;Misuse of null&rsquo; or CLANG_ANALYZER_NONNULL</h2>

<blockquote><p>The static analyzer check for nullability violations supports both aggressive and less aggressive levels
of checking. The more aggressive level checks for nullability violations in all calls</p></blockquote>

<p>This is a very nice addition for anybody dealing with <em>legacy</em> Objective-C code and audited APIs. So remember to enable the more aggressive setting in your project as it won&rsquo;t be enabled after your upgrade.</p>

<p>Most likely you&rsquo;ll find that some APIs were not used correctly, and you&rsquo;ll get more warnings. You should set it to <code>YES</code> instead of <code>YES_NONAGRESSIVE</code> for the most strict checking.</p>

<p>Remember you can revert (hopefully temporarily) to <code>YES_NONAGRESSIVE</code> so you can make your code compile while you audit the analyzer warnings again.</p>

<h2>New setting: CLANG_ANALYZER_OBJC_DEALLOC</h2>

<blockquote><p>The clang static analyzer now checks for improper cleanup of synthesized instance variables in
-dealloc methods</p></blockquote>

<p>Possibly a minor one, but remember that the analyzer could give you new false positives in legacy code. I couldn&rsquo;t make it trigger in a test project, so I don&rsquo;t really know what it will warn you about.</p>

<h1>New build settings</h1>

<h2>New setting: SWIFT_ACTIVE_COMPILATION_CONDITIONS</h2>

<blockquote><p>“Active Compilation Conditions” is a new build setting for passing conditional compilation flags to
the Swift compiler.</p></blockquote>

<p>Previously, we had to declare your conditional compilation flags under <strong>OTHER_SWIFT_FLAGS</strong>, remembering to prepend &ldquo;-D&rdquo; to the setting. For example, to conditionally compile with a <code>MYFLAG</code> value:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="cp">#if MYFLAG</span>
</span><span class='line'><span class="c1">// do stuff</span>
</span><span class='line'><span class="cp">#endif</span>
</span></code></pre></td></tr></table></div></figure>


<p>The value to add to the setting <code>-DMYFLAG</code></p>

<p>Now we only need to pass the value <code>MYFLAG</code> to the new setting. Time to move all those conditional compilation values!</p>

<h2>New settings: SWIFT_SUPPRESS_WARNINGS and SWIFT_TREAT_WARNINGS_AS_ERRORS</h2>

<blockquote><p>Two new build settings have been added to enable Swift compiler options: -suppress-warnings
(SWIFT_SUPPRESS_WARNINGS) and -warnings-as-errors (SWIFT_TREAT_WARNINGS_AS_ERRORS).</p></blockquote>

<p>Given the compiler will emit warnings, specially during transition periods to future versions of the language, it&rsquo;s a good idea to turn this setting off temporarily. The default value for both is <code>NO</code>, but I&rsquo;m inclined to turn <strong>SWIFT_TREAT_WARNINGS_AS_ERRORS</strong> directly and solve issues straight away.</p>

<h2>EMBEDDED_CONTENT_CONTAINS_SWIFT is deprecated</h2>

<p>Basically this setting has been deprecated. See what it was doing here in the <a href="https://developer.apple.com/library/ios/qa/qa1881/_index.html">Apple technote</a>. You might encounter problems when building your application so it&rsquo;s worth upgrading to the new setting if you are using it.</p>

<blockquote><p>The new build setting ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES replaces the use of
EMBEDDED_CONTENT_CONTAINS_SWIFT. This setting indicates that Xcode should always embed
Swift standard libraries in a target for which it has been set, whether or not the target contains Swift
code.</p></blockquote>

<p>I&rsquo;m guessing these are changes preparing ahead of a binary compatible Swift 4 release. But otherwise I don&rsquo;t see what the new build setting does differently.</p>

<h1>XCConfig files</h1>

<p>If you&rsquo;re like me and like to use <a href="https://pewpewthespells.com/blog/xcconfig_guide.html">xcconfig files</a> then this new release has some small improvements as well. I&rsquo;m very happy!</p>

<blockquote><p>.xcconfig files support conditional inclusion of other .xcconfig files, using the syntax #include?</p></blockquote>

<p>If you have a use case for this one, please let me <a href="https://twitter.com/miguelquinon">know</a>!</p>

<p>And the last, but not least:</p>

<blockquote><p>Xcode takes build settings set in xcconfig files into account when suggesting updates to your build
settings.</p></blockquote>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dear SDK developer]]></title>
    <link href="http://miqu.me/blog/2016/04/22/dear-sdk-developer/"/>
    <updated>2016-04-22T11:50:32+00:00</updated>
    <id>http://miqu.me/blog/2016/04/22/dear-sdk-developer</id>
    <content type="html"><![CDATA[<p>As an application developer I get to use many third party code to integrate it into my current project. I want to share a checklist of things a user of an SDK really appreciates.</p>

<!-- more -->


<h1>Documentation</h1>

<p>This is the most important part of any SDK, be it open source or closed source. Users don&rsquo;t need to understand your code, or learn by themselves how to use your APIs. Maintaining documentation is a big part of maintaining an SDK, and it goes far beyond documenting the code.</p>

<h1>Documentation!</h1>

<p>Really, this is very important and lacking on many commercial SDKs. Please include examples, rationale and FAQ.</p>

<h1>Documentation!!</h1>

<p>Don&rsquo;t get it? Just some examples of great additional documentation:</p>

<ul>
<li>Quick: <a href="https://github.com/Quick/Quick/tree/master/Documentation/en-us">Quick</a></li>
<li>RxSwift: <a href="https://github.com/ReactiveX/RxSwift/tree/master/Documentation">RxSwift</a></li>
<li>YapDatabase: <a href="https://github.com/yapstudios/YapDatabase/wiki">YapDatabase</a></li>
</ul>


<h1>Please, open source your SDK</h1>

<p>There&rsquo;s nothing more annonying than encountering bugs or unexpected behaviour and having to skim through logs, documentation and trial and error tests. This can happen anytime when using unfamiliar APIs, and having the source to understand what goes wrong is the first big benefit of open source SDKs. Furthermore, if you accept contributions from the community, you might get some developers helping with debugging and fixing issues.</p>

<p>Having open issue discussions help with questions and faster communication. A good example is projects on Github. See <a href="https://github.com/snowplow/snowplow-objc-tracker/issues/274">an example of Snowplow devs just guiding me over my confusion</a>.</p>

<p>Surely your business model is not dependant on you not publishing the source code that interacts with your platform?</p>

<h1>Changelogs</h1>

<p>Any developer including third party code in an application is assuming the quality of your code into theirs. They also have the final accountability for the end users, who don&rsquo;t care where a bug or crash is coming from. Good engineering practice will make users of your SDK wary of changes you introduce, so a changelog is very important to let them know what changed.</p>

<p>And please, don&rsquo;t just add <em>stability improvements</em>.</p>

<h1>Version migration documentation</h1>

<p>More documentation! This is very special and also very important. If you maintain an SDK for long enough you&rsquo;re necessarily going to need to break APIs. It&rsquo;s at this time that users of your SDK will suffer the most, because an application developer doesn&rsquo;t track your development on a day to day basis. It&rsquo;s very crucial for the happiness of your users that you provide good examples of how to transition to new APIs with the least effort possible. I like this migration guide from <a href="http://snowplowanalytics.com/blog/2016/01/18/snowplow-objective-c-tracker-0.6.0-released/">Snowplow</a> a lot.</p>

<h1>Your SDK is not the only one</h1>

<p>Many examples you can find are very naive in terms of what the host application code contains. Don&rsquo;t expect that the user of the SDK will be using your code exclusively. Compare these two <em>getting started</em> examples from very respectable SDKs:</p>

<p><strong>Facebook</strong>. From their <a href="https://developers.facebook.com/docs/ios/getting-started#delegate">docs</a>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="c1">//  AppDelegate.m</span>
</span><span class='line'><span class="cp">#import &lt;FBSDKCoreKit/FBSDKCoreKit.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="p">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">application:</span><span class="p">(</span><span class="bp">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="nf">didFinishLaunchingWithOptions:</span><span class="p">(</span><span class="bp">NSDictionary</span> <span class="o">*</span><span class="p">)</span><span class="nv">launchOptions</span> <span class="p">{</span>
</span><span class='line'>  <span class="p">[[</span><span class="n">FBSDKApplicationDelegate</span> <span class="n">sharedInstance</span><span class="p">]</span> <span class="nl">application</span><span class="p">:</span><span class="n">application</span>
</span><span class='line'>    <span class="nl">didFinishLaunchingWithOptions</span><span class="p">:</span><span class="n">launchOptions</span><span class="p">];</span>
</span><span class='line'>  <span class="c1">// Add any custom logic here.</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">YES</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">application:</span><span class="p">(</span><span class="bp">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="nf">openURL:</span><span class="p">(</span><span class="bp">NSURL</span> <span class="o">*</span><span class="p">)</span><span class="nv">url</span> <span class="nf">sourceApplication:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">sourceApplication</span> <span class="nf">annotation:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">annotation</span> <span class="p">{</span>
</span><span class='line'>  <span class="kt">BOOL</span> <span class="n">handled</span> <span class="o">=</span> <span class="p">[[</span><span class="n">FBSDKApplicationDelegate</span> <span class="n">sharedInstance</span><span class="p">]</span> <span class="nl">application</span><span class="p">:</span><span class="n">application</span>
</span><span class='line'>    <span class="nl">openURL</span><span class="p">:</span><span class="n">url</span>
</span><span class='line'>    <span class="nl">sourceApplication</span><span class="p">:</span><span class="n">sourceApplication</span>
</span><span class='line'>    <span class="nl">annotation</span><span class="p">:</span><span class="n">annotation</span>
</span><span class='line'>  <span class="p">];</span>
</span><span class='line'>  <span class="c1">// Add any custom logic here.</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">handled</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>Branch</strong>. From their <a href="https://dev.branch.io/getting-started/sdk-integration-guide/guide/ios/#start-a-branch-session">docs</a>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="c1">// Respond to URI scheme links</span>
</span><span class='line'><span class="p">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">application:</span><span class="p">(</span><span class="bp">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="nf">openURL:</span><span class="p">(</span><span class="bp">NSURL</span> <span class="o">*</span><span class="p">)</span><span class="nv">url</span> <span class="nf">sourceApplication:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">sourceApplication</span> <span class="nf">annotation:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">annotation</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// pass the url to the handle deep link call</span>
</span><span class='line'>    <span class="p">[[</span><span class="n">Branch</span> <span class="n">getInstance</span><span class="p">]</span> <span class="nl">handleDeepLink</span><span class="p">:</span><span class="n">url</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// do other deep link routing for the Facebook SDK, Pinterest SDK, etc</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">YES</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Respond to Universal Links</span>
</span><span class='line'><span class="p">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">application:</span><span class="p">(</span><span class="bp">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="nf">continueUserActivity:</span><span class="p">(</span><span class="n">NSUserActivity</span> <span class="o">*</span><span class="p">)</span><span class="nv">userActivity</span> <span class="nf">restorationHandler:</span><span class="p">(</span><span class="kt">void</span> <span class="p">(</span><span class="o">^</span><span class="p">)(</span><span class="bp">NSArray</span> <span class="o">*</span><span class="n">restorableObjects</span><span class="p">))</span><span class="nv">restorationHandler</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">BOOL</span> <span class="n">handledByBranch</span> <span class="o">=</span> <span class="p">[[</span><span class="n">Branch</span> <span class="n">getInstance</span><span class="p">]</span> <span class="nl">continueUserActivity</span><span class="p">:</span><span class="n">userActivity</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="n">handledByBranch</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Both SDKs provide you with a naive example without explaining what the return results are, and the eventuality of an application doing <strong>more</strong> work than actually using their SDK. Seems a very contrived example but consider that a clever engineer will not copy-paste your example code, but integrate it into their application where it fits best.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Xcode 7.3 analyzer improvements]]></title>
    <link href="http://miqu.me/blog/2016/01/21/xcode-7-dot-3-analyzer-improvements/"/>
    <updated>2016-01-21T20:39:53+00:00</updated>
    <id>http://miqu.me/blog/2016/01/21/xcode-7-dot-3-analyzer-improvements</id>
    <content type="html"><![CDATA[<p>The <a href="https://developer.apple.com/xcode/download/">latest xcode beta</a> is available for downloading. There are many improvements, and the usual problems with stuff that stops working&hellip; Ah the love of Apple for it&rsquo;s developers :). But I want to tell you about a feature that caught my attention: <strong>New analyzer settings!</strong></p>

<h1>New analyzer settings</h1>

<p>From the <a href="http://adcdownload.apple.com/Developer_Tools/Xcode_7.3_beta/Xcode_7.3_beta_Release_Notes.pdf">release notes</a>:</p>

<blockquote><p>The static analyzer warns when nil is returned from a method or function with a nonnull return
type or is passed to a parameter marked nonnull.</p>

<p>The static analyzer checks for common misuses of Objective-C generics.</p>

<p>The static analyzer checks for missing localizability. This check is off by default and can be
enabled by selecting ‘Yes’ for ‘Missing localizability’ in the &lsquo;Static Analyzer - Generic Issues’ build
settings.</p></blockquote>

<p>There are 3 new settings related to these new features: CLANG_ANALYZER_NONNULL, CLANG_ANALYZER_OBJC_GENERICS  and CLANG_ANALYZER_LOCALIZABILITY.</p>

<h1>CLANG_ANALYZER_NONNULL and CLANG_ANALYZER_OBJC_GENERICS</h1>

<p>Objective-C continues to get some love! Adding into the changes for <a href="http://miqu.me/blog/2015/04/17/adopting-nullability-annotations/">nullability</a> and <a href="http://miqu.me/blog/2015/06/09/adopting-objectivec-generics/">generics</a>, now the analyzer will start to warn you about usage not in line with your annotations. That&rsquo;s very good news, considering that the compiler checks were a bit lacking. Additional kudos because both settings are enabled by default.</p>

<h1>CLANG_ANALYZER_LOCALIZABILITY</h1>

<p>This is an intriguing one. Let&rsquo;s investigate further; Let&rsquo;s say you have some code where you set a string to a label:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="n">label</span><span class="p">.</span><span class="n">text</span> <span class="o">=</span> <span class="s">@&quot;not-localized&quot;</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>You&rsquo;ll now get a nice analyzer warning: &ldquo;User-facing text should use localized string macro&rdquo;</p>

<p><img src="http://miqu.me/images/posts/user-facing-warning.png" alt="warning1" /></p>

<p>But you won&rsquo;t get a warning when the key is not localized. Like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="n">label</span><span class="p">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">NSLocalizedString</span><span class="p">(</span><span class="s">@&quot;not-localized&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Hey, we can&rsquo;t have everything, but here&rsquo;s my <a href="http://openradar.appspot.com/radar?id=4993758374395904">request for improvement submitted</a>.</p>

<p>Anyway, go ahead and enable these new settings! And use the analyzer!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Transitioning iOS versions]]></title>
    <link href="http://miqu.me/blog/2015/12/14/transitioning-ios-versions/"/>
    <updated>2015-12-14T20:00:25+00:00</updated>
    <id>http://miqu.me/blog/2015/12/14/transitioning-ios-versions</id>
    <content type="html"><![CDATA[<p>Having worked in companies developing their own products, I&rsquo;ve faced many times the problem developers encounter when building iOS applications: Your biggest 3rd party dependency - the OS SDK - is changing under you every year. How does your team deal with this in a maintainable way?</p>

<!-- more -->


<h1>Long-term projects</h1>

<p>Unless you do consulting or freelancing, chances are that you will have to deal repeatedly with evolving code in the same project over the years. Even if you jump from project to project, chances are you land into existing projects where there&rsquo;s some code written some time ago.</p>

<p>In longer projects <a href="https://en.wikipedia.org/wiki/Software_rot">software rot</a> stays and the team needs to deal with it. A successful application will face this earlier than expected, and in the fast-paced market of mobile applications, every year Apple is renovating and ruthlessly changing the  Operating System where our code lives. It requires non-trivial amount of work to keep up with changes, while still delivering improvements and features for our users.</p>

<p>I&rsquo;ve worked in teams where these questions were often raised:</p>

<ul>
<li>How do we keep modernising our code, while maintaining backwards compatibility for our users?</li>
<li>What techniques can be use to keep moving fast, while still supporting older OS versions?</li>
<li>How do we approach throwing away old code using deprecated APIs in a big codebase?</li>
<li>How do we write new code taking into account that we will remove support of an older OS version?</li>
</ul>


<h2>Fragmentation</h2>

<p><a href="http://miqu.me/blog/2015/05/27/fragmentation/">As I wrote before</a>, developers in the Apple ecosystem face a different kind of fragmentation. We&rsquo;re forced to adopt breaking changes every year, or &lsquo;die&rsquo; in the process. Apple forces us to start using new features of the OS, and has a policy of deprecating APIs a lot faster than we like. Instead of supporting a lot of older devices and operating systems, we face many breaking changes every year.</p>

<p>A mobile engineering team should embrace change, knowing they&rsquo;ll need to assign some engineering resources to keep up with code changes required to support new and old OS versions. Otherwise the code will rot faster than expected. (Anybody heard about complete rewrite of a feature just written a year ago?).</p>

<p>Apple recommends maintaining support for the current iOS version and the previous one. At the time of this writing this would be iOS9 and iOS8. Even if your business allows for this rule, you&rsquo;re going to have a transitioning period while you update the application code, and still deliver new features and improvements.</p>

<p>I&rsquo;m going to share with you what in my opinion are best practices to keep up with OS upgrades.</p>

<h1>Transitioning iOS versions gracefully</h1>

<p>I would divide best practices in 3 categories.</p>

<ul>
<li>SDK use</li>
<li>Language: Swift / Objective-C</li>
<li>Maintenance of previous releases</li>
</ul>


<p>Let&rsquo;s review each of them in order.</p>

<h2>SDK use</h2>

<h3>Treat deprecated APIs as errors to resolve</h3>

<p>I think we should always treat warnings as errors, and deprecated API use is no exception. If this is not managed around the release of the new OS version, chances are it will take time for the team to keep up with new API. It&rsquo;s best to modernise code to use newer API in the face of deprecations, than leaving warnings as <em>TODO</em> markers for later.</p>

<h3>At runtime, check for OS versions</h3>

<p>For long time, Apple&rsquo;s <a href="https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-1114537-BABHHJBC">recommended</a> way of checking for API existence has been using <code>respondsToSelector</code>, <code>[SomeClass class]</code>, or <code>NSClassFromString</code>. There&rsquo;s several pitfalls related to runtime checks and so many variations depending on what you are checking for. I don&rsquo;t see any benefit at all to check for methods, classes etc. Apple introduced availability checks for Swift this year, acknowledging and improving over the old practice around ObjC&rsquo;s flexibility. I think we should change our practices and check only for OS version in Objective-C.</p>

<p>Starting with iOS 8 you can use <code>NSProcessInfo</code> to accurately fetch the OS version your app is running on. Use it in your macros, and never again check for API availability for <code>NSClassFromString</code>, <code>instancesRespondToSelector</code> etc. I like to use this to &lsquo;mark&rsquo; code, so we can keep removing legacy code paths. See next tip.</p>

<h3>ObjC: Mark legacy code paths with macros</h3>

<p>The problem with maintaining or having exceptions in the code for older versions of the OS, is that in the future that code will be dead, as your team will eventually drop support of that OS version. Say you still support iOS 7 today, and you need to implement use of newer API to convert points from one view coordinate system to another. In iOS8 a new API was introduced, and the previous one was not (yet) deprecated. You might want to migrate to use it when it is available. In swift, you can use <em>availability conditions</em> to check at compile-time:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">if</span> <span class="err">#</span><span class="n">available</span><span class="p">(</span><span class="n">iOS</span> <span class="mi">8</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="o">*</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nb">self</span><span class="p">.</span><span class="n">view</span><span class="p">.</span><span class="n">convertPoint</span><span class="p">(.</span><span class="n">Zero</span><span class="p">,</span> <span class="nl">toCoordinateSpace</span><span class="p">:</span><span class="n">anotherView</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>  <span class="nb">self</span><span class="p">.</span><span class="n">view</span><span class="p">.</span><span class="n">convertPoint</span><span class="p">(</span><span class="n">CGPointZero</span><span class="p">,</span> <span class="nl">toView</span><span class="p">:</span><span class="n">anotherView</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>When the time comes to drop iOS8, possibly next year, you&rsquo;ll just search the whole project for <code>#available(iOS8</code> checks, and remove the old branch, and removing the check. This cleans the code and clears it of old checks.</p>

<p>You can do the same with Objective-C, defining a function or macro that will determine the OS version at runtime. Furthermore, there may be times where you need to introduce a workaround in the code, related to an older OS version. It is useful to use the same macro to &lsquo;mark&rsquo; the code so when you stop supporting the older OS version, you just need to check for that marker. For example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="c1">//Assume you have a macro that determines OS version, like MQG_OSVERSION_IOS7</span>
</span><span class='line'><span class="c1">//Mark the code with the macro, doing nothing, just where you are doing a workaround that needs to</span>
</span><span class='line'><span class="c1">//be reviewed when you drop iOS7 support</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">MQG_OSVERSION_IOS7</span><span class="p">;</span>
</span><span class='line'>  <span class="c1">// Do your stuff, which only is needed under iOS7, but you still do for all OS versions</span>
</span><span class='line'>  <span class="c1">// like implementing a delegate method that is not needed for iOS8 or greater, etc</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Control the number of 3rd party libraries</h3>

<p>Any 3rd party library that your project contains adds a cost to your project. If - most likely <em>when</em> - Apple breaks compatibility with an OS release, the maintainer may not be as fast as you to adapt to changes.</p>

<p>Be mindful of this cost when considering a new library, and always be ready to step in and help the maintainer by pushing changes upstream. If you can&rsquo;t afford to do this, chances are your team will suffer every year when updating all dependencies.</p>

<h2>Objective-C</h2>

<p>Most possibly your project still has lots of Objective-C code. And it&rsquo;s this code that needs special care, as the team will need to continue living with it, even in the face of Swift.</p>

<h3>Modernise all Objective-C code</h3>

<p>If you have such old code still using pre-arc code, please refer to <a href="https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html">this</a> guide. If you still have pre-2.0 Objective-C code, Xcode has a tool that will let you modernise it in minutes.</p>

<p>There&rsquo;s two important changes to the language since Objective-C 2.0, introduced in 2015; That is the addition of <a href="http://miqu.me/blog/2015/05/27/fragmentation/">nullability annotations</a> and <a href="http://miqu.me/blog/2015/06/09/adopting-objectivec-generics/">lightweight generics</a>.</p>

<p>Be sure to use these features in your new Objective-C code, and change APIs of existing code to annotate it for the use from Swift. It will also make you think about exiting code, and possibly encounter bugs! The most important aspect of doing this is that it will ease using older code from Swift, without the need for a rewrite.</p>

<h3>Rewrites in Swift</h3>

<p>The future of Cocoa development is Swift, and any application-level Objective-C code is destined to die sooner rather than later. If you can afford to slowly rewrite part of your application using Swift, just do it. How to organise rewrites is a subject of a whole new article, so I won&rsquo;t give an opinion on this just now.</p>

<h3>Keep code templates up to date</h3>

<p>It might seem unimportant, but your team should have up to date code templates. With the recent changes to the language, unfortunately Apple has not spent the time to change templates for Objective-C.  I find very annoying that the default templates for Objective-C don&rsquo;t annotate the code with <code>NS_ASSUME_NONNULL_BEGIN</code> and <code>NS_ASSUME_NONNULL_END</code> macros. So if you are still writing with that language it&rsquo;s better your team takes control of all templates and use modern ones.</p>

<h2>Swift</h2>

<p>Swift is changing a lot, but it is <a href="http://swift.org">open source</a>. You can check the roadmap and be ready for adoption from day one, when the new version is released as part of Apple tools.</p>

<p>Not modernising to newer Swift code is not an option. Mainly because there are breaking changes, so it will incur a cost on your development time, which is hopefully returned in developer happiness for using the language - The current state of tooling in Swift makes the experience sometimes slower, so no win there ;).</p>

<p>To keep up to date to Swift developments, be sure that somebody in the development team is following at least to the <a href="https://lists.swift.org/mailman/listinfo/swift-evolution-announce">evolution announce</a> mailing list, and review the <a href="https://github.com/apple/swift-evolution">evolution repository</a>.</p>

<h2>Maintenance of previous releases</h2>

<h3>Continous integration</h3>

<p>In my experience, I&rsquo;ve found that to keep the development speed, the team generally needs to have access to 3 development environments in CI, available for any build:</p>

<ul>
<li>Current stable version of Xcode</li>
<li>Beta version of Xcode</li>
<li>Previous version of Xcode (temporarily)</li>
</ul>


<p>Generally all builds should be scheduled to run against the stable version of Xcode. Apple is moving fast, and there generally is one new beta version of Xcode available. That version may or may not have breaking changes to your project.</p>

<p>Sometimes, the team will find it necessary to build with a previous version of Xcode, for example to be able to deploy a hot fix for an application submitted with it.</p>

<h3>Cocoapods in repository</h3>

<p>Different teams have different approaches to dependencies. If you use cocoapods, most likely you&rsquo;ll want dependencies checked in your repository project. Even though there&rsquo;s disadvantages, the biggest advantage is that you&rsquo;ll be able to build previous releases out of a tag. This helps obviously for maintenance reasons, but also to be able to do git bisect reliably. Take this into consideration when deciding if your dependencies should be checked in the repository.</p>

<h1>Conclusion</h1>

<p>Maintaining software projects is hard, and if you are working with a team with a successful product (you should!), then most likely you&rsquo;ll maintain it over time. I&rsquo;ve shared what I think are good practices to maintain code across different iOS versions, with the minimal effort involved. I hope you find it useful.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Building Swift on OSX]]></title>
    <link href="http://miqu.me/blog/2015/12/05/building-swift-on-osx/"/>
    <updated>2015-12-05T00:38:35+00:00</updated>
    <id>http://miqu.me/blog/2015/12/05/building-swift-on-osx</id>
    <content type="html"><![CDATA[<p>Swift is finally <a href="http://swift.org">open source</a>! This is great because of many reasons, namely the transparency and chances to learn from the people responsible of building the foundational language you work on. I&rsquo;ve been checking how easy was to build the runtime, REPL and compiler, and it turns out the team made an excellent job to make the process very easy. Let&rsquo;s see how to do it.</p>

<!-- more -->


<p>If you ever want to use a bleeding edge version of swift to build your application, now you can! Bear in mind that you should only use development versions of the compiler and runtime only when you are sure you need to do this. Generally as an application developer you rarely need to do so. But hey, let&rsquo;s dive in!</p>

<p>The documentation is very good. Again the team has done an excellent job in my opinion, setting up a full open source setup for the years to come. So I encourage you to read at least the <a href="https://swift.org/getting-started/">getting started</a> and <a href="https://github.com/apple/swift/blob/master/README.md">readme</a> first.</p>

<h1>Downloading a binary development snapshot</h1>

<p>If you don&rsquo;t want to build from sources, you can grab the latest snapshot as described on the <a href="https://swift.org/download/">swift download page</a>. It&rsquo;s certainly an option if for example you have an application written in Swift, and you want to assess how much things will change ahead of next Xcode release. You can check <a href="http://ericasadun.com/2015/12/03/how-to-compile-with-xcode-and-swift-2-2/">Erica Sadun&rsquo;s post</a> if you want more details. Thanks <a href="https://twitter.com/ericasadun">Erica</a> for sharing!</p>

<h1>Building from sources</h1>

<p>I just needed to install two dependencies on my machine, as stated in the repository: cmake and ninja. Be sure to set this up before starting a build. I&rsquo;m using homebrew:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>brew install cmake
</span><span class='line'>brew install ninja
</span></code></pre></td></tr></table></div></figure>


<p>I chose to use ninja for the first time I would build Swift, as it is the default build system, and I wouldn&rsquo;t need to change configuration parameters for the build script.</p>

<p>Once you&rsquo;ve installed the dependencies, clone the required repositories. Bear in mind that the default build script will try to look for more than just the swift repository. As described in the swift <a href="https://github.com/apple/swift/blob/master/README.md">README</a>. So you&rsquo;ll need to clone several repos:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'> mkdir apple
</span><span class='line'> git clone git@github.com:apple/swift.git swift
</span><span class='line'> git clone git@github.com:apple/swift-llvm.git llvm
</span><span class='line'> git clone git@github.com:apple/swift-clang.git clang
</span><span class='line'> git clone git@github.com:apple/swift-lldb.git lldb
</span><span class='line'> git clone git@github.com:apple/swift-cmark.git cmark
</span><span class='line'> git clone git@github.com:apple/swift-llbuild.git llbuild
</span><span class='line'> git clone git@github.com:apple/swift-package-manager.git swiftpm
</span><span class='line'> git clone git@github.com:apple/swift-corelibs-xctest.git
</span><span class='line'> git clone git@github.com:apple/swift-corelibs-foundation.git
</span></code></pre></td></tr></table></div></figure>


<p>Once all repositories are cloned, you can build from the main swift repository, with the default configuration. The Swift team included a wrapper script to make compilation really easy. That script will be using CMake and ninja to build the actual binaries and libraries. Just use the default configuration (using ninja), to see if you can successfully build with your machine:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>./swift/utils/build-script
</span></code></pre></td></tr></table></div></figure>


<p>With the default arguments, you&rsquo;ll find the build artifacts in a &lsquo;build&rsquo; folder so go and explore. To see the version of the newly built swift compiler. So, go one directory upwards, and find the built binaries ready to use:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nb">cd </span>build/Ninja-DebugAssert/swift-macosx-x86_64/bin
</span><span class='line'>./swift --version
</span><span class='line'><span class="c">#Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift c959ce2c83)</span>
</span><span class='line'><span class="c">#Target: x86_64-apple-macosx10.9</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>Success!</strong></p>

<h1>Happy learning!</h1>

<p>I quite liked the quote on the <a href="https://github.com/apple/swift/blob/master/utils/build-script#L261">build-script</a>, where a rationale is presented behind the decision to have a unique simple wrapper script to build the toolchain.</p>

<blockquote><p>While you can invoke CMake directly to build Swift, this tool will save you
time by taking away the mechanical parts of the process, providing you controls
for the important options.</p>

<p>For all automated build environments, this tool is regarded as <em>the</em> <em>only</em> way
to build Swift.  This is not a technical limitation of the Swift build system.
It is a policy decision aimed at making the builds uniform across all
environments and easily reproducible by engineers who are not familiar with the
details of the setups of other systems or automated environments</p></blockquote>

<p>Even somebody with basic knowledge and unexperienced with <em>compilers</em> like myself, could get up and running with a new swift build in very little time.</p>

<p>The release of open source code so close to our day to day jobs is a very special thing for us developers. Compiling swift and the tools is very easy, which just shows the high technical leadership and high quality of the open source release by the team at Apple. Congrats! This will be an amazing journey, for all of us.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Three months with XVim]]></title>
    <link href="http://miqu.me/blog/2015/11/19/three-months-with-xvim/"/>
    <updated>2015-11-19T22:19:24+00:00</updated>
    <id>http://miqu.me/blog/2015/11/19/three-months-with-xvim</id>
    <content type="html"><![CDATA[<p>&lsquo;Know your key shortcuts&rsquo; is the mantra of the productive programmer. I&rsquo;ve chosen to try using the same keybindings everywhere, investing time and learning Vim. And using <a href="https://github.com/XVimProject/XVim">xvim</a> in Xcode. In this article I&rsquo;m going to try to explain why I did this, and I&rsquo;ll share my experience of the process.</p>

<!-- more -->


<p>Knowing your IDE and being productive in it, is one of the best investments you can do as a programmer. We need to write code, and sometimes we do repetitive tasks if we are involved in bigger projects: Navigation and search, refactoring, some formatting and changing existing code.</p>

<h1>Vi(m)</h1>

<p>vi is a command-line text editor. <a href="https://en.wikipedia.org/wiki/Vim_(text_editor)">Vim</a> is an extended version of the same editor.</p>

<p>The main difference from &lsquo;more modern&rsquo; editors, is that it has two modes, and it&rsquo;s designed for proficiency in a kind of command language where the user specifies where he wants to navigate and what he wants to achieve. It was also designed to be completely controlled with the keyboard.</p>

<p>Many people still use it nowadays, which is surprising given that it looks rather old-fashioned, and that the proliferation of different text editing modes has been massive.</p>

<p>If you&rsquo;re interested but not familiar with the editor, you&rsquo;ll find that learning how to use it will be a significant time commitment. There&rsquo;s countless of sources of developers explaining why or why not they&rsquo;d use such editor; you&rsquo;ll find them easily. The sources that inspired me and I go back to from time to time are:</p>

<ul>
<li><a href="http://www.viemu.com/a-why-vi-vim.html">Why, by Jon Beltran de Heredia</a>: Very old source but explanatory for somebody not familiar with the editor.</li>
<li><a href="https://danielmiessler.com/study/vim/">Daniel Miessler&rsquo;s intro</a>: Very well written examples and thinking mode that goes with the editor.</li>
</ul>


<h1>Investments</h1>

<p>Getting familiar and productive with your IDE is a time investment. It can take you from weeks to years, and it&rsquo;s very recommended you do so, because it&rsquo;s rather useful to produce code with less time to have time for other interesting tasks (or coffee!). If you become fast enough, your thoughts will <em>convert</em> to code almost instantly, delaying the time between thinking what to do and actually doing. This is specially important when dealing with many files or a moderate amount of complexity.</p>

<p>About 3 years ago, I moved all my workflows to command-line. So any use of git, external text file editing, or remote server management, I would do through command line. For the command-line interface I chose vim as my  editor. But I never learnt more than the modes and clumsy editing in one page of text. I would always rely on external editors to do the bulk of my work.</p>

<p>A year ago I was already familiar with Xcode and it&rsquo;s keybindings. I was moderately productive with it, but I felt I had to improve. The main thing I noticed was my constant switching to the mouse to navigate within a file. Being also a user of <a href="https://www.jetbrains.com/objc/">AppCode</a>, I thought my vanilla Xcode experience was not very productive.</p>

<p>And so I concluded why not try and invest more time with vim, given I would not only use it for iOS development? I installed <a href="https://github.com/XVimProject/XVim">xvim</a> plugin for Xcode 3 months ago, and I don&rsquo;t want to go back to vanilla Xcode. I&rsquo;ll try and give you an overview.</p>

<h1>Pros</h1>

<p>Being proficient with vim has this implications:</p>

<ul>
<li>Hands mostly not moving from keyboard for much time; even switching between files can be easily done with keystrokes, so you can move around the project only with keyboard</li>
<li>It may seem obvious, but having control of the whole project with keyboard empowers you as a programmer, makes you focus more, and lets you reach parts of code faster</li>
<li>One muscle memory: I&rsquo;m using vim mode in <a href="http://atom.io">other editors</a>. This means I have to remember less keystrokes. The muscle memory is so strong that I miss it in text editors like gmail. I even started using <a href="https://vimium.github.io/">vimium</a>!</li>
<li>With one muscle memory, as I use command line a lot, so I will edit very fast and go back to the IDE, with no loss of focus.</li>
<li>Many other editors have a kind of vim mode available as plugin. So it is a good investment in my opinion.</li>
</ul>


<h1>Cons</h1>

<p>This may be obvious, but the drawbacks of learning vim:</p>

<ul>
<li>Learning curve: I&rsquo;m only scratching the surface and I&rsquo;ve constantly forced myself to use Vim keybindings 100% of my programming time. There&rsquo;s so much more to learn, and it has very steep learning curve</li>
<li>Integration with IDE: It can sometimes be rocky. My experience with the Xcode plugin has been very positive, but I have had crashes here and there. There can also be some key binding conflicts</li>
<li>Not complete vim: Nothing beats the original editor in terms of functionality and customisation. Using vim this way is a middle ground, but for me personally is a necessary step to continue working with my existing tools, and still learn a new way of moving and editing</li>
</ul>


<h1>Learning process</h1>

<p>As I mentioned, learning vim is a big time investment. It will slow you down at the beginning, but if you are convinced that you want to invest time on it, you&rsquo;ll prevail.</p>

<p>Everybody learns differently, so you must find your way. My suggestions are:</p>

<ul>
<li>Print and consult <a href="http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html">graphical Vim cheatsheet</a></li>
<li>Force yourself to only use Vim keybindings for at least a week</li>
<li>Read some introductory posts or books. The ones I mentioned before helped me a lot</li>
</ul>


<h1>The next step</h1>

<p>There&rsquo;s much you can customise and learn. For now I&rsquo;ll try to be faster and more proficient with navigating and editing Objective-C and Swift code. When I&rsquo;m really productive, I&rsquo;ll try and learn more advanced features, mainly graphical mode and macros.</p>

<p>I hope you found the article interesting, and that you also are improving your tooling and productivity as much as I am. <a href="https://twitter.com/miguelquinon">Tell me</a> what you think!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Jumping into existing codebases]]></title>
    <link href="http://miqu.me/blog/2015/11/15/jumping-into-existing-codebases/"/>
    <updated>2015-11-15T18:55:18+00:00</updated>
    <id>http://miqu.me/blog/2015/11/15/jumping-into-existing-codebases</id>
    <content type="html"><![CDATA[<p>I’ve recently changed workplace - <a href="http://peak.net">we’re hiring!</a> - and I’ve jumped into an existing codebase. This is a good time to review what I like to do when I start to work on a project in these circumstances.</p>

<!-- more -->


<p>When you don&rsquo;t start a greenfield project, which is most of the time for many developers, you need to rapidly and effectively get to grasp with all the code, so you can become productive and acknowledgeable with any changes that need to be done. There&rsquo;s some things I always do in these cases.</p>

<h1>Examining the repository</h1>

<h2>Git stats</h2>

<p>I always like to start with the repository itself. Looking at the history, commits and overall activity. Many times you will find that developers are not used to providing consistent and meaningful commit messages (how many times you’ve seem <strong>WIP feature X</strong> commit message?). So looking at the history is not informative. So I prefer to look at global statistics instead.</p>

<p>Assuming you will work with a git repository, the first tool I like to use is <a href="https://github.com/tomgi/git_stats">git stats</a>. As easy as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>gem install git_stats
</span><span class='line'><span class="nb">cd</span> &lt;repo&gt;
</span><span class='line'>git_stats generate
</span></code></pre></td></tr></table></div></figure>


<p>Running this tool will result in a nice html document giving insights about authors, commits, files and lines of code. It’s mostly informative but it’s good to get an overview of the key collaborators of a project, who created, and most importantly, who deleted the most lines of code!</p>

<h3>Gource</h3>

<p>A very interesting way to see the commit history in a graphical way is <a href="http://gource.io">gource</a>. I always like to run it for 5 minutes just to get a feel of the rush of the project, and basically to see
in a glance what are the core parts of the codebase. It also is very funny to look at, and you can get a feel what is the core part of the repository, what parts changed the most, and the number of developers working at the same time. It&rsquo;s available via <a href="http://brew.sh">homebrew</a> as well so very easy to install and run:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>brew install gource
</span><span class='line'><span class="nb">cd</span> &lt;repo&gt;
</span><span class='line'>gource
</span></code></pre></td></tr></table></div></figure>


<p>Check the available options, like <code>seconds-per-day</code>, to customize how you see the animation. I like to run it with 2 seconds per day, to make the visualization move faster (and be funnier to watch!).</p>

<h2>Commit messages</h2>

<p>I like to just do a <code>git log</code> in terminal and understand if the team has any kind of agreement on what and how commit messages are structured. Having good commit messages beats any amount of commented code kept around and is a very integral part of the project.</p>

<p>There has been plenty of discussion on the subject, I’m just going to link here 2 of my favorite articles: <a href="http://chris.beams.io/posts/git-commit/">this</a> and <a href="https://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message">this one</a>. I don’t adhere to all the points presented but agree to many of them. You’ll get an overview of how a project has evolved by looking at some logs, specially from the beginning. Just do:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git log --reverse
</span></code></pre></td></tr></table></div></figure>


<p>Having good commit messages helps a lot when dealing with bug fixes, using <a href="https://git-scm.com/docs/git-bisect">git bisect</a>. When you&rsquo;re the new one in a project you don&rsquo;t have the domain and code knowledge to understand what changed or debug, you can save hours of pain for  bug fixes by using it, and when you nail down the commit that broke something, you can understand the motivation behind it much better.</p>

<h2>Structure</h2>

<p>Just a quick glance of the repository structure can give you a feel of the (lack of) organization and practices of the team. Step number 2 after checking commit logs is browsing the project structure. I&rsquo;m not going to judge or give opinion on what project structure should be the best, as it very much depends on the project, but I generally will try to identify these kind of groups:</p>

<ul>
<li>Code</li>
<li>Assets</li>
<li>Submodules or dependencies</li>
<li>Utility scripts</li>
</ul>


<p>If the repository does not even contain separate folders for this then you know what is your first task!</p>

<h2>Dependency management</h2>

<p>Once I&rsquo;ve checked the commit logs, and project structure, before I move on to the code, my last step is to understand the strategy for dependency management. As I work on iOS projects, let&rsquo;s see the options for these:</p>

<ul>
<li>Pre-built libraries</li>
<li>Copied code (ideally in a sub folder)</li>
<li>Submodule</li>
<li>Cocoapods</li>
<li>Carthage</li>
</ul>


<p>Every project will have a combination of strategies, as many times libraries come from various sources. But if there&rsquo;s a chance, I like to suggest and improve this if possible, and unify how libraries are managed in the project. My opinion is that it&rsquo;s easier to manage dependencies if they are contained and imported the same way.</p>

<p>Many projects will use Cocoapods as the dependency management, even for internal libraries. If that is the case, my last stop is the Podfile. If the team hasn&rsquo;t yet, I just suggest to fix the version for all but internal libraries. This is a practice I consider a must if working with 3rd party libraries, as many unexpected bugs can occur if you import libraries with relaxed version requirements, or just the latest one.</p>

<h1>Code</h1>

<p>It is said that a programmer spends most of time reading code rather than writing it, and it will be specially true when you jump into an existing codebase.</p>

<p>Reading (and understanding) the code of an existing project is very daunting at first. How much will take you to understand the fundamental bits depends on many factors, including domain knowledge, code style, code organization and background of the key project developers.</p>

<p>Before diving into specifics of the code, I like to get an overview:</p>

<ul>
<li>Lines of code tool</li>
<li>Dependency visualiser</li>
</ul>


<h2>Sloccount</h2>

<p><a href="http://www.dwheeler.com/sloccount/">This tool</a> will give you a summary of lines of code in the repository. You will get a feel of overall complexity of code, and if you have source code for dependencies, you can also check which are the bigger dependencies in terms of code size.</p>

<p>Once you take part in more than one project you&rsquo;ll be able to get a feel when a project is <em>big</em> or <em>small</em>, which is very subjective, but useful to know, when you approach new code.</p>

<p>This tool is again available via <a href="http://brew.sh">homebrew</a>, so it&rsquo;s very easy to run in any project:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>brew install sloccount
</span><span class='line'>sloccount &lt;source_directory&gt;
</span></code></pre></td></tr></table></div></figure>


<h2>Dependency visualiser</h2>

<p>The biggest problem of reading new code is that everything is kind of new: Domain, code style, requirements, patterns. A very good way to get a grasp of the code structure is visualise the relationships. I know of two tools that do that for Objective-C: <a href="https://github.com/PaulTaykalo/objc-dependency-visualizer">objc-dependency-visualizer</a> and <a href="https://github.com/nst/objc_dep">objc_dep</a>.</p>

<p>I quite like <a href="https://github.com/PaulTaykalo/objc-dependency-visualizer">objc-dependency-visualizer</a> because it&rsquo;s easier to use to get a broad picture of relative code size between classes and relationships. Just be warned that for medium to big projects the visualisations are just messy. That&rsquo;s when you can use <a href="https://github.com/nst/objc_dep">obj_dep</a> to get graphs for specific files or modules.</p>

<h2>iOS projects</h2>

<p>Before I go into reading specific code, I do these checks first:</p>

<ul>
<li>Run static analyser</li>
<li>Compile project and see warnings</li>
<li>Basic objc good practices (swift is very new for practices yet)</li>
</ul>


<h3>Static analyser</h3>

<p>I will run the static analyser, which is the part of the toolchain that will generally be overlooked by most teams. Developers don&rsquo;t trust analyser because it can give false positives, but in my opinion clean code should have 0 analyser warnings.</p>

<h3>Compile warnings</h3>

<p>With Objective-C and Swift, we get a program to check our code before it&rsquo;s even run. Many see the compiler as a nuisance, specially those used to interpreted languages. I will run a compilation first to see if the project has warnings, treats warnings as errors, and how many outstanding warnings the team is used to have. I consider Jon Reid&rsquo;s <a href="https://github.com/jonreid/XcodeWarnings">xcconfig</a> a very good base line for any project.</p>

<p>Ideally I would like to work on code with most warnings turned on, and warnings treated as error, but this is not the case for many projects out there. The general problem with Xcode is that it creates projects with very lax default warning settings. So many developers don&rsquo;t bother changing them, thus having an unsafe environment for the project from the beginning.</p>

<p>If you start a new project, I&rsquo;d recommend using a tool to configure properly the project, like <a href="https://github.com/thoughtbot/liftoff">liftoff</a> or <a href="https://github.com/krzysztofzablocki/crafter">crafter</a>. For project warnings, I personally prefer to have a .xcconfig file because it&rsquo;s more readable, maintainable, and allows for comments of disabled warnings. See <a href="https://github.com/jonreid/XcodeWarnings">xcconfig</a> or <a href="https://github.com/jspahrsummers/xcconfigs">xcconfigs</a> for examples.</p>

<h3>Basic good practices</h3>

<p>My last step when surveying new code, is checking the safety of the code. I&rsquo;ll check for basic safe usage of Objective-C which is a very punishing language with lots of sharp edges that can cut the unaware developer ;)</p>

<ul>
<li>Safe use of BOOL: See <a href="http://nshipster.com/bool/">http://nshipster.com/bool/</a></li>
<li>Correct use of copy: For classes having mutable subclasses - NSArray, NSDictionary, NSString, NSData&hellip;</li>
<li>Safe use of blocks: Checking for nil block parameters</li>
<li>Memory leaks and blocks: Has the code a simple definition of <a href="https://github.com/jspahrsummers/libextobjc/blob/master/extobjc/EXTScope.h">@weakify and @strongify</a>, or dealing with retain cycles with blocks in a similar way?</li>
<li>Modern code: Uses <a href="http://miqu.me/blog/2015/04/17/adopting-nullability-annotations/">nullability</a> and <a href="http://miqu.me/blog/2015/06/09/adopting-objectivec-generics/">generics</a>?</li>
</ul>


<h1>Start coding!</h1>

<p>I hope you get some useful tips from my review of tools and checks I do when jumping into new projects. I you&rsquo;ve got any more, <a href="https://twitter.com/miguelquinon">tell me!</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Functional programming in the small, with Swift]]></title>
    <link href="http://miqu.me/blog/2015/09/09/functional-programming-in-the-small-with-swift/"/>
    <updated>2015-09-09T15:00:23+00:00</updated>
    <id>http://miqu.me/blog/2015/09/09/functional-programming-in-the-small-with-swift</id>
    <content type="html"><![CDATA[<p>In a <a href="http://miqu.me/blog/2015/08/31/poor-mans-functional-programming/">previous post</a>, I wrote about the choice of paradigms that Swift presents for it&rsquo;s target audience. I also gave my opinion on how people with different backgrounds might see and approach the Functional Programming paradigm.</p>

<p>In this post I want to share how I started to take advantage of FP with Swift for the practical domain of iOS developers: Building apps.</p>

<!-- more -->


<h1>Silver bullets</h1>

<p>In software development there are no silver bullets to kill your problems and move on. We always need to find tradeoffs and evaluate our options to decide what would be the best choice. We often need to consider not only the technical aspects but cultural or team aspects too.</p>

<p>iOS applications involve a great deal of <strong>UI</strong> programming. The iOS frameworks are object-oriented, and Swift is not a pure FP language. Throwing away all the code from Apple&rsquo;s frameworks just to make our code more functional is not a realistic approach.</p>

<p>Thus we need to balance where we want to use from different programming paradigms. For a beginner, or a team with not too much experience with FP, my choice would be doing <a href="http://www.johndcook.com/blog/2009/03/23/functional-in-the-small-oo-in-the-large/">Functional Programming in the small, Object Oriented in the large</a>.</p>

<h1>FP in the small using Swift</h1>

<p>Let&rsquo;s look at the features of Swift and the standard library that help write more &lsquo;functional&rsquo; without having to restructure or change how we build a project.</p>

<h2>Better functions</h2>

<p>One of the first terms you will hear when diving into FP is that functions are <strong>first-class</strong> citizens, or the language supports <strong>higher-order</strong> functions.</p>

<p>What this means is that functions are treated like any other type. Think as functions being treated like objects; They can accept other functions as input, return other functions, and be stored and passed around.</p>

<p>Under this definition Objective-C <em>kind of</em> supports this, but it&rsquo;s lacking in several areas. Firstly, there&rsquo;s a difference between blocks and selectors, the way to use them is different and the syntax is more complex. Secondly, in Swift a closure is a function without name, and they both have the same type, whereas in Objective-C closures can only be expressed with blocks.</p>

<p>A simple example for iOS developers is:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='Swift'><span class='line'><span class="k">func</span> <span class="nf">animation</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">view</span><span class="p">.</span><span class="n">transform</span> <span class="o">=</span> <span class="n">CGAffineTransformMakeScale</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="bp">UIView</span><span class="p">.</span><span class="n">animateWithDuration</span><span class="p">(</span><span class="mf">0.4</span><span class="p">,</span> <span class="nl">animations</span><span class="p">:</span> <span class="n">animation</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Or with a closure:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='Swift'><span class='line'><span class="k">let</span> <span class="n">animation</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">view</span><span class="p">.</span><span class="n">transform</span> <span class="o">=</span> <span class="n">CGAffineTransformMakeScale</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="bp">UIView</span><span class="p">.</span><span class="n">animateWithDuration</span><span class="p">(</span><span class="mf">0.4</span><span class="p">,</span> <span class="nl">animations</span><span class="p">:</span> <span class="n">animation</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>You couldn&rsquo;t do the first example with Objective-C selectors, specially considering that you can declare a function locally inside another function. The bigger difference for me is that the syntax is very concise and easy to understand. The language also has special handling for trailing closures which is very handy to keep the code readable. A side effect of this is that implementing DSLs using closures is very easy (check my library <a href="https://github.com/DarthMike/SwiftAnimations">SwiftAnimations</a> or Robert Böhnke&rsquo;s <a href="https://github.com/robb/Cartography">Cartography</a>). This might seem a minor point, but in my opinion readability of a programming language is very important.</p>

<h2>Functions (and closures) everywhere</h2>

<p>Functions are everywhere, even disguised sometimes:</p>

<ul>
<li>Reference and value type methods are functions that take an implicit &lsquo;self&rsquo; first argument</li>
<li>Computed properties are closures</li>
<li>Lazy variables are defined with a closure</li>
</ul>


<p>Given that functions are everywhere my first recommendation is that you should learn to read and understand the signature. This is specially important when generics are involved, which is a very fundamental part of the language.</p>

<p>A function signature conveys two crucial bits of information:</p>

<ul>
<li>The <strong>shape</strong> it has, so it can be used somewhere else where another function expects same signature</li>
<li><strong>What</strong> the function does. In some cases, specially in the standard library, reading the name and understanding the signature of a function, you can infer what it does. I don&rsquo;t think it replaces documentation but it&rsquo;s a big helper when reading code.</li>
</ul>


<p>For example take the definition of map for optionals in Swift 2.0 standard library (T is the generic type of the optional itself):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="c1">/// If `self == nil`, returns `nil`.  Otherwise, returns `f(self!)`.</span>
</span><span class='line'>    <span class="k">func</span> <span class="n">map</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="nl">f</span><span class="p">:</span> <span class="p">@</span><span class="n">noescape</span> <span class="p">(</span><span class="n">T</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">U</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">U</span><span class="o">?</span>
</span></code></pre></td></tr></table></div></figure>


<p>Just looking at the declaration, specifically focusing on it&rsquo;s signature, we could say that mapping the optional returns an optional of different type, based on the function we pass in, that transforms between the two types.</p>

<p>Even though <strong>Swift compiler does not guarantee functions are pure</strong>, so we could be accessing the network, reading disk or returning nil for every input value, we can tell a lot from a function signature.</p>

<h2>Elementary functions</h2>

<p>Given that FP is about functions, many programming languages offer similar &lsquo;basic&rsquo; basic ones which are the building blocks of more complex logic. Understanding and knowing how to use these can make you start applying a more functional style across all your code easily.</p>

<blockquote><p><strong>Swift 2.0</strong>: Many of the free functions in the standard library have been moved to methods in types or generic protocols. This is more in line with Apple&rsquo;s &lsquo;Protocol Oriented Programming&rsquo;, but the fundamentals of FP are still the same. You can see see those methods as if they were namespaced functions by the type, rather than global functions overloaded for specific types.</p></blockquote>

<p>Learn how the basic functions work, and how you can apply them to your day to day code:</p>

<ul>
<li>Map</li>
<li>Filter</li>
<li>Reduce</li>
<li>flatMap</li>
</ul>


<p><a href="https://twitter.com/HarlanKellaway">Harken Hallway</a> wrote an <a href="http://harlankellaway.com/blog/2015/08/10/swift-functional-programming-intro">excellent post</a> that gives practical introduction to some of them.</p>

<p>Here are some ways I&rsquo;ve changed my programming style by using those more often. It generally boils down to avoiding &lsquo;if&rsquo; statements and loops:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='Swift'><span class='line'><span class="c1">// Map for... mapping. You might be surprised how many times you might need this simple concept</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">MyDataModel</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// Your app model values</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="n">MyViewModel</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">init</span><span class="p">(</span><span class="nl">model</span><span class="p">:</span> <span class="n">Model</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="nb">self</span><span class="p">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="c1">// ViewModel logic/presentation values</span>
</span><span class='line'>  <span class="n">private</span> <span class="k">let</span> <span class="nl">model</span><span class="p">:</span> <span class="n">MyDataModel</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">let</span> <span class="nl">models</span><span class="p">:</span> <span class="p">[</span><span class="n">MyDataModel</span><span class="p">]</span> <span class="c1">//Read from somewhere, maybe DB or remote</span>
</span><span class='line'><span class="k">let</span> <span class="n">viewModels</span> <span class="o">=</span> <span class="n">models</span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="n">MyViewModel</span><span class="p">.</span><span class="k">init</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='Swift'><span class='line'><span class="c1">// Reduce instead of loops. Useful when finding aggregate information</span>
</span><span class='line'><span class="k">let</span> <span class="nl">anySubViewEmpty</span><span class="p">:</span> <span class="n">Bool</span> <span class="o">=</span> <span class="nb">self</span><span class="p">.</span><span class="n">subviews</span><span class="p">.</span><span class="n">reduce</span><span class="p">(</span><span class="nb">false</span><span class="p">)</span> <span class="p">{</span> <span class="n">empty</span><span class="p">,</span> <span class="n">view</span>
</span><span class='line'>     <span class="k">return</span> <span class="n">empty</span> <span class="o">||</span> <span class="n">CGRectEqualToRect</span><span class="p">(</span><span class="n">view</span><span class="p">.</span><span class="n">frame</span><span class="p">,</span> <span class="n">CGRectZero</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='Swift'><span class='line'><span class="c1">// Flatmap with optionals. For example parsing some model array and every parse operation can fail, and it&#39;s described with a failable initialializer.</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">MyDataModel</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// Model values</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">extension</span> <span class="n">MyDataModel</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">init</span><span class="o">?</span><span class="p">(</span><span class="nl">json</span><span class="p">:</span> <span class="n">JSON</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// Parsing</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">let</span> <span class="nl">jsonItems</span><span class="p">:</span> <span class="p">[</span><span class="n">JSON</span><span class="p">]</span> <span class="c1">// Fetched from a remote server</span>
</span><span class='line'><span class="c1">// flatMap in Swift 2.0 has special knowledge of optionals, we take advantage of that</span>
</span><span class='line'><span class="k">let</span> <span class="n">parsedModels</span> <span class="o">=</span> <span class="n">jsonItems</span><span class="p">.</span><span class="n">flatMap</span><span class="p">(</span><span class="n">MyDataModel</span><span class="p">.</span><span class="k">init</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="c1">// Map on optionals</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">User</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">let</span> <span class="nl">name</span><span class="p">:</span> <span class="n">String</span>
</span><span class='line'>    <span class="k">let</span> <span class="nl">nickname</span><span class="p">:</span> <span class="n">String</span><span class="o">?</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="n">UserViewModel</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">init</span><span class="p">(</span><span class="nl">user</span><span class="p">:</span> <span class="n">User</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">user</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">var</span> <span class="nl">displayName</span><span class="p">:</span> <span class="n">String</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">func</span> <span class="n">formatNickName</span><span class="p">(</span><span class="nl">nickName</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">String</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="s">&quot;Known as \(nickName)&quot;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// Here we avoid if let by taking advantage of map and nil coalescing operator.</span>
</span><span class='line'>        <span class="c1">// Specifies intent with less noise</span>
</span><span class='line'>        <span class="k">return</span> <span class="nb">self</span><span class="p">.</span><span class="n">user</span><span class="p">.</span><span class="n">nickname</span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="n">formatNickName</span><span class="p">)</span> <span class="o">??</span> <span class="s">&quot;&quot;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">private</span> <span class="k">let</span> <span class="nl">user</span><span class="p">:</span> <span class="n">User</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>There&rsquo;s many times you can benefit from only these basic functions, which are already implemented in the Swift standard library. Starting to apply these standard functions when coding can go a long way to make yourself and your team understand the use and benefit of a more functional or declarative code style. Getting accustomed to using those functions regularly is the first big step to writing more functional code.</p>

<h3>EDIT 15/09/2015: Is &lsquo;for&rsquo; not necessary?</h3>

<p>I previously wrote that I liked to use maps for simple loops, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='Swift'><span class='line'><span class="c1">// Map instead of loops</span>
</span><span class='line'><span class="k">let</span> <span class="nl">views</span><span class="p">:</span> <span class="p">[</span><span class="bp">UIView</span><span class="p">]</span> <span class="o">=</span> <span class="c1">// Fill array as needed</span>
</span><span class='line'><span class="n">views</span><span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="nb">self</span><span class="p">.</span><span class="n">addSubview</span><span class="p">(</span><span class="err">$</span><span class="mi">0</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'><span class="c1">// To suppress compiler warning</span>
</span><span class='line'><span class="n">_</span> <span class="o">=</span> <span class="n">views</span><span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="nb">self</span><span class="p">.</span><span class="n">addSubview</span><span class="p">(</span><span class="err">$</span><span class="mi">0</span><span class="p">)</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&rsquo;ve recently changed opinion and I think it&rsquo;s better to use standard loops when not producing any result from mapping. We are not increasing readability of the code, but confusing future readers of what is going on. Therefore I don&rsquo;t recommend doing so, so use loops when they are needed.</p>

<p>As you can see any technique can be abused so always keep in mind the benefit of writing code in a particular style.</p>

<h2>Operators</h2>

<p>Spend some time reading about FP and you will eventually bump into some crazy-looking operators. Even though I don&rsquo;t think they increase readability of code, it&rsquo;s useful to understand how they work.</p>

<p>If you start working only with functions, you&rsquo;ll need to connect them in somehow. As far as FP is concerned, the connection should be done by, you guessed it, another function. This &lsquo;connecting&rsquo; function will act as an adapter between the output type of the first one, and the input type of the second one.</p>

<p>A typical example is when you have an optional type. Then you have a function that transforms non-optional types and can fail, represented as an optional return. Here&rsquo;s a dummy example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="c1">// Converts number to string if it is valid</span>
</span><span class='line'><span class="k">let</span> <span class="nl">toValidString</span><span class="p">:</span> <span class="n">Int</span> <span class="o">-&gt;</span> <span class="n">String</span><span class="o">?</span> <span class="o">=</span> <span class="p">{</span> <span class="n">number</span> <span class="k">in</span>
</span><span class='line'>    <span class="n">guard</span> <span class="n">number</span> <span class="o">&lt;</span> <span class="mi">10</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">nil</span> <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="s">&quot;Number is small: \(number)&quot;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Converts number to number if it&#39;s even</span>
</span><span class='line'><span class="k">let</span> <span class="nl">toEvenNumber</span><span class="p">:</span> <span class="n">Int</span> <span class="o">-&gt;</span> <span class="n">Int</span><span class="o">?</span> <span class="o">=</span> <span class="p">{</span> <span class="n">number</span> <span class="k">in</span>
</span><span class='line'>    <span class="n">guard</span> <span class="n">number</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">nil</span> <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">number</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we want to combine those two functions, the input type of one does not match the output type of the other. The functions do not accept optionals, as it does not make sense to do so.</p>

<p>You can bind those two functions with another function, that will translate the output to the input of the next one:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="k">func</span> <span class="n">bind</span><span class="o">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="o">&gt;</span><span class="p">(</span><span class="nl">from</span><span class="p">:</span> <span class="n">A</span><span class="p">,</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">A</span><span class="o">-&gt;</span><span class="n">B</span><span class="o">?</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">B</span><span class="o">?</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="k">let</span> <span class="n">from</span> <span class="o">=</span> <span class="n">from</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="n">transform</span><span class="p">(</span><span class="n">from</span><span class="p">)</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">return</span> <span class="nb">nil</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then combining the operations using bind, the code would be:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="c1">// Outputs &quot;Number is small: 2&quot;</span>
</span><span class='line'><span class="n">bind</span><span class="p">(</span><span class="n">bind</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">toEvenNumber</span><span class="p">),</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">toValidString</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Outputs nil</span>
</span><span class='line'><span class="n">bind</span><span class="p">(</span><span class="n">bind</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">toEvenNumber</span><span class="p">),</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">toValidString</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>There&rsquo;s one big problem with the previous code: It&rsquo;s very hard to read and know what is going on. The connection function takes over the space and pollutes that we are actually just connecting two other functions together.</p>

<p>Remember that an operator in Swift is just a function. It can take two forms, depending on the operands it works with; A single operand, or two operands. We can then define a cryptic symbol can be defined to express &lsquo;bind&rsquo;:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="c1">// Bind operator, also called &#39;flatmap&#39;</span>
</span><span class='line'><span class="k">func</span> <span class="o">&gt;&gt;-&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="o">&gt;</span><span class="p">(</span><span class="nl">from</span><span class="p">:</span> <span class="n">A</span><span class="o">?</span><span class="p">,</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">A</span><span class="o">-&gt;</span><span class="n">B</span><span class="o">?</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">B</span><span class="o">?</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">bind</span><span class="p">(</span><span class="n">from</span><span class="p">,</span> <span class="nl">transform</span><span class="p">:</span> <span class="n">transform</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then the two previous examples read a lot better, assuming you know what &lsquo;bind&rsquo; is, and you know how to interpret the operator:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='swift'><span class='line'><span class="c1">// Outputs &quot;Number is small: 2&quot;</span>
</span><span class='line'><span class="mi">2</span> <span class="o">&gt;&gt;-</span> <span class="n">toEvenNumber</span> <span class="o">&gt;&gt;-</span> <span class="n">toValidString</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Outputs nil</span>
</span><span class='line'><span class="mi">20</span> <span class="o">&gt;&gt;-</span> <span class="n">toEvenNumber</span> <span class="o">&gt;&gt;-</span> <span class="n">toValidString</span>
</span></code></pre></td></tr></table></div></figure>


<p>I think the only reason why many custom operators exist in FP languages is because it improves readability of the code. If you know what an operator does - and it&rsquo;s likely you will because they are very common connectors - then you read code and immediately understand the intention of the code. Using them the code should more declarative, with the tradeoff that somebody not used to those operators will thing it is very foreign syntax.</p>

<p>There&rsquo;s other custom operators you will see when reading about FP. There&rsquo;s the two I&rsquo;ve encountered most often:</p>

<ul>
<li>Bind, also called <strong>FlatMap</strong> in Swift: Expressed as >>>, >>= or <strong>>>-</strong></li>
<li><strong>Apply</strong>, or &lsquo;Applicative functors&rsquo;: Expressed as <strong>&lt;*></strong></li>
</ul>


<p>ThoughtBot created two open source projects that are very helpful to understand practical operators used for function composition: <a href="https://github.com/thoughtbot/Runes">Runes</a> and <a href="https://github.com/thoughtbot/Argo">Argo</a>. Those projects are concise and helped me a lot to grasp some FP concepts, I encourage you to check them.</p>

<blockquote><p><strong>Swift 2.0</strong>: As Swift moves towards removing usage of free functions in favour of methods in protocols and protocol extensions, you&rsquo;ll see less use of these custom operators in the language. But still, it&rsquo;s a nice concept to understand.</p></blockquote>

<h1>Conclusion</h1>

<p>Changing programming habits is hard, but adopting a new way of writing code can be very rewarding if you understand the benefits that it provides.</p>

<p>I think smaller changes to ways we code are achievable by every team and it&rsquo;s just a matter of communicating and discussing the benefits through code reviews. Ultimately the goal is to make code simpler, clearer and easier to read and maintain.</p>

<p>Given Swift is a multi-paradigm language it&rsquo;s up to the programmer to choose how to write code, so it&rsquo;s important to  explore various ways until we find what we could call &lsquo;idiomatic&rsquo; Swift.</p>

<p>We are all learning so don&rsquo;t hesitate to tell me if I&rsquo;m wrong or disagree with me. If you have any comments I&rsquo;d love to <a href="https://twitter.com/miguelquinon">discuss them</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Poor man's functional programming]]></title>
    <link href="http://miqu.me/blog/2015/08/31/poor-mans-functional-programming/"/>
    <updated>2015-08-31T20:00:00+00:00</updated>
    <id>http://miqu.me/blog/2015/08/31/poor-mans-functional-programming</id>
    <content type="html"><![CDATA[<p>It&rsquo;s a very exciting time to be an iOS or Mac developer. The platform moves very fast and we got a new toy (language) to play with: Swift. It&rsquo;s the perfect time to reevaluate, learn and evolve as a programmer, because you will be forced to adopt this new language (yes, I think Swift is the future, 100%).</p>

<p>I want to relate this to the fact that most iOS engineers, or mobile application developers, are traditionally familiar with Object Oriented Programming paradigm. But Swift offers more than different syntax and OOP.</p>

<p>I’m referring to <em>some</em> features inspired by functional languages. If you’ve ever had an interest, but never the chance or motivation to go forth and look into this paradigm, now it&rsquo;s the perfect time to do it.</p>

<!-- more -->


<h1>Functional programming is&hellip;</h1>

<p>I don’t want to start reviewing what functional programming is, there are <a href="http://harlankellaway.com/blog/2015/08/10/swift-functional-programming-intro/">excellent</a> resources out there, <a href="https://www.objc.io/books/">check them out</a>.</p>

<p>I want to point out how a beginner, somebody who starts learning Functional Programming (FP), might feel:</p>

<ul>
<li>Too abstract</li>
<li>Not applicable to my problems</li>
<li>For academics</li>
<li>What does even ‘pure’ mean?</li>
<li>I <strong>need</strong> to mutate stuff!</li>
<li>Meh, I’ll go back to my familiar way of doing things</li>
</ul>


<p>There’s a higher barrier of entry to learn how to think and code programs which are more ‘functional’. The concepts are abstract,  and the benefits are often described as <em>it’s more functional</em> without emphasis of what you are actually gaining. Also there’s the problem that many times all is explained by simple examples. But every programmer knows there’s a huge difference between toy programs and real big projects.</p>

<h1>Flame wars</h1>

<p>You’ll find engineers and computer scientists that believe that the <em>only and true</em> way of doing things is of course thinking functionally, and we’ve all been doing it wrong during the last decades. The math is the only source of truth and validity, hence FP is the only way.</p>

<p>They’ve got their very valid reasons to think this way, but my opinion is that many people familiar with FP start to use higher-level vocabulary, and describe conventions and problems in a way that might seem foreign and strange for somebody who is not used to those terms. Rob Napier has <a href="http://robnapier.net/haskell-overlords">an excellent article describing this</a>.</p>

<p>Computer science, and programming in particular, is a curious field where people take sides around things that may seem totally absurd for an outsider: Vim vs Emacs, Windows vs Mac, Linux vs everything else, Ruby vs Python, OOP vs FP.</p>

<p>Do we really need to fight over who is right, instead of using the tools that are most appropriate to do our job? I’m totally on the side of moving on and being pragmatic about learning, and eventually as growing as a software engineer. By looking at different ways of solving a problem you can only get better at what you do, not worse, because you will have the ability to make an informed choice.</p>

<p>I’d love to see the people who are experts or versed in FP to reach out and share knowledge in a way that can be understood by beginners. If we really want people to be aware of the benefits of using a different paradigm, we should make it approachable to start with.</p>

<h1>Poor man’s functional programming</h1>

<p>I would like to think that for most engineers, the choice of paradigm on our day to day work is not a clear one. Thus we will have direct benefit from a language that lets you choose. Using such a language we will be able to write code in an OOP or FP way where it benefits the most. And it seems I’m <a href="https://realm.io/news/altconf-saul-mora-object-orientated-functional-programming/">not</a> the <a href="http://www.johndcook.com/blog/2009/03/23/functional-in-the-small-oo-in-the-large/">first</a> to think about this.</p>

<p>Coming back to Swift, this is precisely what you can do with it. You can choose programming styles depending on what your problem is, the framework you work with, and even the team you work on.</p>

<p>This flexibility does comes with a price though: The functional features of the language are not as rich as other, <em>purer</em> languages. This is the point of my essay: With Swift, and given the normal users of the language (myself included), we will be able to do <strong>Poor man’s functional programming</strong>. A style that tries to mix the best of both paradigms.</p>

<p>We’ll make mistakes as we learn, but hey that’s part of our job. Do you like code you wrote 6 months ago? I know I don’t.</p>

<h1>Moving on</h1>

<p>I like to be <a href="https://pragprog.com/book/tpp/the-pragmatic-programmer">pragmatic</a> rather than zealous. And I think Swift brings an excellent opportunity to learn more about other ways of thinking about building software, different abstractions, and how to fit those into our day to day work as iOS developers.</p>

<p>Swift, as <a href="http://www.scala-lang.org/">other modern languages</a>, offers a unique opportunity to learn and grow. It’s a very exciting field we work on.</p>

<p><em>Aside: Sources to check the ‘tension’ between OOP and FP:</em></p>

<ul>
<li><a href="http://www.johndcook.com/blog/2009/03/23/functional-in-the-small-oo-in-the-large/">Functional on the small, OO in the large</a></li>
<li><p><a href="https://realm.io/news/altconf-graham-lee-i-have-no-idea-what-i-m-doing/">I have no idea what I’m doing</a></p></li>
<li><p><a href="http://www.smashcompany.com/technology/object-oriented-programming-is-an-expensive-disaster-which-must-end">OOP is an expensive disaster</a></p></li>
<li><p><a href="http://stackoverflow.com/questions/2835801/why-hasnt-functional-programming-taken-over-yet">Why hasn’t functional programming taken over yet</a></p></li>
</ul>


<p><em>And the list goes on and on&hellip;</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Freehand Drawing]]></title>
    <link href="http://miqu.me/blog/2015/08/20/freehand-drawing/"/>
    <updated>2015-08-20T20:09:40+00:00</updated>
    <id>http://miqu.me/blog/2015/08/20/freehand-drawing</id>
    <content type="html"><![CDATA[<p>Yesterday my last post on the series ‘Let’s build Freehand Drawing for iOS’ has been published at Badoo Tech blog. It’s been a very good experience for me, and of course I recommend you visit the tutorials if you are interested.</p>

<p>I divided the tutorials in 3 parts:</p>

<ul>
<li><a href="https://techblog.badoo.com/blog/2015/06/15/lets-build-freehand-drawing-in-ios-part-1/">Part 1</a>: Introduces the feature and a naive implementation</li>
<li><a href="https://techblog.badoo.com/blog/2015/06/29/lets-build-freehand-drawing-in-ios-part-2/">Part 2</a>: Adds undo functionality, refactoring along the way</li>
<li><a href="https://techblog.badoo.com/blog/2015/08/18/lets-build-freehand-drawing-in-ios-part-3/">Part 3</a>: Improves stroke to make it look smoother</li>
</ul>


<h1>Building tutorials is hard</h1>

<p>This series has been my first take on writing technical tutorials. And I must say that it’s a lot harder than I thought. You need to keep track of the content, code correctness, and the pace of the articles so it’s readable and understandable. Coding while writing an article is challenging, but the experience has been very gratifying.</p>

<p>I also get to brag about something I was really happy to build for the <a href="http://bumble.com/">Bumble</a> project. So I think I’ll definitely write other tutorials in the future.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[One commit a day]]></title>
    <link href="http://miqu.me/blog/2015/08/07/one-commit-a-day/"/>
    <updated>2015-08-07T20:22:22+00:00</updated>
    <id>http://miqu.me/blog/2015/08/07/one-commit-a-day</id>
    <content type="html"><![CDATA[<p>One commit a day. A lesson I’ve learned the hard way when dealing with long-term side projects.</p>

<!-- more -->


<h1>The first push</h1>

<p>You are starting a new side project. That new library, framework, cool app idea or just a hack you had in your mind for long time. Starting it is easy, as you find some spare time and kick off your editor of choice and start writing code.</p>

<p>I’ve done this some times, but then if having a full time job, keeping with your side project is very very hard. But to successfully build something useful, something more than a proof of concept, you will need more than just one evening.</p>

<h1>Keeping up</h1>

<p>It’s after some evenings working at home that I would say: “Today I will rest, I’m not working on my side project”. And this act will make that the next day I will be thinking again that I can rest another day. And rest I will.</p>

<p>The long streak of commits, progress and closeness to completion stops right there. It will be very hard for me to restart my work again. Stopping for one day made it harder to restart my work and delayed the whole project for more than a week.</p>

<p>I’ve heard many people have this problem and the solution is very pragmatic: A commit a day.</p>

<h1>A commit a day</h1>

<p>When you work on a side project you do it because you want to use your spare time to build something. It’s a project nonetheless so you still need some goals and motivation. I’ve found that stopping the regular work can kill your motivation and focus.</p>

<p>So I’ve proposed myself whenever I work on a side project, I need to advance something every day. I’m building an app now (shh, top secret), so I will work on anything related to it at least 20 minutes every day. It does not have to be programming. It can be updating a design, thinking of new features to add in the future, tweaking landing page, or actually coding.</p>

<p>This act of keeping up with your project makes it so that I will focus more on it, and I will not loose the sense of progression. Also if I will work on the project only during weekends I found that I never complete my goals so I’m frustrated after the weekend.</p>

<p>If I keep my thoughts focused in my project, even for a short amount of time every day, then coming back to it is more satisfying and I get more things done.</p>

<h1>Ripley signing off</h1>

<p>That’s the end of my ramblings. So lesson learned for me; keep doing something every day for your side project. A commit a day, the road to completion out of frustration.</p>
]]></content>
  </entry>
  
</feed>
