<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>software engineering · michaelprimeaux.com</title><link>https://michaelprimeaux.com/en/tags/software-engineering/</link><description>Parallel and Distributed Systems</description><generator>Hugo</generator><language>en</language><managingEditor>michael@michaelprimeaux.com (Michael Primeaux)</managingEditor><webMaster>michael@michaelprimeaux.com (Michael Primeaux)</webMaster><copyright>&#169; 2026 Michael Primeaux</copyright><lastBuildDate>Mon, 14 Mar 2016 00:06:00 +0000</lastBuildDate><atom:link href="https://michaelprimeaux.com/en/tags/software-engineering/index.xml" rel="self" type="application/rss+xml"/><image><url>https://michaelprimeaux.com/sixafter-logo-150x150.png</url><title>michaelprimeaux.com</title><link>https://michaelprimeaux.com/en/</link></image><item><title>Test Driven Development: Mocks</title><link>https://michaelprimeaux.com/en/posts/2016-03-13-dummies-mocks-stubs-fakes/</link><pubDate>Mon, 14 Mar 2016 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2016-03-13-dummies-mocks-stubs-fakes/</guid><description>The importance of dummies, mocks, stubs, and fakes is fundamental to test-driven development.</description><content:encoded><![CDATA[<p>The importance of dummies, mocks, stubs, and fakes is fundamental to test-driven development.</p>
<p>Dummy objects are passed around but never actually used. Usually, they&rsquo;re simply used to fill parameter lists.</p>
<p>Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).</p>
<p>Stubs provide answers to calls made during the test, usually not responding to anything outside what&rsquo;s programmed for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it &lsquo;sent&rsquo; or only how many messages it &lsquo;sent&rsquo;.</p>
<p>Mocks are what we&rsquo;re talking about here. Mocks are objects pre-programmed with expectations forming a specification of the calls they expect to receive.</p>]]></content:encoded><category>software engineering</category></item><item><title>Developer Testing</title><link>https://michaelprimeaux.com/en/posts/2016-02-22-developer-testing/</link><pubDate>Mon, 22 Feb 2016 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2016-02-22-developer-testing/</guid><description>Why is it important to write automated tests? I&amp;rsquo;m asked this question far too often by software engineering teams. The usual response I receive after asking someone why they don&amp;rsquo;t write tests usually is because &amp;ldquo;I don&amp;rsquo;t have time&amp;rdquo;. Nothing could be further from the truth. In reality, valuable time is saved by writing tests first. Writing tests is an important and fundamental concept to our craft. So much so that I thought best to write about it.</description><content:encoded><![CDATA[<p>Why is it important to write automated tests? I&rsquo;m asked this question far too often by software engineering teams.  The usual response I receive after asking someone why they don&rsquo;t write tests usually is because &ldquo;I don&rsquo;t have time&rdquo;. Nothing could be further from the truth. In reality, valuable time is saved by writing tests <em>first</em>. Writing tests is an important and fundamental concept to our craft. So much so that I thought best to write about it.</p>
<p>But as with all things, first a bit of context.</p>
<h2 id="the-agile-development-methodology" class="heading">The Agile Development Methodology
    <a class="heading__anchor" href="#the-agile-development-methodology" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>Agile is a software development methodology that borrows from 
<a href="http://en.wikipedia.org/wiki/Scrum_%28software_development%29" class="link-external" rel="noopener noreferrer" target="_blank">Scrum
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 project management practices, 
<a href="http://en.wikipedia.org/wiki/Extreme_programming" class="link-external" rel="noopener noreferrer" target="_blank">Extreme Programming
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (XP), and 
<a href="http://en.wikipedia.org/wiki/Test-driven_development" class="link-external" rel="noopener noreferrer" target="_blank">Test Driven Development
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (TDD).</p>
<p>It&rsquo;s not my intention to write about Scrum, XP, and TDD in excruciating detail since there are many excellent books on these subjects. I would, however, like to share my view on why TDD is foundational to any software engineering effort.</p>
<h2 id="scrum" class="heading">Scrum
    <a class="heading__anchor" href="#scrum" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>As most are aware, Scrum is an iterative, incremental framework for managing complex work and though intended for management of software development projects, it can be used as a general program management approach.  Contrary to popular belief, however, Scrum is not an acronym. First used to describe hyper-productive development in 1987 by 
<a href="http://en.wikipedia.org/wiki/Ikujiro_Nonaka" class="link-external" rel="noopener noreferrer" target="_blank">Ikujiro Nonaka
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and 
<a href="http://en.wikipedia.org/wiki/Hirotaka_Takeuchi" class="link-external" rel="noopener noreferrer" target="_blank">Hirotaka Takeuchi
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, Scrum actually refers to the mechanism used in rugby for getting an out-of-play ball back into play.</p>
<p>Many software engineering teams still practice the Waterfall development methodology and for some projects &ndash; in particular when requirements are static and rarely change &ndash; it&rsquo;s an appropriate practice. The building of a jet propulsion engine is a good example of a project that likely benefits from the Waterfall development methodology. In contrast, Scrum&rsquo;s two pillars are team empowerment and adaptability:</p>
<ol>
<li>
<p>Team empowerment: Once teams are given work to do, they are responsible for figuring out how to do it. The team does the best it can during each iteration. While a team works, their only interaction with management is to tell management what is getting in their way and needs to be removed to improve their productivity.</p>
</li>
<li>
<p>Adaptability: Scrum uses &ldquo;punctuated equilibrium&rdquo;. The team maintains an equilibrium during each iteration, insulated from outside disturbance. Iteration are generally punctuated every thirty days so that the team and management can evaluate what should be done during the next iteration; this decision is based on what the team has accomplished and what the environment dictates is the next most important thing to do.</p>
</li>
</ol>
<h3 id="velocity" class="heading">Velocity
    <a class="heading__anchor" href="#velocity" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>It&rsquo;s difficult to discuss the importance of writing tests without first discussing the notion of velocity and while I could write a dissertation just on the notion of velocity, it is simply defined as how many story points of effort a team can <em>complete</em> in a single iteration. Once established, velocity can be used to plan projects and forecast release and product completion dates. A team&rsquo;s velocity is an important key performance indicator.</p>
<h2 id="extreme-programming" class="heading">eXtreme Programming
    <a class="heading__anchor" href="#extreme-programming" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>From XP, we borrow pair programming and continuous integration. Code reviews are good so let’s do them all the time; this is pair programming. Measuring code quality and ensuring a source level change didn’t cause any regressions is good so let’s do it as often as possible; continuous integration.</p>
<p>It should be no surprise that writing automated tests and measuring code coverage go hand-in-hand with continuous integration.</p>
<h2 id="test-driven-development" class="heading">Test-Driven Development
    <a class="heading__anchor" href="#test-driven-development" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>For any particular software engineering effort, there are three axes to control: quality, schedule, and scope. Quality remains locked at “high” and so the engineering team must choose how to balance the remaining two.</p>

<blockquote><p>Clean code that works&hellip;now. This is the seeming contradiction that lies behind much of the pain of programming. Test driven development replies to this contradiction with a paradox &ndash; test the program before you write it.</p>
<ul>
<li>Test Driven Development: By Example, 2002</li>
</ul>
</blockquote>
<p>Regardless of your development methodology, writing tests are important. Automated testing underpins many Agile practices, most significantly as part of 
<a href="http://en.wikipedia.org/wiki/Continuous_integration" class="link-external" rel="noopener noreferrer" target="_blank">Continuous Integration
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.  TDD is a process by which the tests are written <em>before</em> the code itself. Through a rapid cycle of adding new tests, making them pass, and then refactoring to clean code, the software design evolves through the tests.</p>
<h3 id="red-green-refactor" class="heading">Red, Green, Refactor
    <a class="heading__anchor" href="#red-green-refactor" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>When a test is first written, it most likely won&rsquo;t even compile - the implementation hasn&rsquo;t been written. Modern IDEs allow a developer to rapidly create code and then refactor it into shape. After adding a test, a developer strives to reach a &ldquo;green bar&rdquo; (all tests passing) as quickly as possible, and then refactors backed up by the safety of the tests. This quickly becomes a cycle of &ldquo;red, green, refactor&rdquo;, adding a kind of rhythm to the development process. When a new feature is required, tests are added, code is written to make the tests pass, and finally refactoring removes any duplication.</p>
<h2 id="benefits-of-writing-tests" class="heading">Benefits of Writing Tests
    <a class="heading__anchor" href="#benefits-of-writing-tests" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>Whether your project is open source, commercial, or corporate, there are clear and important advantages to writing automated tests.</p>
<ol>
<li><strong>Courage</strong>. Writing tests allows one to move from a state of uncertainty to that of courage. When you write tests, you know the state of the your code. Intimately.  You know if your code works or if it doesn’t. You know if your old code, and the code of your colleagues works as expected.</li>
<li><strong>Communication</strong>. Tests can also be used for communication. This works well to communicate what the software does.  We all may not know how to speak the same language but as a member of a particular software engineering team, we all know how to communicate using a specific programming language. We all can read and write code with good clarity.</li>
<li><strong>Documentation</strong>. Beyond shore-to-shore communication, tests also make a good candidate to use as examples in documentation and in comments.</li>
<li><strong>Refactoring</strong>.  Setting your minimum allowable test coverage percentage to a respectable number (90 percent and greater) allows you to refactor your source code with confidence. Use cases pivot. You refactor the code. Then simply make all failing tests pass again.</li>
<li><strong>Design</strong>. Writing tests <em>first</em> leads to a better design.
<ol>
<li>It forces you to think about the shape of your API up front; to really understand how your consumer uses the API before you spend valuable time writing the implementation.</li>
<li>It allows you to understand any possible frustrations and complexities with the shape of your API before your consumer.</li>
<li>It saves time by ensuring the API satisfies your design goals and <em>nothing</em> more.</li>
</ol>
</li>
</ol>
<p>All of these points are critical to a team&rsquo;s velocity.</p>
<h2 id="rules-of-engagement" class="heading">Rules of Engagement
    <a class="heading__anchor" href="#rules-of-engagement" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>The keywords MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD, SHOULD NOT, RECOMMENDED, MAY and OPTIONAL, when they appear in this document, are to be interpreted as described in 
