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

  <title><![CDATA[Konstantin Anoshkin]]></title>
  <link href="http://anoshkin.net/atom.xml" rel="self"/>
  <link href="http://anoshkin.net/"/>
  <updated>2012-04-14T19:04:51+04:00</updated>
  <id>http://anoshkin.net/</id>
  <author>
    <name><![CDATA[Konstantin Anoshkin]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Objective-C Properties and Coding Style]]></title>
    <link href="http://anoshkin.net/blog/2012-04-14/objective-c-properties-and-coding-style/"/>
    <updated>2012-04-14T19:00:00+04:00</updated>
    <id>http://anoshkin.net/blog/2012-04-14/objective-c-properties-and-coding-style</id>
    <content type="html"><![CDATA[<p>Objective-C 2.0 introduced a nice thing called <em>properties</em>. In this post, intended for beginning Objective-C developers, I will try to explain what properties are and how not to misuse the syntax.</p>

<!--more-->


<h3>Objective-C 2.0 Properties</h3>

<p>Objective-C properties help readability a lot, making interface declarations more self-descriptive. They also help you make your code a little cleaner by replacing some of the square brackets with the dot notation <em>where appropriate</em>. All in all, Objective-C 2.0 properties encourage a better coding style, but their use requires a little understanding and discipline.</p>

<p>Consider the following class interface and particularly the property declaration:</p>

<figure class='code'><figcaption><span>Property declaration</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">@interface</span> <span class="nc">Container</span> : <span class="nc">NSObject</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">@public</span>
</span><span class='line'>    <span class="kt">float</span> <span class="n">_weight</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">assign</span><span class="p">)</span> <span class="kt">float</span> <span class="n">weight</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This declaration says that you can both get and set the weight of a container object (indicated by <code>readwrite</code> attribute or by the absence of <code>readonly</code> attribute), that the value type is <code>float</code> and that it is not safe to use the property as is in multithreaded scenarios (<code>nonatomic</code> means there are no locks involved). That&#8217;s a whole lot of useful information in a single line.</p>

<h3>Dot Notation</h3>

<p>When you need to access the weight property, you write <code>myBag.weight = 2000.0f</code>. Nothing could be simpler, right? Not quite. An attentive mind will notice the dot between the object and the property name and <a href="http://en.wikipedia.org/wiki/Objective-C">say</a>,</p>

<blockquote><p>Objective-C is a thin layer on top of C, and moreover is a strict superset of C&#8230;</p></blockquote>

<p>Yep.</p>

<blockquote><p>In C we use a period to access <a href="http://en.wikipedia.org/wiki/C_syntax#Accessing_members">a member of a struct</a>, like <code>stat.st_size</code>.</p></blockquote>

<p>Yep.</p>

<blockquote><p>Objective-C objects are in fact C pointers and <code>id</code> is declared as a pointer to a C struct.</p></blockquote>

<p>Yep:</p>

<figure class='code'><figcaption><span>objc/objc.h</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">typedef</span> <span class="k">struct</span> <span class="n">objc_object</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">Class</span> <span class="n">isa</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span> <span class="o">*</span><span class="kt">id</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<blockquote><p>What the heck, then, does <code>myBag.weight</code> mean? Is <code>myBag</code> a struct, a pointer or what?</p></blockquote>

<p>Okay. Glad you noticed that. Now – if you want to retain your sanity – forget about what <code>id</code> is. It&#8217;s a generic Objective-C object, its size is equal to the size of a pointer (well, the grass is green, the sky is blue, a pointer has the size of a pointer) and that&#8217;s all you need to know. You are not supposed to know what <code>id</code> actually is, got that? Now, the dot in Objective-C, being the same symbol as a period in C, is a special syntax to access object properties. Don&#8217;t confuse it with accessing a member of a struct, because <code>id</code> is not a struct. Don&#8217;t confuse it with accessing an object&#8217;s instance variables either, because a property in general has no correlation with any instance variables. A property is just that, <em>a property</em>.</p>

<p>One day, when you become proficient in Objective-C, you will write something like this:</p>

<figure class='code'><figcaption><span>Properties vs Ivars</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">Container</span> <span class="o">*</span><span class="n">aBag</span> <span class="o">=</span> <span class="p">[</span><span class="n">Container</span> <span class="n">new</span><span class="p">];</span>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="n">aBag</span><span class="p">.</span><span class="n">weight</span> <span class="o">!=</span> <span class="n">aBag</span><span class="o">-&gt;</span><span class="n">_weight</span><span class="p">)</span>
</span><span class='line'>    <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;*** Accessor method returned a different weight&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that <code>aBag.weight</code> is the value of the property and <code>aBag-&gt;_weight</code> directly refers to the instance variable called <code>_weight</code>. Don&#8217;t do it unless this is your class and you know what you&#8217;re doing and there&#8217;s no other way to do it. Bad me, I&#8217;m teaching you horrible things. Sorry. I just want to point out that a property is not the same thing as an instance variable, even if they happen to have the same name. More on that a bit later.</p>

<h3>Accessor Methods</h3>

<p>More often than not you probably just <code>@synthesize</code> the property and forget about it. But wait, <em>you don&#8217;t use something unless you know what it does</em>, right? So what does <code>@synthesize</code> do? According to the official documentation,</p>

<blockquote><p>You use the <code>@synthesize</code> directive to tell the compiler that it should synthesize the setter and/or getter methods for a property if you do not supply them within the <code>@implementation</code> block.</p></blockquote>

<p>In other words, <code>@synthesize</code> tells the <em>compiler</em> to generate Objective-C methods which more or less look like this:</p>

<figure class='code'><figcaption><span>Accessor Methods</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="o">-</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">weight</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">_weight</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nl">setWeight:</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">newValue</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="n">_weight</span> <span class="o">=</span> <span class="n">newValue</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Never mind that I&#8217;m simplifying things a bit here, because the actual code inside the curly brackets isn&#8217;t relevant to the discussion. I want to stress the fact that the compiler actually inserts standard Objective-C accessor methods, <code>-weight</code> and <code>-setWeight:</code>; that is, the methods exist in the compiled binary and to the outside world they look just like any other Objective-C methods. If you had to implement this property yourself, you would write these accessor methods too.</p>

<p>Remember when I said that a property is not the same thing as an instance variable? Here&#8217;s an example:</p>

<figure class='code'><figcaption><span>Getter Accessor Method with Unit Conversion</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="o">-</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">weight</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">_weight</span> <span class="o">*</span> <span class="mf">28.35f</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nl">setWeight:</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">newValue</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="n">_weight</span> <span class="o">=</span> <span class="n">newValue</span> <span class="o">/</span> <span class="mf">28.35f</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Aha! So the instance variable keeps the weight in ounces (this is called an <em>implementation detail</em>) and the getter accessor method returns the weight in grams. When you use the <code>weight</code> property, you get the weight in grams too. Note that without access to the source code you would not even suspect that the class uses other measurement units internally, because it takes and returns grams. This is called <em>encapsulation</em> in object-oriented programming: you only see what you need to see, poking around in other people&#8217;s objects is frown upon and may have consequences.</p>

<p>What does it matter to you? Whether you write <code>myBag.weight</code> or <code>[myBag weight]</code> – or <code>myBag.weight = 2000.0f</code> or <code>[myBag setWeight: 2000.0f]</code> – the code that is going to be executed is the same. The difference is entirely syntactic, which brings us to the question of style and importance of syntax.</p>

<h3>The Value of Style</h3>

<p>Coding style is not about writing code that works, it&#8217;s about writing <em>readable</em> code. Remember, you don&#8217;t write code for a compiler, you write it for humans, including yourself. Perhaps, in half a year – when you can&#8217;t remember every subtle detail – you will have to fix a bug in your own program. You will read your own code, asking yourself, what the heck the geek who wrote it was thinking. It&#8217;s better to spend time writing readable code now than to waste time deciphering a mess later.</p>

<p>Please, don&#8217;t write code <a href="http://www.ioccc.org/2000/natori.c">like this</a> unless you&#8217;re trying to win <a href="http://www.ioccc.org/">The International Obfuscated C Code Contest</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="kt">double</span> <span class="n">l</span><span class="p">;</span><span class="n">main</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="n">o</span><span class="p">,</span><span class="n">O</span><span class="p">){</span><span class="k">return</span> <span class="n">putchar</span><span class="p">((</span><span class="n">_</span><span class="o">--+</span><span class="mi">22</span><span class="o">&amp;&amp;</span><span class="n">_</span><span class="o">+</span><span class="mi">44</span><span class="o">&amp;&amp;</span><span class="n">main</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="o">-</span><span class="mi">43</span><span class="p">,</span><span class="n">_</span><span class="p">),</span><span class="n">_</span><span class="o">&amp;&amp;</span><span class="n">o</span><span class="p">)</span><span class="o">?</span><span class="p">(</span><span class="n">main</span><span class="p">(</span><span class="o">-</span><span class="mi">43</span><span class="p">,</span><span class="o">++</span><span class="n">o</span><span class="p">,</span><span class="n">O</span><span class="p">),((</span><span class="n">l</span><span class="o">=</span><span class="p">(</span><span class="n">o</span><span class="o">+</span><span class="mi">21</span><span class="p">)</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="o">-</span><span class="n">O</span><span class="o">*</span><span class="mi">22</span><span class="o">-</span><span class="n">O</span><span class="o">*</span><span class="n">O</span><span class="p">),</span><span class="n">l</span><span class="o">*</span><span class="n">l</span><span class="o">&lt;</span><span class="mi">4</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">fabs</span><span class="p">(((</span><span class="n">time</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">-</span><span class="mi">607728</span><span class="p">)</span><span class="o">%</span><span class="mi">2551443</span><span class="p">)</span><span class="o">/</span><span class="mf">405859.</span><span class="o">-</span><span class="mf">4.7</span><span class="o">+</span><span class="n">acos</span><span class="p">(</span><span class="n">l</span><span class="o">/</span><span class="mi">2</span><span class="p">))</span><span class="o">&lt;</span><span class="mf">1.57</span><span class="p">))[</span><span class="s">&quot; #&quot;</span><span class="p">]))</span><span class="o">:</span><span class="mi">10</span><span class="p">);}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Dot Notation Misuse</h3>

<p>From the compiler&#8217;s point of view, it&#8217;s perfectly valid to use the dot notation and the traditional Objective-C message syntax interchangeably:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">NSArray</span> <span class="o">*</span><span class="n">myArray</span> <span class="o">=</span> <span class="p">...;</span>
</span><span class='line'><span class="k">if</span> <span class="p">([</span><span class="n">myArray</span> <span class="n">count</span><span class="p">]</span> <span class="o">!=</span> <span class="n">myArray</span><span class="p">.</span><span class="n">count</span><span class="p">)</span>
</span><span class='line'>    <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;The compiler has gone crazy&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Luckily for us, a human brain is orders of magnitude more intelligent than any compiler humans have created so far. Here&#8217;s the problem: when a trained eye of an experienced Objective-C developer sees a dot following an object, it sends a signal to other brain convolutions that the dot is followed by a <em>property</em>. Then it goes like this, &#8220;Wait, I recognize the thing; <code>count</code> is not actually a property, it&#8217;s just an accessor method. The syntax is OK, though. Ignored. Proceed.&#8221;</p>

<p>I contend that, having saved exactly two key strokes <em>once</em>, you make a reader spend more mental effort <em>every time</em> somebody reads the code. It&#8217;s not that bad in cases when the method name is well-known, but if a reader doesn&#8217;t recognize the method immediately, it may cause confusion.</p>

<p>Let&#8217;s now consider more egregious examples.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="kt">double</span> <span class="n">currentThreadPriority</span> <span class="o">=</span> <span class="n">NSThread</span><span class="p">.</span><span class="n">threadPriority</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>You know what? Class objects don&#8217;t have properties, period. This will stall the parsing pipeline in any brain. Don&#8217;t do it.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">NSFileHandle</span> <span class="o">*</span><span class="n">fileHandle</span> <span class="o">=</span> <span class="p">...;</span>
</span><span class='line'><span class="n">NSData</span> <span class="o">*</span><span class="n">data</span> <span class="o">=</span> <span class="n">fileHandle</span><span class="p">.</span><span class="n">readDataToEndOfFile</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>How can a verb be a property? WTF? It&#8217;s an Objective-C <em>method</em>, not a C++ member function or other irrelevant shit like that. Have some respect for the language!</p>

<p>I cringe at the syntax misuse. It compiles and works, sure, but it looks weird, as if written by aliens. It demonstrates that you either don&#8217;t understand the language or don&#8217;t care about your work. If you do care, take some time to read &#8220;The Objective-C Programming Language&#8221; and &#8220;Concepts in Objective-C Programming&#8221;, the guides available right under the Documentation tab in the Xcode Organizer (⌘⇧2, Cmd+Shift+2). When done, welcome to <a href="http://stackoverflow.com/">Stack Overflow</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hello Octopress]]></title>
    <link href="http://anoshkin.net/blog/2011-10-27/hello-octopress/"/>
    <updated>2011-10-27T12:30:00+04:00</updated>
    <id>http://anoshkin.net/blog/2011-10-27/hello-octopress</id>
    <content type="html"><![CDATA[<p>I am restarting the blog as a testimony to the axiom that the expectable happens most unexpectedly. To be precise, I fell victim to technical problems with my hosting provider. I could see it coming, but that&#8217;s cold comfort. Nonetheless, an optimist, tucked away deep inside me, believes this is a chance to get rid of loud-mouthed opinionated stuff and to start clean.</p>

<!--more-->


<p>I&#8217;ve been a customer of <a href="http://www.supremehost.net/" title="SupremeHost">SupremeHost</a> for several years. I can&#8217;t say I was ever particularly happy with the provider, but it did the job for a ridiculously low price. There were occasional what-the-fuck moments when in the heat of the battle against spam they blocked a couple continents with firewall rules, but those used to be promptly sorted out by their support. Like, say, <em>use port 26 for SMTP</em> (I&#8217;m not kidding). In general, though, it was an OK experience.</p>

<p>However, as is usually the case with everything suspiciously cheap, the service was going downhill. At first sending messages via an online support form stopped producing any replies. Then I had to switch to SMTP through an SSH tunnel. Then SSH stopped working because &#8220;PTY allocation request failed on channel 0&#8221; and I got effectively locked out of my server.</p>

<p>Enter <a href="http://www.linode.com/?r=9ad239d5ac189f745a60b9279581bd1c9a5a218a" title="Linode Xen VPS hosting">Linode</a> (referral link, if you don&#8217;t mind). I confess I yielded to <a href="http://mattgemmell.com/2009/11/15/linode/" title="Matt Gemmell on Linode">Matt Gemmell</a>&#8217;s opinion, and I don&#8217;t regret it. <a href="http://en.wikipedia.org/wiki/Virtual_private_server" title="Virtual private server">VPS</a> is a damn cool thing and if you need more than a dumbed-down admin panel, you&#8217;re going to love the freedom. Linode&#8217;s quad core Linux nodes (hence the name) start at $20 for impressive 512 MB RAM. I was sold.</p>

<p>So I moved from a traditional <a href="http://en.wikipedia.org/wiki/LAMP_(software_bundle)" title="Linux + Apache + MySQL + PHP">LAMP</a>/<a href="http://wordpress.org/" title="WordPress">WordPress</a> setup to <a href="http://www.ubuntu.com/" title="Ubuntu Linux">Ubuntu</a> + <a href="http://wiki.nginx.org/Main" title="Nginx HTTP server">Nginx</a> + <a href="http://octopress.org/" title="Octopress blogging framework">Octopress</a> with a bit of <a href="http://www.php.net/" title="PHP">PHP</a>/<a href="http://www.sqlite.org/" title="SQLite">SQLite</a> for several stupid scripts I still need. I decided to use the free flavor of <a href="http://www.google.com/apps/intl/en/group/" title="Free Google Apps">Google Apps</a> for email, because I&#8217;m far from being competent enough to run my own mail server. The migration took several days, mainly because I&#8217;m spoiled by Macs and, alas, too few things on Linux &#8220;just work&#8221;. The blog content had to be sacrificed, but I think it was either not interesting enough or just outdated. Everything else should work more or less reliably.</p>
]]></content>
  </entry>
  
</feed>