<a href="http://datatracker.ietf.org/doc/rfc2119/" class="link-external" rel="noopener noreferrer" target="_blank">RFC-2119
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<p>In the end, automated tests are just code. Even with best intentions, they can be written incorrectly and contain bugs. Here are a few rules to consider.</p>
<ol>
<li>Keep unit tests small and fast. Ideally the entire test suite SHOULD be executed before every code check in. Keeping the tests fast reduce the development turnaround time.</li>
<li>Tests MUST be fully automated and non-interactive.</li>
<li>Keep tests independent. To ensure testing robustness and simplify maintenance, tests SHALL NOT rely on other tests and SHALL NOT depend on the order in which other tests are executed.</li>
<li>A test MUST leave the environment exactly as it was before execution began. This is an extremely important point since test execution order is never guaranteed and so environmental changes may cause sporadic test failures, which can waste precious time.</li>
<li>A test MUST contain at least one assertion.</li>
<li>A test MUST contain 3 logical sections: arrange, act, and assert.</li>
<li>A test MUST cover boundary cases.</li>
<li>A test fixture MUST provide negative tests. Negative tests intentionally misuse the code and verify robustness and appropriate error handling.</li>
<li>Write tests to reproduce bugs. When a bug is reported, write a test to reproduce the bug (i.e. a failing test) and use this test as success criteria when fixing the code.</li>
<li>Conduct peer 
<a href="http://en.wikipedia.org/wiki/Code_review" class="link-external" rel="noopener noreferrer" target="_blank">code reviews
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 on all tests.</li>
</ol>
<p>Regarding bullet 6, William C. Wake 
<a href="http://c2.com/cgi/wiki?ArrangeActAssert" class="link-external" rel="noopener noreferrer" target="_blank">wrote about
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 using the &ldquo;arrange, act, assert&rdquo; pattern in 2003.  Here&rsquo;s a summary:</p>
<ul>
<li><strong>Arrange</strong>: What is being set up and initialized is contained in the arrange section.</li>
<li><strong>Act</strong>: What method is being executed is contained in the act section</li>
<li><strong>Assert</strong>: What determines the outcome of the test is in the the assert section.</li>
</ul>
<p>I&rsquo;ll write about dummies, mocks, stubs, and fakes in a future post.</p>
<h2 id="conclusion" class="heading">Conclusion
    <a class="heading__anchor" href="#conclusion" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>With test-driven development, robust code is delivered along with a comprehensive suite of tests, allowing developers to make future changes with confidence.  I firmly believe that unless you&rsquo;ve been a contributing member of a software team responsible for a complex code base that it may be difficult to appreciate the critical importance of having a suite of automated unit, integration, and story tests.</p>
<p>Move away from a position of uncertainty to one of confidence. Write tests.</p>]]></content:encoded><category>software engineering</category></item><item><title>The Convergence of Mobile, Cloud, Social, and Big Data</title><link>https://michaelprimeaux.com/en/posts/2013-05-29-mobile-cloud-social-big-data/</link><pubDate>Thu, 30 May 2013 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2013-05-29-mobile-cloud-social-big-data/</guid><description>There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. &amp;ndash; C.A.R. Hoare</description><content:encoded><![CDATA[<blockquote><p>There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. &ndash; C.A.R. Hoare</p>
</blockquote>
<p>Mobile, cloud, social, and non-relational storage technologies (big data) represent agility for many businesses. In particular, the convergence of these technologies equip businesses with efficient methods to attribute key performance indicators, optimize direction, and, equally as important, allocate changes in business processes; all in real-time and for a vast array of incoming data formats.  Individually, each of these technology areas are immense and yet are enablers for each other; and so in early 2008, I began to focus my attention, application of algorithms, and research to the convergence of these technologies.</p>
<p>The technology shifts created by mobile, cloud, social, and big data have changed and are drastically changing the business landscape. The convergence of these technologies began years ago and has matured considerably since 2008. Social media has contributed to this significantly. 
<a href="http://research.google.com/archive/bigtable.html" class="link-external" rel="noopener noreferrer" target="_blank">Big Table
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and, subsequently, 
<a href="http://hbase.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">HBase
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 was born out of need within Google and the open source community, respectively. The Apache 
<a href="http://cassandra.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">Cassandra
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 project was born out of need within Facebook. We’re able to learn interesting statistics like the half-life of links shared on Twitter is approximately 2.8 hours, sports stories spread the fastest of any topics, and there’s a material difference between what people <em>share</em> and what they <em>click</em>.  From a sales and marketing perspective, we can use big data to better leverage social channels to rapidly detect changes in buyer activities and preferences, identify market shifts, monitor competitive activity, and identify opportunities for your business. Social media is analyzed to understand behavior, although it’s important to remember as with all population sampling, it only provides a specialized <em>subset</em> of opinions about a particular topic.</p>
<h2 id="big-data-and-analytics" class="heading">Big Data and Analytics
    <a class="heading__anchor" href="#big-data-and-analytics" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>Big Data is primarily classified by the use of non-relational durable storage solutions that embrace the following characteristics of information technology.</p>
<ul>
<li><strong>Volume</strong>. As of 2012, about 2.5 exabytes of data are created each day, and that number is doubling every 40 months or so. More data cross the internet every second than were stored in the entire internet just 20 years ago. For example, it is estimated that Wal Mart collects more than 2.5 
<a href="http://en.wikipedia.org/wiki/Petabyte" class="link-external" rel="noopener noreferrer" target="_blank">petabytes
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 of data every hour from its customer transactions.</li>
<li><strong>Velocity</strong>. For many applications, the speed of data creation is more important than the volume. Real-time or nearly real time information makes it possible for a company to be much more agile than its competitors. Just consider the speed at which information is collected from the sheer number of active mobile devices each and every day.</li>
<li><strong>Variety</strong>. Information now flows from a plethora of sources in the form of messages, updates, and images from social networks; electronic communication; instrumented machinery; readings from sensors; GPS signals from mobile phones; point of sale terminals; and so on. Structured databases are not well suited to storing and processing information from these disparate sources.</li>
</ul>
<p>&hellip;and it’s only becoming even more challenging. Each and every day, three times per second, we produce the equivalent of the amount of data that the Library of Congress has in its entire print collection. Most of the data produced is irrelevant noise. Clearly, the challenge is in filtering this noise and distilling the remaining information into meaningful results. This vast amount of information is essentially useless without analytics. Timely analytics. Accurate analytics. Technical solutions in this space must be able to traverse multiple data centers, the cloud, and geographical zones. Accurate, real-time analysis affords us an opportunity to answer crucial business questions by those wanting to confidently pivot in response to key performance indicators (KPI).</p>
<p>Before you delve into the non-relational storage arena, it’s important to understand the 
<a href="http://lpd.epfl.ch/sgilbert/pubs/BrewersConjecture-SigAct.pdf" class="link-external" rel="noopener noreferrer" target="_blank">CAP theorem
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.  The theorem began as a conjecture made by University of California, Berkeley computer scientist 
<a href="http://en.wikipedia.org/wiki/Eric_Brewer_%28scientist%29" class="link-external" rel="noopener noreferrer" target="_blank">Eric Brewer
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 at the 2000 Symposium on Principles of Distributed Computing (PODC). In 2002, Seth Gilbert and Nancy Lynch of 
<a href="http://www.mit.edu" class="link-external" rel="noopener noreferrer" target="_blank">MIT
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 published a formal proof of Brewer&rsquo;s conjecture, rendering it a theorem.</p>
<p>Formally, in theoretical computer science, the CAP theorem states that it is impossible for a distributed computer system to simultaneously satisfy all three of the following characteristics:</p>
<ol>
<li><strong>Consistency</strong>. All nodes possess the same data at the same time.</li>
<li><strong>Availability</strong>. A guarantee the system is available at all times to fully operate.</li>
<li><strong>Partition tolerance</strong>. The system continues to operate despite arbitrary message loss or failure of part of the system.</li>
</ol>
<p>In May 2012, 
<a href="http://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed" class="link-external" rel="noopener noreferrer" target="_blank">Brewer clarified some of his positions
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 on why the oft-used &ldquo;two out of three&rdquo; concept can be misleading or misapplied.</p>
<p>Broadly speaking, durable storage technologies are divided into two innate categories: relational and non-relational.  Relational databases have been widely used for the past 40 years and are still quite widely used today. Microsoft SQL Server,  IBM DB2, Oracle, and MySQL are all examples of relational database implementations. Suffice it to say most non-relational database system architectures favor partition tolerance and availability over strong consistency.  Other implementations such as 
<a href="http://cassandra.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">Cassandra
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 offer per-operation control over which two characteristics of the  CAP theorem you would like to favor. My favorite distribution of Cassandra is 
<a href="http://www.datastax.com" class="link-external" rel="noopener noreferrer" target="_blank">DataStax
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, which combines Cassandra, 
<a href="http://hadoop.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">Hadoop
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, and 
<a href="http://lucene.apache.org/solr/" class="link-external" rel="noopener noreferrer" target="_blank">SOLR
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 into a single packaged distribution. Their 
<a href="http://www.datastax.com/what-we-offer/products-services/datastax-enterprise" class="link-external" rel="noopener noreferrer" target="_blank">enterprise edition
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 is free to developers, which is convenient.</p>
<p>With respect to non-relational databases, one must consider a few orientations.</p>
<ul>
<li><strong>Object</strong>. Object databases work to void object-relational impedance mismatch that occurs when trying to use a relational database under an application written in an object-oriented programming language. These databases store information in terms of the object themselves and not in terms of columns and rows.</li>
<li><strong>XML</strong>. XML databases are optimized specifically for working with XML and are, essentially, a special form of a document-oriented database. XML databases have one core function: to allow for efficient storage and query of XML documents. Examples of XML databases are 
<a href="http://www.softwareag.com/corporate/products/wm/tamino/default.asp" class="link-external" rel="noopener noreferrer" target="_blank">Tamino
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (Software AG), 
<a href="http://exist-db.org" class="link-external" rel="noopener noreferrer" target="_blank">eXist
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://www.oracle.com/technetwork/products/berkeleydb/overview/index.html" class="link-external" rel="noopener noreferrer" target="_blank">Berkeley XML DB
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (Oracle), 
<a href="http://www.marklogic.com" class="link-external" rel="noopener noreferrer" target="_blank">MarkLogic
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 Server, and 
<a href="http://www.sedna.org" class="link-external" rel="noopener noreferrer" target="_blank">Sedna
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</li>
<li><strong>Document</strong>.  The basic unit or storage for a document database is a complete document. 
<a href="http://couchdb.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">CouchDB
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and 
<a href="http://www.mongodb.org" class="link-external" rel="noopener noreferrer" target="_blank">MongoDB
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 are examples of document databases. This storage model offers several key advantages over a relational model:
<ul>
<li>A document can store any number of fields of any length, and each field can store multiple values. A relational model requires all fields to be present for every record.</li>
<li>An empty (or null) value (and the related field) does not need to be stored, which can save space. On a related note, many relational databases currently offer support for sparse tables.</li>
<li>Full text search capabilities are typically an intrinsic feature.</li>
<li>Security can be assigned at an individual document level.</li>
<li>A predefined schema is not required whereas a relational database does require schema to be defined in advance of performing any storage or retrieval operation.</li>
</ul>
</li>
<li><strong>Key-Value</strong>. A key/value database stores values based on keys and are classically represented as a Distributed Hashtable (DHT). In a relational model, we tend to first consider the tables that our domain requires, then think of how we can normalize the tables to avoid duplicate data. In a key-value store, however, typically we don’t define a schema. Examples of key-value databases are 
<a href="http://www.mongodb.org" class="link-external" rel="noopener noreferrer" target="_blank">MongoDB
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://redis.io" class="link-external" rel="noopener noreferrer" target="_blank">Redis
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, Amazon 
<a href="http://aws.amazon.com/dynamodb/" class="link-external" rel="noopener noreferrer" target="_blank">Dynamo
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, and 
<a href="http://www.project-voldemort.com" class="link-external" rel="noopener noreferrer" target="_blank">Project Voldemort
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</li>
<li><strong>Graph</strong>. Graph databases differ from other non-relational database offerings (such as key-value stores) in that they represent the <em>edge</em> intrinsically. Instead of tables and columns, a graph database uses three basic constructs to represent data: nodes, edges, and properties. A <em>node</em> is a standalone, independent object. An <em>edge</em> is an object that depends on the existence of two nodes. <em>Properties</em> represent attributes of a node. Examples of graph databases are 
<a href="http://en.wikipedia.org/wiki/FlockDB" class="link-external" rel="noopener noreferrer" target="_blank">FlockDB
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (open sourced by Twitter in April 2010) and 
<a href="http://www.neo4j.org" class="link-external" rel="noopener noreferrer" target="_blank">Neo4J
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</li>
<li><strong>Columnar</strong>. A columnar database organizes data around columns instead of rows. This subtle difference optimizes some workloads—in particular, data warehouse and analytics applications that require computing aggregate values over very large sets of data—for certain kinds of problems. Databases that follow this orientation represent distributed hash tables (
<a href="http://michaelprimeaux.com/blog/2007/06/04/peer-to-peer-p2p/">DHTs</a>
). Examples of columnar databases are 
<a href="http://cassandra.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">Cassandra
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, Google 
<a href="http://research.google.com/archive/bigtable.html" class="link-external" rel="noopener noreferrer" target="_blank">Bigtable
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://hbase.apache.org" class="link-external" rel="noopener noreferrer" target="_blank">HBase
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, and 
<a href="http://hypertable.org" class="link-external" rel="noopener noreferrer" target="_blank">Hypertable
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</li>
</ul>
<p>It’s worth noting, a few databases exist that represent hybrid orientation implementations; for example, 
<a href="http://basho.com/riak/" class="link-external" rel="noopener noreferrer" target="_blank">Riak
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 is a hybrid implementation based on Amazon’s Dynamo in that it is both a document-oriented and key-value implementation.</p>
<p>Though, just as with relational databases, planning is key when thinking about launching a non-relational database project.  Non-relational database projects require a slightly different software engineering approach. In a relational model, we tend to consider our data model first and then we build queries to satisfy application requirements. Even though a database may be schema-less, in a non-relational model we 
<a href="http://tools.ietf.org/html/rfc2119" class="link-external" rel="noopener noreferrer" target="_blank">MUST
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 first understand an application’s queries as a starting point since that influences database and table layout.</p>
<p>Even companies with a large investment in relational-based analytics tools can leverage non-relational storage technologies but even so, the definition of a taxonomy (or multiple taxonomies) for analytics processing is of paramount importance.  Typically, companies that are heavily invested in relational technology as their primary systems of record employ extract transform and load (ETL) processes to periodically restructure information based on defined taxonomies into non-relational storage products so they are able to take advantage of more efficient and real-time processing using massively parallel analytics technologies such as Hadoop and Map Reduce.</p>
<p>Capturing data without analytics in mind from a business’ point of view is generally moot. Consider the goals, and what key data sets are needed to reach those goals. More important is to consider the set of business questions you are attempting to answer and, to that point, I strongly recommend you bring in a data scientist to perform an audit of an existing data architecture.</p>
<h2 id="mobile" class="heading">Mobile
    <a class="heading__anchor" href="#mobile" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>Business-to-consumer (B2C) mobile applications impose different requirements than that of business-to-business (B2B) and corporate applications not only in terms of marketing but in terms of the requirement for an extremely efficient and aesthetically pleasant user experience (UX) and more stringent software engineering practices.  It’s very difficult to recover from poor reviews and customer feedback.</p>
<p>In the mobile space, three companies represent the majority globally: Apple, Google, and Microsoft. Apple and Google make up 96% of the entire mobile space. Microsoft is less than 1% with the remaining (just over 3%) still on others such as RIM, Symbian, etc. If we add tablets to the equation (so now not just mobile phones) then as of April 2013 Apple iOS represents 59.04%, Google Android represents 26.02%, and Microsoft Windows Phone represents 1.14%; see 
<a href="http://www.netmarketshare.com/mobile-market-share" class="link-external" rel="noopener noreferrer" target="_blank">Net Marketshare
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<p>Across all mobile operating systems, if you observe how people use smartphones and look beyond telephone calls, email, and texting then you&rsquo;ll see that native applications dominate. Users spend on average, 82% of their mobile minutes with native applications and 18% with web browsers. They download approximately 50 to 75 applications to their phones (out of more than a million available) but regularly use about 10.</p>
<p>As another data point, mobile applications broadly fall into six categories:</p>
<ol>
<li>Games and Entertainment</li>
<li>Social Networks (
<a href="http://facebook.com" class="link-external" rel="noopener noreferrer" target="_blank">FaceBook
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://twitter.com" class="link-external" rel="noopener noreferrer" target="_blank">Twitter
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://pinterest.com" class="link-external" rel="noopener noreferrer" target="_blank">Pinterest
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://path.com" class="link-external" rel="noopener noreferrer" target="_blank">Path
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://tumblr.com" class="link-external" rel="noopener noreferrer" target="_blank">Tumblr
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, etc)</li>
<li>Utilities (maps, clocks, calendars, cameras, email, etc.)</li>
<li>Discovery (
<a href="http://www.yelp.com" class="link-external" rel="noopener noreferrer" target="_blank">Yelp
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://grubhub.com" class="link-external" rel="noopener noreferrer" target="_blank">GrubHub
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, etc.)</li>
<li>Education (
<a href="http://renkara.com/applications-accelastudy.php" class="link-external" rel="noopener noreferrer" target="_blank">AccelaStudy
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://contig.renkara.com" class="link-external" rel="noopener noreferrer" target="_blank">Contig
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://flashcardchampion.renkara.com" class="link-external" rel="noopener noreferrer" target="_blank">Flashcard Champion
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, etc.)</li>
<li>Brands (
<a href="http://www.nike.com" class="link-external" rel="noopener noreferrer" target="_blank">Nike
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, 
<a href="http://redbull.com" class="link-external" rel="noopener noreferrer" target="_blank">Red Bull
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, etc.)</li>
</ol>
<p>Regardless of your application’s category, native mobile applications offer the absolute best user experience in many aspects with frame rates, offline support, and access to the device’s hardware to name but a few. Contrary to what some may espouse, mobile devices are not well connected; even within major United States and European cities where connectivity is commonly sporadic and network latency is commonly high. Consider many scenarios whereby consumers pass in and out of good network connectivity while on mass transit systems; even mobile applications that “think” they are connected to a hi-fidelity wireless network may really be connected to a personal hotspot that is in turn connected to the internet over a cellular channel.  It is for these reasons I strongly discourage the creation of hybrid and HTML 5 mobile applications. Granted, hybrid and HTML 5 mobile applications may prove satisfactory in some corporate settings  but if you want your application to be among the 10 regularly used ones then write native applications; it’s that simple. And for the record, I am not a fan of “
<a href="http://en.wikipedia.org/wiki/Fourth-generation_programming_language" class="link-external" rel="noopener noreferrer" target="_blank">4GL
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
” Integrated Development Environments (IDE) that generate code for multiple mobile platforms. Perhaps I am a purist but I prefer to have direct access to and control over the core mobile platform rather than having to interact with process and technical abstractions. Your consumers do appreciate this level of attention to detail and UX refinement.</p>
<p>If you do write applications in the consumer space and if you&rsquo;ve purchased a smartphone or tablet recently, and if it doesn&rsquo;t have an Apple logo on it then it&rsquo;s likely you already know of Android. Android is an open sourced Linux-based operating system (OS) launched approximately 5 years ago as of the time of this writing. The Android operating system dates back to 2003 when Rich Milner, Nick Sears, Chris White, and Andy Rubin began working on a mobile operating system, which Google would eventually purchase in 2005.</p>
<p>For years, the dominant OS was Symbian, which has its roots in Psion&rsquo;s 
<a href="http://en.wikipedia.org/wiki/EPOC_%28operating_system%29" class="link-external" rel="noopener noreferrer" target="_blank">EPOC
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 OS.  By 2007, Symbian accounted for over 63% of the worldwide smartphone market. However, by 2008, when it was purchased by Nokia, that number dropped to 52%. At that time, the closest competitors were Research in Motion&rsquo;s Blackberry OS (16.6%), Microsoft Windows Mobile (11.8%), and the still very new iOS (8.2%). With Nokia&rsquo;s OS was poorly equipped to compete with iOS, RIM not wanting to leave its business focus comfort zone and Microsoft&rsquo;s mobile attempts clearly failing, it was Google&rsquo;s purchase of Android Inc in 2005 that would eventually provide competition. The Linux-based, open-source mobile operating system would eventually find its way to the market with the release of the T-Mobile G1 in 2008. The G1 sold well and that elevated Android to reach a 3.9% share in the smartphone market in 2009. At this point, iOS had doubled its share to 14.4%, RIM grew to just under 20%, Windows Mobile had dropped to 8.7%, and Symbian correspondingly dropped to 46.9%. Today, Android is used in 61% of the smartphones (excluding tablets) sold worldwide, which is primarily attributed to the fact it can be found in both high-end handsets (targeting the Apple iPhone) and in lower budget smartphones (where Symbian once dominated). Apple iOS commands 20.5% of the worldwide smartphone marketshare, which is impressive considering it&rsquo;s only included in Apple&rsquo;s own high-end phones. RIM is now down to less than 1.5%.</p>
<p>In general, it’s easier to get an application released onto Android, but from technical engineering and business points of view, Android does have significant disadvantages over Apple’s iOS:</p>
<ul>
<li>Google Play is not nearly as mature as Apple’s AppStore</li>
<li>People are reluctant to pay for Android applications</li>
<li>Piracy of applications is widespread</li>
<li>Security concerns and malware</li>
<li>Little to no enterprise penetration</li>
<li>OS upgrades are cumbersome, slow, and generally require intervention by the carrier</li>
</ul>
<p>However, one can’t ignore Android these days; and in particular for the consumer space. Corporate and business applications are a different matter altogether and I’ll save that discussion for a later point in time time.</p>
<h2 id="cloud" class="heading">Cloud
    <a class="heading__anchor" href="#cloud" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>So, where does cloud technology fit into all of this? Everywhere. Cloud is the cohesive technology between mobile, social, and big data. Over the past nearly 6 years, and after nearly 500 mobile applications shipped, I can think of exactly one mobile application I’ve written that did not involve hosted cloud services; and cloud providers are only getting better. Cloud providers such as Amazon 
<a href="http://aws.amazon.com" class="link-external" rel="noopener noreferrer" target="_blank">AWS
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and Microsoft Windows 
<a href="http://www.windowsazure.com" class="link-external" rel="noopener noreferrer" target="_blank">Azure
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 provide full featured platform-as-a-service (PaaS) and infrastructure-as-a-service (IaaS) offerings that complement solutions spanning the mobile, social, and non-relational database solution domains. There are other providers such as 
<a href="http://linode.com" class="link-external" rel="noopener noreferrer" target="_blank">Linode
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 that offer competitive IaaS offerings and with a very approachable cost model.  Discussing the technical offering of each cloud provider is not in scope of this post; perhaps at a later point in time.</p>
<p>The use of cloud computing technologies offer companies an opportunity to recognize cost savings that would otherwise not be possible with on-premise hardware. For example, it’s not uncommon to develop scripts that create an environment starting with mere credentials to a fully functioning application stack in a matter of minutes only to tear it down once the day is over. Patterns such as this are quite attractive to stakeholders.</p>
<h2 id="conclusion" class="heading">Conclusion
    <a class="heading__anchor" href="#conclusion" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>For organizations seeking a fast, easy and cost-effective way to manage business operations, the convergence of mobile, cloud, social, and non-relational storage technologies equips parallel and distributed system designers and business stakeholders with a unique opportunity and insight into new patterns that would otherwise require a much higher degree of cooperation and effort among disperate teams. an architecture that seeks signals, models them for their impact, and then adapts to the business process of the organization.  These technologies also facilitate pattern-based strategy architectures, which are attractive to both technical architects and business stakeholders. A pattern-based strategy identifies signals, models them for their impact, and then adapts to the business process of the organization instead of current strategies defined by making hypotheses, performing analyses, and reaching conclusions that are often wrong given the time to implement the change and pivot occurs at a slower pace than the tracked market trends.</p>
<p>Good architectures don’t just happen, they are designed.</p>
<p><strong>References:</strong></p>
<ul>
<li>Andrew McAfee and Erik Brynjolfsson. “Big Data: The Management Revolution”. Harvard Business Review,  October 2012.</li>
</ul>]]></content:encoded><category>software engineering</category><category>mobile</category></item><item><title>Universal Frameworks for iOS</title><link>https://michaelprimeaux.com/en/posts/2011-10-30-universal-frameworks-for-ios/</link><pubDate>Sun, 30 Oct 2011 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2011-10-30-universal-frameworks-for-ios/</guid><description>As of the time of this writing, Xcode does not offer a framework project to for Cocoa Touch (only Cocoa). Apple’s primary reasons behind this decision are security, performance, and memory footprint. As such Xcode limits iOS developers to the Static Library project type. Static libraries are quite cumbersome to work with since we must make compile-time decisions when working with the iOS Simulator or an iOS device. The iOS simulator on Mac OS X uses the i386 architecture whereas the iOS devices use either armv6 (opens in a new window) or armv7 (opens in a new window) .</description><content:encoded><![CDATA[<p>As of the time of this writing, Xcode does not offer a framework project to for Cocoa Touch (only Cocoa). Apple’s primary reasons behind this decision are security, performance, and memory footprint.  As such Xcode limits iOS developers to the Static Library project type. Static libraries are quite cumbersome to work with since we must make compile-time decisions when working with the iOS Simulator or an iOS device. The iOS simulator on Mac OS X uses the i386 architecture whereas the iOS devices use either 
<a href="http://en.wikipedia.org/wiki/ARM_architecture" class="link-external" rel="noopener noreferrer" target="_blank">armv6
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 or 
<a href="http://en.wikipedia.org/wiki/ARM_architecture" class="link-external" rel="noopener noreferrer" target="_blank">armv7
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<p>Additionally, iOS 5 introduces a new compile-time memory management feature known as 
<a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html" class="link-external" rel="noopener noreferrer" target="_blank">Automatic Reference Counting (ARC)
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
. The abridged version is you will never need to type retain or release again, which dramatically simplifies the development process while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps execute much faster, with predictable, smooth performance. However, it’ll take the community some time to refactor various open source libraries to support ARC. Therefore, we have a much larger need today than ever before to move these source files into static libraries that are compiled with ARC disabled so we can compile our primary applications with ARC enabled.</p>
<p>Alternatively, if you are including files that don’t yet support ARC in a project that is ARC enabled, you can set the -fno-objc-arc compiler flag for each of these files. To do this in Xcode, go to your active target and select the “Build Phases” tab. In the “Compiler Flags” column, set -fno-objc-arc for each of the source files. Of course, this can be a very laborious process.</p>
<p>Apple defines a 
<a href="http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html" class="link-external" rel="noopener noreferrer" target="_blank">framework
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 as a bundle (a structured directory) that contains a dynamic shared library along with associated resources, such as nib files, image files, and header files. When you develop an application, your project links to one or more frameworks. For example, iOS application projects link by default to the Foundation, UIKit, and Core Graphics frameworks. Your code accesses the capabilities of a framework through the application programming interface (API), which is published by the framework through its header files. Because the library is dynamically shared, multiple applications can access the framework code and resources simultaneously. The system loads the code and resources of a framework into memory, as needed, and shares the one copy of a resource among all applications. A universal (or multi-architecture) file is nothing more than an application bundle.</p>
<p>As I mentioned above, iOS does not support dynamic shared libraries but using 
<a href="http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/lipo.1.html" class="link-external" rel="noopener noreferrer" target="_blank">lipo
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (a tool which comes with 
<a href="http://developer.apple.com/devcenter/ios" class="link-external" rel="noopener noreferrer" target="_blank">iOS SDK
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
) we are able to merge several static libraries into a single static library. By using lipo, we are able to create a static library that supports all architectures (armv6, armv7, and i386) packaged as a universal framework. Here’s how.</p>
<p>Within Xcode, either create a new iOS project or open an existing iOS project. If you do decide to create a new project then feel free to choose any application template you prefer. Which one you choose is irrelevant. Add a “Cocoa Bundle” target (not Cocoa Touch Bundle). Select the newly created bundle target in the left navigation panel, select the “Build Settings” tab and modify the following 
<a href="http://developer.apple.com/library/mac/#documentation/DeveloperTools/Reference/XcodeBuildSettingRef/0-Introduction/introduction.html#//apple_ref/doc/uid/TP40003931-CH1-SW1" class="link-external" rel="noopener noreferrer" target="_blank">settings
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 as required:</p>
<ul>
<li><strong>Architectures</strong> (ARCHS): <em>Standard (armv6 armv7 armv7s)</em>. In Xcode 4.2, use the value of $(ARCHS_STANDARD_32_BIT). If you wish to compile for older devices then add a new line with a value of armv6. Please see 
<a href="https://devforums.apple.com/message/536368#536368" class="link-external" rel="noopener noreferrer" target="_blank">this
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 Apple Developer forum post for more details.</li>
<li><strong>Base SDK</strong> (SDKROOT). <em>Latest iOS (iOS X.X)</em>.</li>
<li><strong>Build Active Architecture Only</strong> (ONLY_ACTIVE_ARCH). <em>No</em>. This allows us to compile for armv6 and armv7.</li>
<li><strong>Valid Architecture</strong> (VALID_ARCHS): <em>Standard (armv6 armv7 armv7s)</em>. In Xcode 4.2 and greater, use the value of $(ARCHS_STANDARD_32_BIT). If you wish to compile for older devices then add a new line with a value of armv6.</li>
<li><strong>Dead Code Stripping</strong> (DEAD_CODE_STRIPPING): <em>No</em></li>
<li><strong>Link With Standard Libraries</strong> (LINK_WITH_STANDARD_LIBRARIES): <em>No</em></li>
<li><strong>Mach-O Type</strong> (MACH_O_TYPE): <em>Relocatable Object File</em>. This is the most important change. Here, we instruct the compiler to treat the Bundle as a relocatable file, by doing this, we can turn it into a framework with the wrapper setting.</li>
<li><strong>Wrapper Extension</strong> (WRAPPER_EXTENSION). <em>framework</em>. Here we change the Bundle to a Framework. To Xcode, frameworks is just a folder with the extension .framework, which has inside one or more compiled binary sources, resources and some folders, a folder, usually called Headers, contains all the public headers.</li>
<li><strong>Generate Debug Symbols</strong> (GCC_GENERATE_DEBUGGING_SYMBOLS): <em>No</em>.</li>
<li><strong>Generate Position-Dependent Code</strong> (GCC_DYNAMIC_NO_PIC): <em>No</em></li>
<li><strong>Targeted Device Family</strong> (TARGETED_DEVICE_FAMILY). <em>iPhone/iPad</em>.</li>
</ul>
<p>Here are the steps&hellip;</p>
<ul>
<li>With the bundle target still in focus, select the “Info” tab and ensure the “Bundle OS Type code” is equal to FMWK.</li>
<li>Add any source code and resources to the bundle. With the bundle target selected, click the “Build Phase” tab. At the bottom, press the “Add Phase” button and then “Add Copy Headers“.</li>
<li>Open the newly created “Copy Headers” section and separate your public headers from private or project headers.</li>
<li>Open the “Compile Source” section and add any .m, .c, .mm, .cpp and any other compilable source file. If your framework contains any non-compilable files such as images, sounds, and other resources, you can add them to the “Copy Bundle Resources” section.</li>
</ul>
<p>You can access any non-compilable resources by using NSBundle.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-objc" data-lang="objc"><span class="line"><span class="cl"><span class="p">[[</span><span class="n">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="nl">pathForResource</span><span class="p">:</span><span class="s">@&#34;MyFramework.framework/Resources/FileName&#34;</span> <span class="nl">ofType</span><span class="p">:</span><span class="s">@&#34;fileExtension&#34;</span><span class="p">];</span>
</span></span></code></pre></div><p>Next we’ll create a new Aggregate Target. As mentioned earlier, to join both architectures products into one, we must to use lipo.</p>
<ul>
<li>Add a new target by pressing the “Add Target” button.</li>
<li>Under the Cocoa Touch section, select the “Other” subcategory and then choose the “Aggregate” target. The “Product Name” is arbitrary.</li>
<li>Add a new “Run Script” phase under this newly created target. Copy and paste the following script into the “Run Script” phase.</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># The framework name and version</span>
</span></span><span class="line"><span class="cl"><span class="nv">X_FRAMEWORK_NAME</span><span class="o">=</span>CoreFramework
</span></span><span class="line"><span class="cl"><span class="nv">X_FRAMEWORK_VERSION</span><span class="o">=</span>A
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># This folder contains the final output of the framework.</span>
</span></span><span class="line"><span class="cl"><span class="nv">X_INSTALL_DIR</span><span class="o">=</span><span class="si">${</span><span class="nv">SRCROOT</span><span class="si">}</span>/Products/<span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span>.framework
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># This working directory will be deleted after completion.</span>
</span></span><span class="line"><span class="cl"><span class="nv">X_WORKING_DIR</span><span class="o">=</span>build
</span></span><span class="line"><span class="cl"><span class="nv">X_DEVICE_DIR</span><span class="o">=</span><span class="si">${</span><span class="nv">X_WORKING_DIR</span><span class="si">}</span>/<span class="si">${</span><span class="nv">CONFIGURATION</span><span class="si">}</span>-iphoneos/<span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span>.framework
</span></span><span class="line"><span class="cl"><span class="nv">X_SIMULATOR_DIR</span><span class="o">=</span><span class="si">${</span><span class="nv">X_WORKING_DIR</span><span class="si">}</span>/<span class="si">${</span><span class="nv">CONFIGURATION</span><span class="si">}</span>-iphonesimulator/<span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span>.framework
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;******************************************************&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;X_DEVICE_DIR = </span><span class="si">${</span><span class="nv">X_DEVICE_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;X_SIMULATOR_DIR = </span><span class="si">${</span><span class="nv">X_SIMULATOR_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;******************************************************&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;SYMROOT = </span><span class="si">${</span><span class="nv">SYMROOT</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;OBJROOT = </span><span class="si">${</span><span class="nv">OBJROOT</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;PROJECT_DIR = </span><span class="si">${</span><span class="nv">PROJECT_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;CONFIGURATION_BUILD_DIR = </span><span class="si">${</span><span class="nv">CONFIGURATION_BUILD_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;CONFIGURATION = </span><span class="si">${</span><span class="nv">CONFIGURATION</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;CONFIGURATION_TEMP_DIR = </span><span class="si">${</span><span class="nv">CONFIGURATION_TEMP_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;DERIVED_FILE_DIR = </span><span class="si">${</span><span class="nv">DERIVED_FILE_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;BUILD_PRODUCTS_DIR = </span><span class="si">${</span><span class="nv">BUILT_PRODUCTS_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;BUILD_DIR = </span><span class="si">${</span><span class="nv">BUILD_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;TARGET_TEMP_DIR = </span><span class="si">${</span><span class="nv">TARGET_TEMP_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;PROJECT_TEMP_DIR = </span><span class="si">${</span><span class="nv">PROJECT_TEMP_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;PRODUCT_NAME = </span><span class="si">${</span><span class="nv">PRODUCT_NAME</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;******************************************************&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Build both simulator and device architectures.</span>
</span></span><span class="line"><span class="cl">xcodebuild clean
</span></span><span class="line"><span class="cl">xcodebuild -configuration <span class="si">${</span><span class="nv">CONFIGURATION</span><span class="si">}</span> -target <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span> -sdk iphoneos -SYMROOT<span class="o">=</span><span class="si">${</span><span class="nv">SYMROOT</span><span class="si">}</span> -OBJROOT<span class="o">=</span><span class="si">${</span><span class="nv">OBJROOT</span><span class="si">}</span>
</span></span><span class="line"><span class="cl">xcodebuild -configuration <span class="si">${</span><span class="nv">CONFIGURATION</span><span class="si">}</span> -target <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span> -sdk iphonesimulator -SYMROOT<span class="o">=</span><span class="si">${</span><span class="nv">SYMROOT</span><span class="si">}</span> -OBJROOT<span class="o">=</span><span class="si">${</span><span class="nv">OBJROOT</span><span class="si">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Clean the oldest.</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="o">[</span> -d <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">&#34;</span> <span class="o">]</span>
</span></span><span class="line"><span class="cl"><span class="k">then</span>
</span></span><span class="line"><span class="cl">rm -rf <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Recreate the folder structure for the final product binaries.</span>
</span></span><span class="line"><span class="cl">mkdir -p <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">mkdir -p <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions&#34;</span>
</span></span><span class="line"><span class="cl">mkdir -p <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">mkdir -p <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/Resources&#34;</span>
</span></span><span class="line"><span class="cl">mkdir -p <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/Headers&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Create the required symbolic links. Please note the paths MUST relative, </span>
</span></span><span class="line"><span class="cl"><span class="c1"># otherwise the symbolic links will be invalid when the folder is copied/moved.</span>
</span></span><span class="line"><span class="cl">ln -s <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/Current&#34;</span>
</span></span><span class="line"><span class="cl">ln -s <span class="s2">&#34;Versions/Current/Headers&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Headers&#34;</span>
</span></span><span class="line"><span class="cl">ln -s <span class="s2">&#34;Versions/Current/Resources&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Resources&#34;</span>
</span></span><span class="line"><span class="cl">ln -s <span class="s2">&#34;Versions/Current/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Copy the headers and resources files to the final product folder.</span>
</span></span><span class="line"><span class="cl">cp -R <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_DEVICE_DIR</span><span class="si">}</span><span class="s2">/Headers/&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/Headers/&#34;</span>
</span></span><span class="line"><span class="cl">cp -R <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_DEVICE_DIR</span><span class="si">}</span><span class="s2">/&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/Resources/&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Remove artifacts from the resources folder.</span>
</span></span><span class="line"><span class="cl">rm -r <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/Resources/Headers&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/Resources/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Use lipo to merge both binary files (i386 + armv6/armv7) into one universal files.</span>
</span></span><span class="line"><span class="cl">lipo -create <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_DEVICE_DIR</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_SIMULATOR_DIR</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span> -output <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_INSTALL_DIR</span><span class="si">}</span><span class="s2">/Versions/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_VERSION</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">X_FRAMEWORK_NAME</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Remove the working directory</span>
</span></span><span class="line"><span class="cl">rm -r <span class="s2">&#34;</span><span class="si">${</span><span class="nv">X_WORKING_DIR</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span></code></pre></div><p>You’ll need to modify the first non-comment line in the script to be equal to the product name of your framework bundle target.</p>
<p>Now build the Aggregate target. It does not matter which architecture you select to build (iOS Device or Simulator) since the script creates a working folder, compiles the framework target twice (once for the iOS device and once for the iOS Simulator) and generates the output to a folder named “Products” located in the $(SRCROOT) root folder</p>
<p>If you previously upgraded from Xcode 3.x to 4.2, then your existing (SYMROOT) value will very likely cause the above script to fail. The failure is caused because the script expects your (SYMROOT) value to be set to the new Xcode 4 value of “build”, which refers to a relative path to $(SRCROOT). Unfortunately, you are not able to modify this setting in Xcode 4 due to an already reported bug. To work around this bug, close Xcode and simply delete the existing SYMROOT key and value from your com.apple.dt.Xcode.plist file, which is located in ~/Library/Preferences/.</p>
<p>Isn’t it about time for the WWDC to be here again?</p>]]></content:encoded><category>software engineering</category><category>apple</category></item><item><title>Stop the line so the line never stops.</title><link>https://michaelprimeaux.com/en/posts/2011-09-26-stop-the-line-so-the-line-never-stops/</link><pubDate>Mon, 26 Sep 2011 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2011-09-26-stop-the-line-so-the-line-never-stops/</guid><description/><content:encoded><![CDATA[<p>I recently read 
<a href="http://www.amazon.com/Lean-Startup-Entrepreneurs-Continuous-Innovation/dp/0307887898" class="link-external" rel="noopener noreferrer" target="_blank">The Lean Startup
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and a corresponding 
<a href="http://techcrunch.com/2011/09/25/founder-stories-eric-ries-lean-startups-stop-the-line-so-the-line/" class="link-external" rel="noopener noreferrer" target="_blank">interview
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 by 
<a href="http://www.crunchbase.com/person/chris-dixon" class="link-external" rel="noopener noreferrer" target="_blank">Chris Dixon
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (
<a href="http://twitter.com/#!/cdixon" class="link-external" rel="noopener noreferrer" target="_blank">@cdixon
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
) at 
<a href="http://www.crunchbase.com/" class="link-external" rel="noopener noreferrer" target="_blank">CrunchBase
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 with the author, 
<a href="http://www.crunchbase.com/person/eric-ries" class="link-external" rel="noopener noreferrer" target="_blank">Eric Ries
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 (
<a href="http://twitter.com/#!/ericries" class="link-external" rel="noopener noreferrer" target="_blank">@ericries
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
).  Ries tells Dixon one of the phrases Toyota uses on the production line is “stop the line so that the line never stops.” It means “if you want to be able to sustainably have high productivity you have to stop as soon as you have a quality problem and remove it because quality problems pile up and compound… eventually you can grind your whole development organization to a halt.”</p>
<p>One of the more interesting concepts (to me anyway) is the notion of continuous deployment, which is a natural extension of 
<a href="http://en.wikipedia.org/wiki/Continuous_integration" class="link-external" rel="noopener noreferrer" target="_blank">Continuous Integration
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
. Continuously integrate (commit early and often). On commit automatically run all tests. If the tests pass deploy to the cluster. If the deploy succeeds, repeat.</p>
<p>The book is really quite eye-opening and offers a different way of thinking about not only the process of software engineering (by noting a slightly modified version of the 
<a href="http://en.wikipedia.org/wiki/Agile_software_development" class="link-external" rel="noopener noreferrer" target="_blank">Agile
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 development methodology) but the impact of a change to revenue by ignoring 
<a href="http://techcrunch.com/2011/07/30/vanity-metrics" class="link-external" rel="noopener noreferrer" target="_blank">vanity metrics
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<p>I highly recommend this book for anyone who wants to create something new.</p>]]></content:encoded><category>software engineering</category></item><item><title>Efficient Memory Usage in iOS</title><link>https://michaelprimeaux.com/en/posts/2011-06-19-efficient-memory-usage-in-ios/</link><pubDate>Sun, 19 Jun 2011 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2011-06-19-efficient-memory-usage-in-ios/</guid><description>I wrote a post some time back on the value of singletons in a garbage collected language as an important component of scalability. The majority of my work these days targets Apple iOS (opens in a new window) devices and so a reduced memory footprint and overall memory management is not only fundamental but paramount. The use of singleton objects and an effective caching strategy are simple patterns to get you started.</description><content:encoded><![CDATA[<p>I wrote a 
<a href="/blog/2011/05/11/a-word-on-elegance/">post</a>
 some time back on the value of singletons in a garbage collected language as an important component of scalability. The majority of my work these days targets Apple 
<a href="http://en.wikipedia.org/wiki/IOS_%28Apple%29" class="link-external" rel="noopener noreferrer" target="_blank">iOS
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 devices and so a reduced memory footprint and overall memory management is not only fundamental but paramount. The use of singleton objects and an effective caching strategy are simple patterns to get you started.</p>
<p>I suggest reading Apple’s 
<a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading" class="link-external" rel="noopener noreferrer" target="_blank">Thread Programming Guide
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 as a prerequisite, which also covers some of the basic concepts behind Blocks and 
<a href="http://developer.apple.com/library/mac/#featuredarticles/BlocksGCD/_index.html" class="link-external" rel="noopener noreferrer" target="_blank">Grand Central Dispatch (GCD)
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<h2 id="singletons" class="heading">Singletons
    <a class="heading__anchor" href="#singletons" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>For purposes of this discussion, a singleton is a class that only allows a single instance of itself to be created within an operating system process.</p>
<p>Apple’s 
<a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html%23//apple_ref/doc/uid/TP40002974-CH4-SW32" class="link-external" rel="noopener noreferrer" target="_blank">documentation
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 on creating a singleton recommends a pattern that works for most iOS applications but is not thread-safe. Beginning with iOS 2.0 and prior to iOS 4.1, one of the most widely used methods to introduce a thread-safe singleton was through the use of the 
<a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW3" class="link-external" rel="noopener noreferrer" target="_blank">@synchronized
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 directive.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-objc" data-lang="objc"><span class="line"><span class="cl"><span class="p">+</span> <span class="p">(</span><span class="n">MyClass</span><span class="o">*</span><span class="p">)</span> <span class="nf">instance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="n">MyClass</span> <span class="o">*</span><span class="n">gInstance</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">@synchronized</span><span class="p">(</span><span class="nb">self</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="nb">nil</span> <span class="o">==</span> <span class="n">gInstance</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">gInstance</span> <span class="o">=</span> <span class="p">[[</span><span class="n">MyClass</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="p">(</span><span class="n">gInstance</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>There are a few inefficiencies with this approach because not only generates a recursive mutex lock but also introduces an exception handler. Specifically, Apple’s documentation indicates that as a precautionary measure, the @synchronized block implicitly adds an exception handler to the protected code. This handler automatically releases the mutex in the event that an exception is thrown. This means that in order to use the @synchronized directive, you must also enable Objective-C exception handling in your code. If you do not want the additional overhead caused by the implicit exception handler, you should consider using the lock classes.</p>
<p>At the time of this writing, the @synchronized directive turns into this basic pseudo-code:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-objc" data-lang="objc"><span class="line"><span class="cl"><span class="kt">id</span> <span class="n">_eval_once</span> <span class="o">=</span> <span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">objc_sync_enter</span><span class="p">(</span> <span class="n">_eval_once</span> <span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">@try</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="cm">/* code goes here */</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="k">@finally</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">objc_sync_exit</span><span class="p">(</span> <span class="n">_eval_once</span> <span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>For implementation details see: 
<a href="http://www.opensource.apple.com/source/objc4/objc4-437.1/runtime/objc-sync.m" class="link-external" rel="noopener noreferrer" target="_blank">http://www.opensource.apple.com/source/objc4/objc4-437.1/runtime/objc-sync.m
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
</p>
<p>Much of Apple’s sample code prior to iOS 4.0 use this approach. There is a technically more efficient and lock-free approach for applications that target operating systems earlier than Mac OS X version 10.5 or iOS 4.0.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-objc" data-lang="objc"><span class="line"><span class="cl"><span class="p">+</span> <span class="p">(</span><span class="n">MyClass</span><span class="o">*</span><span class="p">)</span> <span class="nf">gInstance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="kt">void</span> <span class="o">*</span> <span class="k">volatile</span> <span class="n">gInstance</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">gInstance</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">MyClass</span> <span class="o">*</span><span class="n">temp</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyClass</span> <span class="p">[</span><span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">OSAtomicCompareAndSwapPtrBarrier</span><span class="p">(</span><span class="mh">0x0</span><span class="p">,</span> <span class="n">temp</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">gInstance</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span><span class="n">temp</span> <span class="k">release</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="p">(</span><span class="n">gInstance</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>You can read more about preferred versions of the atomic and synchronization operations 
<a href="http://developer.apple.com/library/mac/#documentation/DriversKernelHardware/Reference/libkern_ref/OSAtomic_h/index.html" class="link-external" rel="noopener noreferrer" target="_blank">here
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<p>For applications that target operating systems equal to or greater than Mac OS X 10.6 or iOS 4.0, the recommended singleton pattern uses dispatch_once, which relies on components of Grand Central Dispatch (GCD).</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-objc" data-lang="objc"><span class="line"><span class="cl"><span class="p">+</span> <span class="p">(</span><span class="n">MyClass</span> <span class="o">*</span><span class="p">)</span> <span class="nf">instance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="n">MyClass</span><span class="o">*</span> <span class="n">gInstance</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="n">dispatch_once_t</span> <span class="n">pred</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="n">dispatch_once</span><span class="p">(</span><span class="o">&amp;</span><span class="n">pred</span><span class="p">,</span> <span class="o">^</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">gInstance</span> <span class="o">=</span> <span class="p">[[</span><span class="n">MyClass</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="p">});</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="p">(</span><span class="n">gInstance</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>It should go without saying but I’ll do so for completeness. All instance-level methods of classes that implement a singleton pattern 
<a href="http://www.ietf.org/rfc/rfc2119.txt" class="link-external" rel="noopener noreferrer" target="_blank">MUST
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 be thread-safe.</p>
<h2 id="nscache" class="heading">NSCache
    <a class="heading__anchor" href="#nscache" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h2>
<p>I suspect this class remains relatively unused by most iOS developers. 
<a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSCache_Class/Reference/Reference.html" class="link-external" rel="noopener noreferrer" target="_blank">NSCache
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 is essentially a container that it stores key-value pairs but unfortunately does so without an O(1) time complexity (like that of 
<a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html" class="link-external" rel="noopener noreferrer" target="_blank">NSMutableDictionary
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
) but does automatically evicts objects from its store when the ‘cost’ (a heuristic that of course involves memory pressure) of the cache rises above a configurable threshold.</p>
<p>Per Apple’s documentation, NSCache objects differ from other mutable collections in a few ways:</p>
<ul>
<li>The NSCache class incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.</li>
<li>You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.</li>
<li>Retrieving something from an NSCache object returns an autoreleased result.</li>
<li>Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.These features are necessary for the NSCache class, as the cache may decide to automatically mutate itself asynchronously behind the scenes if it is called to free up memory.</li>
</ul>
<p>Though not required, NSCache works in conjunction with objects that implement the 
<a href="http://developer.apple.com/library/mac/documentation/cocoa/reference/NSDiscardableContent_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSDiscardableContent" class="link-external" rel="noopener noreferrer" target="_blank">NSDiscardableContent
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 protocol in that the 
<a href="http://developer.apple.com/library/mac/documentation/cocoa/reference/NSDiscardableContent_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSDiscardableContent/discardContentIfPossible" class="link-external" rel="noopener noreferrer" target="_blank">discardContentIfPossible
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 method is called when an object is removed.</p>
<p>The two most widely used caching strategies are proactive and reactive loading.</p>
<ol>
<li><strong>Proactive Cache Loading</strong>. A proactive cache loading strategy attempts to retrieve all required state when a process or application starts and cache it for the lifetime of the process or application. A decision to use a proactive cache loading strategy is generally done so in conjunction with either asynchronous pull loading based on expected not actual usage of the information being cached or notification-based loading whereby an application’s services are notified when cached state changes.</li>
<li><strong>Reactive Cache Loading</strong>. A reactive cache loading strategy retrieves data as it is requested by the application and caches it for future requests. A decision to use a reactive cache loading strategy is generally done so in conjunction with synchronous pull loading since the pattern is relatively easy to test and implement.</li>
</ol>
<p>Regardless of the selected caching strategy, determining a cache expiration policy is is key for most applications. There are a plethora of expiration policy patterns but the majority of them fall into the following general categories:</p>
<ul>
<li><strong>Time-based</strong>. Cached information is invalidated based on relative (sliding window) or absolute time periods.</li>
<li><strong>Notification-based</strong>. Cached information is invalidated based on instructions from a source.</li>
<li><strong>Counter-based</strong>. Cached information is invalidated based on a reference counts.</li>
</ul>
<p>Regardless of the expiration policy, scavenging based on memory pressure and other heuristics are key to a proper cache design and symmetry of that design is a key component in any distributed algorithm.</p>
<p>For most applications, a reactive cache loading strategy with a synchronous pull model is the most appropriate option. NSCache does evict objects based on memory pressure and so I strongly recommend you implement the NSDiscardableContent protocol for any objects you plan to store in NSCache. NSDiscardableContent is a simple counter-based pattern. For information stored in NSCache that represents NSData objects, Apple provides the 
<a href="http://developer.apple.com/library/mac/documentation/cocoa/reference/NSPurgeableData_Class/Reference/Reference.html#//apple_ref/occ/cl/NSPurgeableData" class="link-external" rel="noopener noreferrer" target="_blank">NSPurgeableData
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 class. NSPurgeable data inherits from NSMutableData and is available in iOS 4.0 and Mac OS X 10.6 and later. A description of how to use this class can be found be reading 
<a href="http://developer.apple.com/library/mac/#technotes/CachingPurgeableMemory/Introduction/Introduction.html" class="link-external" rel="noopener noreferrer" target="_blank">Caching and Purgeable Memory
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</p>
<p>I’d like to make one last point. Please be careful not to over-engineer your caching strategy but be elegant about your design.</p>]]></content:encoded><category>software engineering</category><category>apple</category></item><item><title>A Word on Elegance</title><link>https://michaelprimeaux.com/en/posts/2011-05-11-a-word-on-elegance/</link><pubDate>Wed, 11 May 2011 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2011-05-11-a-word-on-elegance/</guid><description>What do I mean by elegance? Most software engineers equate elegance to efficiency. Still some equate elegance to robustness. Others choose correctness. All are variables in the equation of elegance. Many other variables exist and all are important.
Make no mistake, software is art. Some artists prefer oil on canvas as their medium. Others choose water color. As software design engineers (SDEs), we have chosen 1s and 0s to communicate our creative visions. Elegance must exist at each step in the engineering process and not only as an afterthought in the development phase. Elegance is the responsibility of each and every participant with accountability functioning as the enforcement point. Without accountability, chaos reigns. Accountability starts within.</description><content:encoded><![CDATA[<p>What do I mean by elegance? Most software engineers equate elegance to efficiency. Still some equate elegance to robustness. Others choose correctness. All are variables in the equation of elegance. Many other variables exist and all are important.</p>
<p>Make no mistake, software is art. Some artists prefer oil on canvas as their medium. Others choose water color. As software design engineers (SDEs), we have chosen 1s and 0s to communicate our creative visions. Elegance must exist at each step in the engineering process and not only as an afterthought in the development phase. Elegance is the responsibility of each and every participant with accountability functioning as the enforcement point. Without accountability, chaos reigns. Accountability starts within.</p>
<p>Many software engineers find themselves searching for or memorizing patterns that “work” to shortly find themselves bulimically spewing the results onto their canvas. What happened to the elegance of originality? It’s far too easy to become a GP (“Google programmer”). Why? Because creativity and originality aren’t as convenient as letting others do the work, which leads to chaos and hard-to-maintain code; especially, when you’re not the original developer.</p>
<p>Stay with me; we’re only scratching the surface. Do you remember the last time you really evaluated the usability of your consumer API surface? Did your evaluation begin in the design phase? Start with a high-level view of the scenarios. Then oscillate between architectural design and the scenario-based consumer API surface. Do not think about technical design until you’re satisfied with the consumer API surface and the architectural design. Keep in mind; it’s certainly acceptable to refactor your architectural design and consumer API based on the technical design.</p>
<p>An apparent and even larger obstacle I’ve noticed lately is the inability for an SDE to make the jump from procedure (function)-oriented design concepts to that of object-oriented design while keeping an eye to efficiency in terms computational complexity and network round-trips. The devil is in the details. In my opinion, one can’t be an effective architect without being an even more effective SDE; period. The implementation details do change the architecture.</p>
<p>In general, people tend to migrate toward what makes them feel good. Rework doesn’t feel good; no one enjoys it. But, if that was true—if you truly don’t enjoy rework—then why would you not elegantly architect, design, and develop your code initially? Why do you make more work for yourself? Elegance as a foundational element is difficult. More often than not, SDEs take the easy way out because they equate efficiency to speed of completion. I’m not advocating “analysis paralysis” but instead a balanced design. Deadlines must be met. Deadlines should not be met at the expense of elegance.</p>
<p>It’s your canvas.</p>
]]></content:encoded><category>computing</category><category>software engineering</category></item><item><title>MethodImplOptions.Synchronized</title><link>https://michaelprimeaux.com/en/posts/2008-01-09-methodimploptions-dot-synchronized/</link><pubDate>Wed, 09 Jan 2008 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2008-01-09-methodimploptions-dot-synchronized/</guid><description>Recent discussions with colleagues regarding use of MethodImplOptions.Synchronized (opens in a new window) prompted me to discuss its implications.
Let’s begin with formal documentation. The MethodImplOptions.Synchronized option “specifies that the method can be executed by only one thread at a time. Static methods lock on the type, while instance methods lock on the instance. Only one thread can execute in any of the instance functions and only one thread can execute in any of a class’s static functions”.</description><content:encoded><![CDATA[<p>Recent discussions with colleagues regarding use of 
<a href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions%28VS.71%29.aspx" class="link-external" rel="noopener noreferrer" target="_blank">MethodImplOptions.Synchronized
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 prompted me to discuss its implications.</p>
<p>Let’s begin with formal documentation. The MethodImplOptions.Synchronized option “specifies that the method can be executed by only one thread at a time. Static methods lock on the type, while instance methods lock on the instance. Only one thread can execute in any of the instance functions and only one thread can execute in any of a class’s static functions”.</p>
<p>I’d like to focus on the statement “static methods lock on the type”. Microsoft strongly discourages locking on any public types, or on instances you do not control. This means the common constructs lock(this), lock(typeof(SomeType)), and lock(“myLock”) violate this guideline. If you’re not yet familiar with the reason behind this guideline then please read 
<a href="http://www.bluebytesoftware.com/blog/default.aspx" class="link-external" rel="noopener noreferrer" target="_blank">Joe Duffy’s
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 blog entry titled “
<a href="http://www.bluebytesoftware.com/blog/2005/09/29/RudeUnloadsAndOrphanedLocks.aspx" class="link-external" rel="noopener noreferrer" target="_blank">Rude unloads and orphaned locks
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
“. To provide additional support for Joe’s comments, the formal documentation for MethodImplOptions states “Locking on the instance or on the type, as with the Synchronized flag, is not recommended for public types because code other than your own can take locks on public types and instances. This might cause deadlocks or other synchronization problems.”</p>
<p>To be explicit, the lock(typeof(MyClass)) semantic is exactly what MethodImplOptions.Synchronized implements when adorned on static methods . The lock in this case is across all 
<a href="http://msdn2.microsoft.com/en-us/library/system.appdomain.aspx" class="link-external" rel="noopener noreferrer" target="_blank">AppDomains
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 in the same operating system process. Consider the following code, which if executed from multiple AppDomains would result in a conflict.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="n">ManualResetEvent</span> <span class="n">e1</span><span class="p">=</span> <span class="k">new</span> <span class="n">ManualResetEvent</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">lock</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="kt">object</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">AppDomain</span> <span class="n">domain</span> <span class="p">=</span> <span class="n">AppDomain</span><span class="p">.</span><span class="n">CreateDomain</span><span class="p">(</span><span class="s">&#34;MyDomain&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">domain</span><span class="p">.</span><span class="n">DoCallBack</span><span class="p">(</span><span class="k">delegate</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">ThreadPool</span><span class="p">.</span><span class="n">QueueUserWorkItem</span><span class="p">(</span><span class="k">delegate</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">ManualResetEvent</span> <span class="n">e2</span><span class="p">=</span> <span class="k">new</span> <span class="n">ManualResetEvent</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="k">lock</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="kt">object</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">e2</span><span class="p">.</span><span class="n">Set</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">});</span>
</span></span><span class="line"><span class="cl">    <span class="p">});</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="n">e1</span><span class="p">.</span><span class="n">WaitOne</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>As a result, you should shy away from using MethodImplOptions.Synchronized for the same reasons you wouldn’t use lock(this) or lock(typeof(SomeClass)).</p>
<p>Additionally, the MethodImplOptions.Synchronized option favors neither readers nor writers. Use of a low-locking technique such as a 
<a href="http://en.wikipedia.org/wiki/Spinlock" class="link-external" rel="noopener noreferrer" target="_blank">spinlock
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 or the intrinsic 
<a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" class="link-external" rel="noopener noreferrer" target="_blank">ReaderWriterLockSlim
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and 
<a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.aspx" class="link-external" rel="noopener noreferrer" target="_blank">Monitor
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 classes will perform and scale much better on both single-core and multi-core (CPU) hardware.</p>
<p>The above points are important for several reasons:</p>
<ol>
<li>The scope of a lock is very relevant if a goal of your code is reuse. As a framework writer is it your responsibility to ensure consumers writing applications with differing scalability thresholds and concurrency rates are equally favored; the solution should either work for all consumers or offer options for each category of consumer.</li>
<li>If you’re writing code that is to be used in systems where efficient use of system resources (CPU and memory) is paramount then it is your responsibility to provide an implementation that meets this fundamental goal.</li>
<li>If you’re executing under the Microsoft SQL Server CLR host and your code takes too long to execute then SQL will force an AppDomain unload, which means (as a specific example), that a finally block wishing to execute Monitor.Exit won’t even be run. As Joe indicates, until you’ve created 4,294,967,295 threads such that the Thread IDs wrap around and the old ID gets assigned to a new thread, and that thread spuriously decides to Exit the Monitor without acquiring it first, your system is going to be locked up for a bit. In other words, deadlocked.</li>
<li>The MethodImplAttribute can only be used to adorn (you guessed it) methods.</li>
</ol>
<p>So how much of a difference can a low-locking technique really make? That obviously depends on the technique. I wrote a parallelized program (using the 
<a href="http://www.distributethis.com/post/2007/12/Microsoft-Parallel-Extensions-to-NET-Framework-35.aspx" class="link-external" rel="noopener noreferrer" target="_blank">Microsoft Parallel Extensions for .NET 3.5
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
) that tests synchronization using MethodImplOptions.Synchronized, Monitor, Interlocked, ReaderWriterLockSlim, and a spin-lock. For those interested, I’ve included my spin-lock implementation to the bottom of this article.</p>
<p><strong>Result Set 1: Write-only access:</strong></p>
<ul>
<li>Monitor executed 10,000,000 iterations in 13,372,540 (ticks), which is 747 iterations per tick.</li>
<li>Interlocked executed 10,000,000 iterations in 18,767,746 (ticks), which is 532 iterations per tick.</li>
<li>ReaderWriterLockSlim executed 10,000,000 iterations in 18,908,171 (ticks), which is 528 iterations per tick.</li>
<li>MethodImpl executed 10,000,000 iterations in 22,724,176 (ticks), which is 440 iterations per tick.</li>
<li>SpinLock executed 10,000,000 iterations in 17,713,122 (ticks), which is 564 iterations per tick.</li>
</ul>
<p><strong>Result Set 2: Read/write access with an equal read and write frequency:</strong></p>
<ul>
<li>Monitor executed 10,000,000 iterations in 23,701,864 (ticks), which is 421 executions iterations per tick.</li>
<li>Interlocked executed 10,000,000 iterations in 19,864,150 (ticks), which is 503 iterations per tick.</li>
<li>ReaderWriterLockSlim executed 10,000,000 iterations in 33,751,562 (ticks), which is 296 iterations per tick.</li>
<li>MethodImpl executed 10,000,000 iterations in 43,442,379 (ticks), which is 230 iterations per tick.</li>
<li>SpinLock executed 10,000,000 iterations in 20,713,905 (ticks), which is 482 iterations per tick.</li>
</ul>
<p><strong>Result Sets 3: Read-only access:</strong></p>
<ul>
<li>Monitor executed 10,000,000 iterations in 13,810,171 (ticks), which is 724 iterations per tick.</li>
<li>Interlocked executed 10,000,000 iterations in 17,628,526 (ticks), which is 567 iterations per tick.</li>
<li>ReaderWriterLockSlim executed 10,000,000 iterations in 20,326,949 (ticks), which is 491 iterations per tick.</li>
<li>MethodImpl executed 10,000,000 iterations in 34,891,919 (ticks), which is 286 iterations per tick.</li>
<li>SpinLock executed 10,000,000 iterations in 7,046,085 (ticks), which is 1,419 iterations per tick.</li>
</ul>
<p>The results are not surprising. Use of the MethodImplOptions.Synchronized option on static methods resulted in a lower concurrency rate. If you require thread synchronization for static methods then I strongly recommend you consider an alternate synchronization technique.</p>
<p>The full source code can be found 
<a href="/files/SpinLock.zip">here</a>
.</p>]]></content:encoded><category>computing</category><category>software engineering</category></item><item><title>Singletons in C#</title><link>https://michaelprimeaux.com/en/posts/2007-12-13-singletons-in-c-sharp/</link><pubDate>Thu, 13 Dec 2007 00:06:00 +0000</pubDate><author>michael@michaelprimeaux.com (Michael Primeaux)</author><guid>https://michaelprimeaux.com/en/posts/2007-12-13-singletons-in-c-sharp/</guid><description>In many ways software engineering strives to strike a balance between testability, scalability (and performance), execution logic and symmetry (in the case of distributed algorithms), and semantics (easily understood and usable) while remaining efficient in the use of system resources. In my opinion, these major variables are all of equal weighting; these define the elegance of a particular design.
For this discussion, I’d like to focus on two of the variables: testability and efficient use of system resources.</description><content:encoded><![CDATA[<p>In many ways software engineering strives to strike a balance between testability, scalability (and performance), execution logic and symmetry (in the case of distributed algorithms), and semantics (easily understood and usable) while remaining efficient in the use of system resources. In my opinion, these major variables are all of equal weighting; these define the 
<a href="/blog/2011/05/11/a-word-on-elegance/">elegance</a>
 of a particular design.</p>
<p>For this discussion, I’d like to focus on two of the variables: testability and efficient use of system resources.</p>
<h3 id="garbage-collection" class="heading">Garbage Collection
    <a class="heading__anchor" href="#garbage-collection" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>As a prerequisite, I recommend you read 
<a href="http://msdn2.microsoft.com/en-us/library/ms973837.aspx" class="link-external" rel="noopener noreferrer" target="_blank">this article
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 on how garbage collection works at a high-level in the Microsoft .NET Framework. If you 
<a href="http://en.wikipedia.org/wiki/Attention-Deficit_Hyperactivity_Disorder" class="link-external" rel="noopener noreferrer" target="_blank">don’t have time
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 to read the article then do take time to read the next paragraph.</p>
<p>The following is a good idea what sorts of things we should try to avoid to get the best performance out of the garbage collector:</p>
<ul>
<li>Too many allocations.</li>
<li>Too-large allocation.</li>
<li>Too many pointers.</li>
<li>Too many roots.</li>
<li>Too many object writes.</li>
<li>Too many Almost-Long-Life objects.</li>
<li>If you implement IDisposable then suppress finalization to reduce costs.</li>
</ul>
<h3 id="dependency-injection-di" class="heading">Dependency Injection (DI)
    <a class="heading__anchor" href="#dependency-injection-di" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>A foundational pattern in xUnit testing is 
<a href="http://en.wikipedia.org/wiki/Dependency_injection" class="link-external" rel="noopener noreferrer" target="_blank">Dependency Injection (DI)
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, which is a form of 
<a href="http://martinfowler.com/articles/injection.html" class="link-external" rel="noopener noreferrer" target="_blank">Inversion of Control (IoC)
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
. Specifically, DI is a programming technique where the implementation of one class is actually performed partially by another. Inversion of Control is where a program gives up control of its own execution and simply responds to requests made of it. In the same way, a class using dependency injection gives up control over some of its implementation and lets the injected class do the work.</p>
<p>Three types of DI exist; constructor injection, property injection, and method call injection. Constructor injection is a pattern whereby dependencies are “injected” into an object during construction. Whereas, property injection uses set operations [on properties] to inject dependencies, which obviously occurs after construction. Method call injection simply uses method calls to inject dependencies.</p>
<p>Several libraries exist in each category. For example, the 
<a href="http://www.castleproject.org/container/index.html" class="link-external" rel="noopener noreferrer" target="_blank">Castle Project
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and 
<a href="http://www.codeplex.com/ObjectBuilder" class="link-external" rel="noopener noreferrer" target="_blank">ObjectBuilder
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 both provide supports for constructor and property injection. As a side note, the 
<a href="http://www.codeplex.com/entlib/" class="link-external" rel="noopener noreferrer" target="_blank">Microsoft Enterprise Library
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 uses ObjectBuilder internally as its DI container framework. 
<a href="http://www.codeplex.com/unity" class="link-external" rel="noopener noreferrer" target="_blank">Unity
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 is Microsoft’s latest offering from the Pattern’s &amp; Practices team. Unity offers property, constructor, and method call injection.</p>
<h3 id="static-classes" class="heading">Static Classes
    <a class="heading__anchor" href="#static-classes" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>A 
<a href="http://msdn2.microsoft.com/en-us/library/79b3xss3%28VS.80%29.aspx" class="link-external" rel="noopener noreferrer" target="_blank">static class
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 does not have any instance-level members (including constructors) and is defined by the 
<a href="http://msdn2.microsoft.com/en-us/library/98f28cdx%28VS.80%29.aspx" class="link-external" rel="noopener noreferrer" target="_blank">static modifier
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
. The compiler in turn marks a static class as sealed and automatically creates a private constructor.</p>
<p>The main features of a static class are:</p>
<ul>
<li>They only contain static members.</li>
<li>They cannot be instantiated.</li>
<li>They are sealed.</li>
<li>They cannot contain 
<a href="http://msdn2.microsoft.com/en-us/library/k6sa6h87%28VS.80%29.aspx" class="link-external" rel="noopener noreferrer" target="_blank">Instance Constructors
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
.</li>
</ul>
<p>Inherently in the context of unit (and integration) testing, static classes are not recommended due to the difficulty in implementing a thread-safe property injector DI pattern and being unable to use constructor DI. However, the advantage of a properly written static class is there’s only one object instance per 
<a href="http://msdn2.microsoft.com/en-us/library/system.appdomain.aspx" class="link-external" rel="noopener noreferrer" target="_blank">AppDomain
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
, which can be very efficient on the GC (see the Too Many Object Writes section of the previously referenced 
<a href="http://msdn2.microsoft.com/en-us/library/ms973837.aspx" class="link-external" rel="noopener noreferrer" target="_blank">article
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
).</p>
<p>This is a very important and annoying impedance that exists between the use of a constructor DI pattern and efficient use (and preservation) of system resources (in this case CPU and memory). We want to be testable and mindful of system resources.</p>
<h3 id="singletons" class="heading">Singletons
    <a class="heading__anchor" href="#singletons" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>For purposes of this discussion, a singleton is a class that only allows a single instance of itself to be created within an AppDomain. The following are recommended singleton patterns.</p>
<p>Sidebar: 
<a href="http://msdn.microsoft.com/msdnmag/issues/05/10/MemoryModels/default.aspx" class="link-external" rel="noopener noreferrer" target="_blank">Understand the Impact of Low-Lock Techniques in Multithreaded Apps
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
</p>
<p><strong>Pattern 1: Niladic Constructor</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="kd">public</span> <span class="k">class</span> <span class="nc">MySingleton</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="kd">static</span> <span class="k">readonly</span> <span class="n">MySingleton</span> <span class="n">_instance</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MySingleton</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="n">MySingleton</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="n">MySingleton</span> <span class="n">Instance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_instance</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p><strong>Pattern 2: Niladic Constructor (Full Lazy Initialization)</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="kd">public</span> <span class="k">class</span> <span class="nc">MySingleton</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">class</span> <span class="nc">Nested</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="c1">//</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// Explicit static constructor to tell C# compiler not to mark</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// type as BeforeFieldInit</span>
</span></span><span class="line"><span class="cl">        <span class="c1">//</span>
</span></span><span class="line"><span class="cl">        <span class="kd">static</span> <span class="n">Nested</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">        <span class="kd">internal</span> <span class="kd">static</span> <span class="k">readonly</span> <span class="n">MySingleton</span> <span class="n">Instance</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MySingleton</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="n">MySingleton</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="n">MySingleton</span> <span class="n">Instance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Nested</span><span class="p">.</span><span class="n">Instance</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Note, the following alternate method results in identical MSIL…</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="kd">public</span> <span class="k">class</span> <span class="nc">MySingleton</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">static</span> <span class="k">class</span> <span class="nc">Nested</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kd">internal</span> <span class="kd">static</span> <span class="k">readonly</span> <span class="n">MySingleton</span> <span class="n">Instance</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MySingleton</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="n">MySingleton</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="n">MySingleton</span> <span class="n">Instance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Nested</span><span class="p">.</span><span class="n">Instance</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>A rather dated but still applicable discussion regarding the use of the double-check lock pattern written by 
<a href="http://blogs.msdn.com/vancem/" class="link-external" rel="noopener noreferrer" target="_blank">Vance Morrison
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 can be found 
<a href="http://discuss.develop.com/archives/wa.exe?A2=ind0203B&amp;L=DOTNET&amp;P=R375" class="link-external" rel="noopener noreferrer" target="_blank">here
<svg class="icon icon--external" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M14 4h6v6M20 4l-8.5 8.5"/>
    <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>
</svg>
<span class="visually-hidden"> (opens in a new window)</span>
</a>
 and provides insight into subtle nuances of the Microsoft .NET Framework memory model implementation. I’ve attached a PDF version of the discussion to this entry (at the bottom) in case the archive disappears.</p>
<p>Singleton<T></p>
<p>So back to the question at hand, which is how do I ensure my production code is testable and uses system resources efficiently as possible? I offer the Singleton<T> class, which is defined as…</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">sealed</span> <span class="k">class</span> <span class="nc">Singleton</span> <span class="k">where</span> <span class="n">T</span> <span class="p">:</span> <span class="k">new</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="kd">static</span> <span class="k">readonly</span> <span class="n">T</span> <span class="n">_instance</span> <span class="p">=</span> <span class="k">new</span> <span class="n">T</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="n">Singleton</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="n">T</span> <span class="n">Instance</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_instance</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>With the Singleton<T> class, we’re now able to use the following syntax in production code…</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="kd">public</span> <span class="k">class</span> <span class="nc">CustomerManager</span> <span class="p">:</span> <span class="n">BusinessLogicComponent</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="n">ICustomerRepository</span> <span class="n">_repository</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// This constructor is required by the Singleton generic class.</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl"><span class="na">    [EditorBrowsable(EditorBrowsableState.Never)]</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="n">CustomerManager</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="p">:</span> <span class="k">this</span><span class="p">(</span><span class="n">Singleton</span><span class="p">.</span><span class="n">Instance</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//  This constructor is to be used only by test projects.</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl"><span class="na">    [EditorBrowsable(EditorBrowsableState.Never)]</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="n">CustomerProcessor</span><span class="p">(</span><span class="n">ICustomerRepository</span> <span class="n">repository</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">_repository</span> <span class="p">=</span> <span class="n">repository</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="k">void</span> <span class="n">Add</span><span class="p">(</span><span class="n">Customer</span> <span class="n">customer</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">_repository</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">customer</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>…and the following syntax from test projects.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-csharp" data-lang="csharp"><span class="line"><span class="cl"><span class="na">[Test]</span>
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="k">void</span> <span class="n">Customer_Add_Succeeds</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// Create a mock customer repository and inject it into the business</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// logic component (manager).</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl">    <span class="n">ICustomerRepository</span> <span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MockCustomerRepository</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="n">CustomerManager</span> <span class="n">manager</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomerManager</span><span class="p">(</span><span class="n">repository</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// Perform remaining test operations.</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Consumers of the CustomerManager business logic class in production simply use the Singleton<T> class just as the CustomerManager does with the CustomerRepository class in its niladic constructor.</p>
<p>A final note on thread-safety. The singleton pattern does require all instance level methods to be thread-safe.</p>
<h3 id="conclusion" class="heading">Conclusion
    <a class="heading__anchor" href="#conclusion" tabindex="-1" aria-hidden="true"
        title="Link to this section">
<svg class="icon icon--hash" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
    stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
    <path d="M5 9h14M5 15h14M10 3.5 8 20.5M16 3.5l-2 17"/>
</svg>
</a>
</h3>
<p>The Singleton<T> class provides an implementation pattern to balance between two important variables in any design; that of testability and efficient use of system resources. I encourage you to experiment.</p>
<p>
<a href="/files/Garbage-Collector-Basics-and-Performance-Hints.zip">Garbage Collector Basics and Performance Hints.zip</a>
</p>]]></content:encoded><category>computing</category><category>software engineering</category></item></channel></rss>