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

  <title><![CDATA[@vrybas.(blogs).here]]></title>
  <link href="http://vrybas.github.io/atom.xml" rel="self"/>
  <link href="http://vrybas.github.io/"/>
  <updated>2019-09-09T20:33:23+03:00</updated>
  <id>http://vrybas.github.io/</id>
  <author>
    <name><![CDATA[Vladimir Rybas]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[A Way to Organize POROs in Rails]]></title>
    <link href="http://vrybas.github.io/blog/2014/08/15/a-way-to-organize-poros-in-rails/"/>
    <updated>2014-08-15T17:26:54+04:00</updated>
    <id>http://vrybas.github.io/blog/2014/08/15/a-way-to-organize-poros-in-rails</id>
    <content type="html"><![CDATA[<h2>Service layer</h2>

<p>So, now we came to a point where our app no longer fit to vanilla MVC
and we want to extract a service layer. By &ldquo;Service Layer&rdquo; I mean a
collection of Plain Old Ruby Objects, which hold pure business logic.
What kind of POROs it might be? Here&rsquo;s good <a href="http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/">examples</a> by Bryan
Helmkamp.</p>

<p>But, anyway, the question is &ndash; where do we put them in our Rails app
files structure?</p>

<p>We got several options here:</p>

<ol>
<li>Dump everything to <code>lib/</code> dir</li>
<li>Call them non-ActiveRecord Models and put them in <code>app/models/</code></li>
<li>Use Cases and Entities (DDD approach). So <code>app/use_cases/</code> &amp;
<code>app/entities/</code></li>
<li>???</li>
</ol>


<p>Let&rsquo;s go through these options one after another.</p>

<h3>1. <code>lib/</code></h3>

<p>If class/module is not Model, View or Controller, it suppose to go to
<code>lib/</code>, isn&rsquo;t it? Well, this approach got some disadvantages.</p>

<p>When you start to extract your business logic to POROs, on a big project
you&rsquo;re going to have a few dozen of them, maybe hundred(s). Pretty
soon you open your <code>lib/</code> &ndash; and it scares you. Even worse when it gets
explored by a new team member.</p>

<p>Another thing &ndash; it&rsquo;s pretty easy to loose track on what is used where
and for what reason. Especially in dynamic metaprogramming-friendly
language like Ruby.</p>

<p>So, slowly but surely, <code>lib/</code> becomes &ldquo;a big ball of mud&rdquo;.</p>

<h2>2. non-ActiveRecord Models</h2>

<p>We can say that PORO, which returns data structure(or for whatever other
reason), is non-ActiveRecord model and put it in <code>models/</code></p>

<p>Well, that&rsquo;s better. Sort of. Part is in <code>lib/</code>, part is in <code>models/</code>. A
slightly less mess, but in two places now.</p>

<p>Besides, there&rsquo;s a problem each time to decide, whether this PORO is a
&ldquo;model&rdquo; or not. And it&rsquo;s even harder for others to guess, where to find
that class(in <code>models/</code> or still in <code>lib/</code>), when they explore your
code.</p>

<h2>3. Use Cases &amp; Entities</h2>

<p>The concept of Use Cases &amp; Entities comes from DDD and from Bob Martin&rsquo;s
<a href="http://vrybas.github.io/blog/2014/04/04/rails-and-pipes/">talk</a> &ldquo;Architecture: The Lost Years&rdquo;.  So, the core of your system
suppose be in UseCases(verbs e.g. <em>CreateUser</em>) and Entities(nouns e.g.
<em>User</em>).</p>

<p>But the problem with this &ndash; it takes you far away from &ldquo;The Rails Way&rdquo;.
In one case I want to use my ActiveRecord model as Entitly, but in the
other case it should be a separate class with it&rsquo;s corresponding
Repository class. Sometimes, my Controller is pretty much the UseCase,
but sometimes I want to extract a few classes from it.</p>

<p>Decisions, decisions. It&rsquo;s all confusing. Besides, everyone in the team
need to share the exact same architectural concepts (which is solved by
&ldquo;The Rails Way&rdquo; at first place). Pretty quick the codebase can become
more mess and hard to follow through, than it was before.</p>

<h2>4. Namespaces FTW!</h2>

<p>So, there&rsquo;s another approach, which is used by every single gem and
almost any other Ruby program &ndash; the namespaced classes and modules.</p>

<h6>Disclaimer: As usual, code examples are highly synthetic and short to keep them easy to follow. Your &ldquo;results may vary&rdquo;.</h6>

<h3>Example with Controllers</h3>

<p>Let&rsquo;s say you got a dashboard in your app.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">DashboardController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">index</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>And you want to show a list of users on this dashboard. But user list
should be paginated, sortable, and <code>current_user</code>-specific. Now, I think
it&rsquo;s a good idea to create a <code>UserList</code> class, which will handle all
this logic.</p>

<p>But where do I put this class? Is this a Service / Model / Use Case /
Entity / Query Object / Policy Object? When using namespaces, the answer
is &ndash; I don&rsquo;t really care.</p>

<p>All I need to do is to create one more folder in <code>controllers/</code> with the
same name as controller.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">controllers</span>
</span><span class='line'><span class="err">├──</span> <span class="n">dashboard_controller</span>
</span><span class='line'><span class="err">│</span>   <span class="err">└──</span> <span class="n">user_list</span><span class="o">.</span><span class="n">rb</span>
</span><span class='line'><span class="err">└──</span> <span class="n">dashboard_controller</span><span class="o">.</span><span class="n">rb</span>
</span></code></pre></td></tr></table></div></figure>


<p>and put my <code>UserList</code> class there</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># app/controllers/dashboard_controller/user_list.rb</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">DashboardController</span>
</span><span class='line'>  <span class="k">class</span> <span class="nc">UserList</span>
</span><span class='line'>    <span class="c1"># codes codes codes</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>and then it can be used in <code>DashboardController</code> like this</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">DashboardController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">index</span>
</span><span class='line'>    <span class="vi">@users</span> <span class="o">=</span> <span class="no">UserList</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">current_user</span><span class="p">,</span>
</span><span class='line'>                          <span class="ss">page</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:page</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>                          <span class="n">per_page</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:per_page</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>                          <span class="n">sort_by</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:sort_by</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>                          <span class="n">order_asc</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:order_asc</span><span class="o">]</span><span class="p">)</span><span class="o">.</span><span class="n">all</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that we don&rsquo;t even need to specify the namespace.</p>

<h3>Example with Models</h3>

<p>Pretty much the same thing with models.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">can_follow?</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
</span><span class='line'>    <span class="no">FollowingPolicy</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="n">user</span><span class="p">)</span><span class="o">.</span><span class="n">can_follow?</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>with files structure</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">models</span>
</span><span class='line'><span class="err">├──</span> <span class="n">user</span>
</span><span class='line'><span class="err">│</span>   <span class="err">└──</span> <span class="n">following_policy</span><span class="o">.</span><span class="n">rb</span>
</span><span class='line'><span class="err">└──</span> <span class="n">user</span><span class="o">.</span><span class="n">rb</span>
</span></code></pre></td></tr></table></div></figure>


<p>and possible content</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># app/models/user/following_policy.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">User</span>
</span><span class='line'>  <span class="k">class</span> <span class="nc">FollowingPolicy</span>
</span><span class='line'>    <span class="kp">attr_reader</span> <span class="ss">:current_user</span><span class="p">,</span> <span class="ss">:other_user</span><span class="p">,</span> <span class="ss">:account_verification</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">current_user</span><span class="p">,</span> <span class="n">other_user</span><span class="p">)</span>
</span><span class='line'>      <span class="vi">@current_user</span> <span class="o">=</span> <span class="n">current_user</span>
</span><span class='line'>      <span class="vi">@other_user</span> <span class="o">=</span> <span class="n">other_user</span>
</span><span class='line'>      <span class="vi">@account_verification</span> <span class="o">=</span> <span class="n">current_user</span><span class="o">.</span><span class="n">account_verification</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1"># ...</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>As a result &ndash; your code is more logically organized. It would be
pretty easy to guess the locations of classes while exploring code &ndash;
your app will still look like a Rails app.</p>

<p>And you&rsquo;ll never afraid to extract one more single responsibility class,
knowing that it is going to be small and &ldquo;local&rdquo; and won&rsquo;t conflict with
other&rsquo;s classes.</p>

<p>What goes to <code>lib/</code> then? API wrappers for one example. The
application-independent code, or code which doesn&rsquo;t belong to any model
or controller.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[TDD Resources]]></title>
    <link href="http://vrybas.github.io/blog/2014/04/30/tdd-resources/"/>
    <updated>2014-04-30T20:34:04+04:00</updated>
    <id>http://vrybas.github.io/blog/2014/04/30/tdd-resources</id>
    <content type="html"><![CDATA[<h2>Design:</h2>

<h3>J.B. Rainsberger &ndash; <a href="http://vimeo.com/80533536">Integrated Tests Are A Scam</a></h3>

<blockquote><ul>
<li>They don&rsquo;t put pressure to the design</li>
<li>You can&rsquo;t possibly test all cases</li>
<li>Introduce &ldquo;Collaboration&rdquo; tests</li>
<li>Introduce &ldquo;Contract&rdquo; tests</li>
</ul>
</blockquote>

<h3>Chris Parsons &ndash; <a href="http://www.confreaks.com/videos/697-rubyconf2011-your-tests-are-lying-to-you">Your Tests Are Lying To You</a></h3>

<blockquote><ul>
<li>Some tests are saying they&rsquo;re Unit, but they&rsquo;re actually Integration</li>
<li>Good design is easy to test &ndash; Corey Haines</li>
<li>Mockist vs Classist</li>
<li>Mocks are isolated, fast &amp; tell you about design flaws</li>
</ul>
</blockquote>

<h3>Sandi Metz &ndash; <a href="http://www.youtube.com/watch?v=qT5iriwidRg">The Design of Tests</a></h3>

<blockquote><ul>
<li>Messages to self &ndash; rarely tested</li>
<li>Incoming messages &ndash; assert on result</li>
<li>Outgoing messages &ndash; test for calling</li>
<li>Outgoing queries &ndash; don&rsquo;t test!</li>
<li>Outgoing commands &ndash; expectations on mocks</li>
</ul>
</blockquote>

<h2>Mocks:</h2>

<h3>Martin Fowler &ndash; <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks Aren&rsquo;t Stubs</a></h3>

<blockquote><ul>
<li>Mockist &amp; behavior design VS Classist &amp; STATE</li>
</ul>
</blockquote>

<h3>Corey Haines &ndash; <a href="http://www.confreaks.com/videos/1237-aloharuby2012-yay-mocks">Yay! Mocks!</a></h3>

<blockquote><ul>
<li>Mocks lead to better design that easy to change</li>
<li>OO design is about interactions &amp; roles</li>
<li>Delegate the &ldquo;How&rdquo;</li>
<li>Don&rsquo;t let mocks depend on other mocks</li>
</ul>
</blockquote>

<h3>Gregory Moeck &ndash; <a href="http://www.confreaks.com/videos/659-rubyconf2011-why-you-don-t-get-mock-objects">Why You Don&rsquo;t Get Mock Objects</a></h3>

<blockquote><ul>
<li>Mocks assert on messages</li>
<li>Stub returns value</li>
<li>Mock Roles, not concrete objects.</li>
</ul>
</blockquote>

<h3>Gary Bernhardt &ndash; <a href="https://www.destroyallsoftware.com/blog/2014/test-isolation-is-about-avoiding-mocks">Test Isolation Is About Avoiding Mocks</a></h3>

<blockquote><ul>
<li>Nested stubs are telling something about the design</li>
<li>Good TDDers don&rsquo;t write nested mocks</li>
<li>We write huge functions because there&rsquo;s no pressure of testing them
isolated.</li>
<li>Isolated tests are microscope of object interactions</li>
<li>Don&rsquo;t let your tests make code less readable</li>
<li>Don&rsquo;t isolate &ldquo;Collaborator&rdquo; classes</li>
</ul>
</blockquote>

<h3>Piotr Solnica &ndash; <a href="http://solnic.eu/2014/05/22/mocking-and-ruby.html">Mocking and Ruby</a></h3>

<blockquote><ul>
<li>Mock only what you own
  If you don&rsquo;t own it, you don&rsquo;t have control on it&rsquo;s evolution</li>
<li>Don&rsquo;t mock just for speed</li>
<li>Be aware that mocks introduce maintenance costs</li>
<li>Keep mocks simple</li>
<li>Mock rarely</li>
</ul>
</blockquote>

<h2>Is TDD Dead?</h2>

<h3>series: <a href="http://martinfowler.com/articles/is-tdd-dead/">Part 1 &ndash; 5</a></h3>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[GRASP for Ruby(on Rails)]]></title>
    <link href="http://vrybas.github.io/blog/2014/04/12/grasp-for-ruby-on-rails/"/>
    <updated>2014-04-12T11:51:59+04:00</updated>
    <id>http://vrybas.github.io/blog/2014/04/12/grasp-for-ruby-on-rails</id>
    <content type="html"><![CDATA[<h2>What is GRASP?</h2>

<p><strong>GRASP(<a href="http://en.wikipedia.org/wiki/GRASP_(object-oriented_design)">1</a>)</strong> stands for General Responsibility Assignment Software
Patterns and Principles.</p>

<h2>Why it&rsquo;s important?</h2>

<p>Responsibilities Assignment is one of the most important skills in OOP.</p>

<p>The two major problems of relatively large codebases &ndash; code is messy and
code is coupled. Usually it&rsquo;s messy because it&rsquo;s coupled. And often
times the cause is wrong responsibilities assignment.</p>

<p>So, GRASP helps to effectively distribute responsibilities between
objects.</p>

<p>These patterns and principles are:</p>

<ul>
<li>Controller</li>
<li>Creator</li>
<li>High Cohension</li>
<li>Indirection</li>
<li>Information Expert</li>
<li>Low Coupling</li>
<li>Polymorphism</li>
<li>Protected Variations</li>
<li>Pure Fabricaton</li>
</ul>


<h2>Controller</h2>

<p><em>Controller is an object within the system which handles incoming
parameters and delegate work to others.</em></p>

<p>That&rsquo;s what &ldquo;C&rdquo; in &ldquo;MVC&rdquo; for. In Rails we have our Controllers, which
handle incoming requests. But, according to GRASP, they should meet a
few requirements:</p>

<ul>
<li>The controller should delegate the work that needs to be done to other
objects</li>
<li>It only coordinates or controls the activity</li>
<li>It should not do much work itself</li>
</ul>


<p>So, in other words, the controller should be skinny. Maybe so skinny it
can only process incoming parameters and render results. All other work
should be delegated if possible.</p>

<h2>Creator</h2>

<p><em>Creator is an object, which creates other objects.</em></p>

<p>If the one class instance have enough data to create other class
instance (or form other class instance) &ndash; it should proceed with
creation.</p>

<p>That&rsquo;s how you extract responsibilities. By gathering together a related
data chunks with methods, required for its processing in a separate
class, which instance can be used in current context.</p>

<p><code>$RAILS_ROOT/lib</code> is your friend at this time. But if this folder grows
tremendously, it&rsquo;s a clear sign you need a Service Layer(<a href="http://vrybas.github.io/blog/2014/04/04/rails-and-pipes/">2</a>).</p>

<h2>High Cohension</h2>

<p><em>The responsibilities of a given element should be strongly related and
highly focused.</em></p>

<p>But Rails models usually look like a bunch of unrelated methods, just
happening to use the data from one database table. So, &ldquo;Fat Models&rdquo;
strongly violate this principle. Good news there&rsquo;s plenty of
ways(<a href="http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/">3</a>,<a href="http://signalvnoise.com/posts/3372-put-chubby-models-on-a-diet-with-concerns">4</a>) to decompose them.</p>

<p>Speaking of modules and &ldquo;Concerns&rdquo;, it is kind of cheating(<a href="https://gist.github.com/ryanb/4172391">5</a>),
because you&rsquo;re not taking responsibility away, you&rsquo;re hiding it. But it
still useful in some cases, and might be a first step on gathering and
extracting related pieces of functionality.</p>

<h2>Indirection</h2>

<p><em>The Indirection pattern supports low coupling (and reuse potential)
between two elements by assigning the responsibility of mediation
between them to an intermediate object.</em></p>

<p>MVC shows a good example of Indirection. Views don&rsquo;t know anything about
the Models(or shouldn&rsquo;t anyway), because there&rsquo;s a Controller between
them.</p>

<p>But in a complex system that&rsquo;s not enough. Ar Controller should not know
about the Models either, talking to them through Contexts, or UseCases,
or Interactors. And Controller action clearly shouldn&rsquo;t be using several
models at once.</p>

<h2>Information Expert</h2>

<p><em>Responsibility should be placed in a class with the most information
required to fulfill it&rsquo;s data.</em></p>

<p>While this is a good general approach, it better not to be in conflict
with <strong>High Cohension</strong> and not to produce God-classes(aka Fat Models).</p>

<h2>Low Coupling</h2>

<p>Principles of Low Coupling:</p>

<ul>
<li><em>Lower dependencies between classes</em></li>
<li><em>Changes in one class should not impact other classes</em></li>
<li><em>Higher Reuse Potential</em></li>
</ul>


<p>In Rails coupling often comes with chain(>2) of before_filters,
ActiveRecord callbacks and stuff like <code>accept_nested_attributes_for</code>.</p>

<p>To reduce a number of these harmful entities, you can use Creator class
and it&rsquo;s variations. Need to synchronize a creation of two or more
models during single HTTP request? Use an Interactor or UseCase in your
controller and make the process as clear as possible. Ideally, your
models should not know about each other more than they are
related via <code>has_many/belongs_to</code> associations.</p>

<h2>Polymorphism &amp; Protected Variations</h2>

<p><em>According to Polymorphism, responsibility of defining the variation of
behaviors based on type is assigned to the types for which this
variation happens.</em></p>

<p><em>The Protected Variations pattern protects elements from the variations
on other elements (objects, systems, subsystems) by wrapping the focus
of instability with an interface and using polymorphism to create
various implementations of this interface.</em></p>

<p>Sandi Metz excellent quote(<a href="http://www.sandimetz.com/blog/2009/06/12/ruby-case-statements-and-kind-of/">6</a>):</p>

<blockquote><p>No object in your system should have to know the class of any other
object in order to know how to behave.  Everything is a Duck.  Tell
the Duck WHAT and the Duck should know HOW.</p></blockquote>

<h2>Pure Fabrication</h2>

<p><em>A Pure Fabrication is a class that does not represent a concept in the
problem domain, specially made up to achieve low coupling, high
cohesion, and the reuse potential thereof derived (when a solution
presented by the Information Expert pattern does not). This kind of
class is called &ldquo;Service&rdquo; in Domain-driven design.</em></p>

<h5>References:</h5>

<ul>
<li><a href="http://en.wikipedia.org/wiki/GRASP_(object-oriented_design)">Wikipedia &ndash; General Responsibility Assignment Software Patterns</a></li>
<li><a href="http://vrybas.github.io/blog/2014/04/04/rails-and-pipes/">Rails and Pipes(DDD notes)</a></li>
<li><a href="http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/">Code Climate Blog &ndash; 7 Patterns to Refactor Fat ActiveRecord Models</a></li>
<li><a href="http://signalvnoise.com/posts/3372-put-chubby-models-on-a-diet-with-concerns">DHH &ndash; Put chubby models on a diet with concerns</a></li>
<li><a href="https://gist.github.com/ryanb/4172391">Ryan Bates &ndash; My issues with Modules</a></li>
<li><a href="http://www.sandimetz.com/blog/2009/06/12/ruby-case-statements-and-kind-of/">Sandi Metz &ndash; Ruby Case Statements and &lsquo;kind_of?&rsquo;</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Work-In-Progress Pull Requests]]></title>
    <link href="http://vrybas.github.io/blog/2014/04/11/wip-pull-requests/"/>
    <updated>2014-04-11T05:19:56+04:00</updated>
    <id>http://vrybas.github.io/blog/2014/04/11/wip-pull-requests</id>
    <content type="html"><![CDATA[<p>There&rsquo;ve been a lot of criticism(<a href="http://oldblog.antirez.com/post/pull-requests-are-not-conversations.html">1</a>,<a href="https://github.com/torvalds/linux/pull/17#issuecomment-5654674">2</a>,<a href="https://netguru.co/blog/posts/ups-and-downs-of-pull-request-flow-part-1">3</a>) on Github Pull Requests
lately.</p>

<p>The summary is pretty much in this(<a href="https://news.ycombinator.com/item?id=2183287">4</a>) Hacker News comment:</p>

<blockquote><p>&hellip;GitHub is missing this kind of environment(IRC, Mailing List), so
it&rsquo;s almost inevitable that first contact between maintainers and
potential contributors usually comes in the form of a pull request.</p></blockquote>

<p>&hellip; and there&rsquo;s no code review and discussion is done BEFORE a pull
request, which is the main reason so many PRs are not accepted.</p>

<p>While it&rsquo;s definitely true, it don&rsquo;t have to be this way.</p>

<p>There&rsquo;s a common mental model the Pull Request is something finished.
The ready-to-ship kind of thing.</p>

<p>Well, not always.</p>

<p>In our team, we send Pull Requests as soon as there&rsquo;s one commit in a
new branch. Or even empty commit.</p>

<p><code>git commit -m "initial commit: User can verify their account" --allow-empty</code></p>

<p>Contributor starts with some basic ideas, small patches,
failing tests. And then the PR grows collaboratively. Somebody
contributes with comment. Somebody does commit. Sometimes commits are
reverted and replaced with better solutions. It&rsquo;s like an asynchronous
pair programming with unlimited collaborators. The value is tremendous.</p>

<p>And PR got merged only when no one have better ideas. Ideally all team
members, who are interested in keeping codebase clean and to have a
collective code ownership(everyone, right?), should look through PR and
leave :thumbsup: in comments. One thumb is required, others are
optional(if you&rsquo;re in a hurry with shipment, which you souldn&rsquo;t).</p>

<p>Although this kind of Work-In-Progress PRs are possible only in
repositories in which everyone has commit access to, I&rsquo;d suggest to try
this approach when sending PR from a fork. The early-on feedback is
still very much valuable.</p>

<h5>References:</h5>

<ul>
<li><a href="http://oldblog.antirez.com/post/pull-requests-are-not-conversations.html">@antirez &ndash; Pull requests are not conversations</a></li>
<li><a href="https://news.ycombinator.com/item?id=2182873">↑ &amp; discussion on Hacker News</a></li>
<li><a href="https://github.com/torvalds/linux/pull/17#issuecomment-5654674">@torvalds &ndash; &ldquo;I don&rsquo;t do github pull requests&hellip;&rdquo;</a></li>
<li><a href="https://netguru.co/blog/posts/ups-and-downs-of-pull-request-flow-part-1">@madsheep &ndash; Ups and Downs of Pull Request Flow</a></li>
<li><a href="https://github.com/neovim/neovim/pulls">NEOVim pull-requests</a></li>
</ul>


<p><img src="http://cl.ly/image/1Q2r1J0L2b35/Screen%20Shot%202014-08-25%20at%202.40.17%20PM.png" alt="neovim-prs" /></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rails and Pipes (DDD Notes)]]></title>
    <link href="http://vrybas.github.io/blog/2014/04/04/rails-and-pipes/"/>
    <updated>2014-04-04T04:06:42+04:00</updated>
    <id>http://vrybas.github.io/blog/2014/04/04/rails-and-pipes</id>
    <content type="html"><![CDATA[<p>Very interesting talk on Rails apps architecture by Uncle Bob.</p>

<p><em>Ruby Midwest 2011 &ndash; Keynote: Architecture the Lost Years by Robert Martin</em></p>

<div class="embed-video-container"><iframe src="http://www.youtube.com/embed/WpkDN78P884 "></iframe></div>


<p>Here&rsquo;s the short summary:</p>

<p>&ldquo;The MVC is not your applcation architecture.  It&rsquo;s just a tiny part of
it. The real Application should have it&rsquo;s own architecture(built in with
the best OOP practices and patterns), and interact with your
I/O(controllers) and data storage(models) via limited API.</p>

<p>The real Application should be in <strong>Interactors</strong> &amp; <strong>Entities</strong>(or
similar structure). It should be in the <strong>Service Layer</strong>.</p>

<p><strong>Controller</strong> => <strong>Interactor</strong> => <strong>Entity</strong> => <strong>Model</strong></p>

<p>Controller shouldn&rsquo;t speak to the Model directly. And Model shouldn&rsquo;t
speak to other Models. And all the dependencies should go only one way,
but not the other.&#8221;</p>

<p>Well, this totally ruins the Rails Way, and not really applicable
directly. But that&rsquo;s a good start and occasion to think about
architecture, code organizing etc. Here&rsquo;s some materials on the topic I
found useful and pleasant to read:</p>

<ul>
<li><a href="https://practicingruby.com/articles/responsibility-centric-vs-data-centric-design">Practicing Ruby Issue #4.11  &ndash; &ldquo;Responsibility-centric vs.  data-centric design&rdquo;</a></li>
<li><a href="http://eng.joingrouper.com/blog/2014/03/03/rails-the-missing-parts-interactors">Grouper &ndash; &ldquo;Rails. The Missing Parts. Interactors.&rdquo;</a></li>
<li><a href="https://news.ycombinator.com/item?id=7335211">↑ &amp; epic discussion on Hacker News</a></li>
<li><a href="http://www.amazon.com/Object-Oriented-Software-Engineering-Approach/dp/0201544350/">InfoQ &ndash; Domain Driven Design Quickly</a></li>
<li><a href="http://blog.carbonfive.com/2012/01/10/does-my-rails-app-need-a-service-layer/">Jared Carrol &ndash; Does My Rails App Need a Service Layer?</a></li>
<li><a href="http://en.wikipedia.org/wiki/Data,_context_and_interaction">Wikipedia &ndash; Data, Context and Interaction(DCI)</a></li>
<li><a href="https://github.com/jonleighton/focused_controller">Focused Controller gem</a></li>
<li><a href="https://github.com/voxdolo/decent_exposure">Decent Exposure gem</a></li>
<li><a href="http://parley.rubyrogues.com/t/dhh-debating-controllers-abstracts-on-hn/1823/26">Ruby Rogues Parley discussion</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Refactoring Patterns: Method Object]]></title>
    <link href="http://vrybas.github.io/blog/2014/03/25/method-object/"/>
    <updated>2014-03-25T04:06:42+04:00</updated>
    <id>http://vrybas.github.io/blog/2014/03/25/method-object</id>
    <content type="html"><![CDATA[<p>Here&rsquo;s how you can deal with huge and messy method.</p>

<ol>
<li>Take a huge method and extract it to it&rsquo;s own class.</li>
<li>Pass all dependencies as parameters</li>
<li>Replace body of original method with Method Object call.</li>
</ol>


<h4>Before:</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">matches_conditions_hash?</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span> <span class="o">=</span> <span class="vi">@conditions</span><span class="p">)</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">conditions</span><span class="o">.</span><span class="n">empty?</span>
</span><span class='line'>    <span class="kp">true</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">model_adapter</span><span class="p">(</span><span class="n">subject</span><span class="p">)</span><span class="o">.</span><span class="n">override_conditions_hash_matching?</span> <span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span>
</span><span class='line'>      <span class="n">model_adapter</span><span class="p">(</span><span class="n">subject</span><span class="p">)</span><span class="o">.</span><span class="n">matches_conditions_hash?</span> <span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="n">conditions</span><span class="o">.</span><span class="n">all?</span> <span class="k">do</span> <span class="o">|</span><span class="nb">name</span><span class="p">,</span> <span class="n">value</span><span class="o">|</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">model_adapter</span><span class="p">(</span><span class="n">subject</span><span class="p">)</span><span class="o">.</span><span class="n">override_condition_matching?</span> <span class="n">subject</span><span class="p">,</span> <span class="nb">name</span><span class="p">,</span> <span class="n">value</span>
</span><span class='line'>          <span class="n">model_adapter</span><span class="p">(</span><span class="n">subject</span><span class="p">)</span><span class="o">.</span><span class="n">matches_condition?</span> <span class="n">subject</span><span class="p">,</span> <span class="nb">name</span><span class="p">,</span> <span class="n">value</span>
</span><span class='line'>        <span class="k">else</span>
</span><span class='line'>          <span class="n">attribute</span> <span class="o">=</span> <span class="n">subject</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="nb">name</span><span class="p">)</span>
</span><span class='line'>          <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">kind_of?</span><span class="p">(</span><span class="no">Hash</span><span class="p">)</span>
</span><span class='line'>            <span class="k">if</span> <span class="n">attribute</span><span class="o">.</span><span class="n">kind_of?</span><span class="p">(</span><span class="nb">Array</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">defined?</span><span class="p">(</span><span class="no">ActiveRecord</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">attribute</span><span class="o">.</span><span class="n">kind_of?</span><span class="p">(</span><span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Relation</span><span class="p">))</span>
</span><span class='line'>              <span class="n">attribute</span><span class="o">.</span><span class="n">any?</span> <span class="p">{</span> <span class="o">|</span><span class="n">element</span><span class="o">|</span> <span class="n">matches_conditions_hash?</span> <span class="n">element</span><span class="p">,</span> <span class="n">value</span> <span class="p">}</span>
</span><span class='line'>            <span class="k">else</span>
</span><span class='line'>              <span class="o">!</span><span class="n">attribute</span><span class="o">.</span><span class="n">nil?</span> <span class="o">&amp;&amp;</span> <span class="n">matches_conditions_hash?</span><span class="p">(</span><span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>            <span class="k">end</span>
</span><span class='line'>          <span class="k">elsif</span> <span class="o">!</span><span class="n">value</span><span class="o">.</span><span class="n">is_a?</span><span class="p">(</span><span class="nb">String</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">value</span><span class="o">.</span><span class="n">kind_of?</span><span class="p">(</span><span class="no">Enumerable</span><span class="p">)</span>
</span><span class='line'>            <span class="n">value</span><span class="o">.</span><span class="n">include?</span> <span class="n">attribute</span>
</span><span class='line'>          <span class="k">else</span>
</span><span class='line'>            <span class="n">attribute</span> <span class="o">==</span> <span class="n">value</span>
</span><span class='line'>          <span class="k">end</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h4>After:</h4>

<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='ruby'><span class='line'><span class="k">def</span> <span class="nf">matches_conditions_hash?</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span> <span class="o">=</span> <span class="vi">@conditions</span><span class="p">)</span>
</span><span class='line'>  <span class="no">IsMatchesConditionsHash</span><span class="o">.</span><span class="p">(</span><span class="n">model_adapter</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">IsMatchesConditionsHash</span> <span class="o">&lt;</span> <span class="no">Struct</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:model_adapter</span><span class="p">,</span> <span class="ss">:subject</span><span class="p">,</span> <span class="ss">:conditions</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">call</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
</span><span class='line'>    <span class="kp">new</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span><span class="o">.</span><span class="n">call</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">model_adapter</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span><span class="p">)</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">.</span><span class="n">model_adapter</span>  <span class="o">=</span> <span class="n">model_adapter</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">.</span><span class="n">subject</span>    <span class="o">=</span> <span class="n">subject</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">.</span><span class="n">conditions</span> <span class="o">=</span> <span class="n">conditions</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># ======== actual logic</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">call</span>
</span><span class='line'>    <span class="k">return</span> <span class="kp">true</span> <span class="k">if</span> <span class="n">conditions</span><span class="o">.</span><span class="n">empty?</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="n">model_adapter</span><span class="o">.</span><span class="n">override_conditions_hash_matching?</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span><span class="p">)</span>
</span><span class='line'>      <span class="k">return</span> <span class="n">model_adapter</span><span class="o">.</span><span class="n">matches_conditions_hash?</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">conditions</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">conditions</span><span class="o">.</span><span class="n">all?</span> <span class="k">do</span> <span class="o">|</span><span class="nb">name</span><span class="p">,</span> <span class="n">value</span><span class="o">|</span>
</span><span class='line'>      <span class="k">if</span> <span class="n">model_adapter</span><span class="o">.</span><span class="n">override_condition_matching?</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="nb">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">model_adapter</span><span class="o">.</span><span class="n">matches_condition?</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="nb">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="n">condition_match?</span><span class="p">(</span><span class="n">subject</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="nb">name</span><span class="p">),</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="c1"># ========</span>
</span><span class='line'>
</span><span class='line'>  <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">condition_match?</span><span class="p">(</span><span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">value</span>
</span><span class='line'>    <span class="k">when</span> <span class="no">Hash</span>       <span class="k">then</span> <span class="n">match_hash_value</span><span class="p">(</span><span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="k">when</span> <span class="no">Enumerable</span> <span class="k">then</span> <span class="p">(</span><span class="o">!</span><span class="n">value</span><span class="o">.</span><span class="n">is_a?</span><span class="p">(</span><span class="nb">String</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">value</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">attribute</span><span class="p">))</span>
</span><span class='line'>    <span class="k">else</span> <span class="n">attribute</span> <span class="o">==</span> <span class="n">value</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">match_hash_value</span><span class="p">(</span><span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">attribute</span><span class="o">.</span><span class="n">kind_of?</span><span class="p">(</span><span class="nb">Array</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">defined?</span><span class="p">(</span><span class="no">ActiveRecord</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">attribute</span><span class="o">.</span><span class="n">kind_of?</span><span class="p">(</span><span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Relation</span><span class="p">))</span>
</span><span class='line'>      <span class="n">attribute</span><span class="o">.</span><span class="n">any?</span> <span class="p">{</span> <span class="o">|</span><span class="n">element</span><span class="o">|</span> <span class="nb">self</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="p">(</span><span class="n">model_adapter</span><span class="p">,</span> <span class="n">element</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="o">!</span><span class="n">attribute</span><span class="o">.</span><span class="n">nil?</span> <span class="o">&amp;&amp;</span> <span class="nb">self</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="p">(</span><span class="n">model_adapter</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can go deeper of course. Shave the yak completely. But that&rsquo;s a good
start.</p>

<p>Actually, in this case, the implementation was moved back to the
original class with a few private methods extracted. But nice thing
about Method Object &ndash; it helps to understand all the dependencies this
beast have, so you can deal with it.</p>

<h6>References:</h6>

<ul>
<li><a href="http://sourcemaking.com/refactoring/replace-method-with-method-object">SourceMaking &ndash; Replace Method with Method Object</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How Do I Pomodoro]]></title>
    <link href="http://vrybas.github.io/blog/2013/07/24/how-do-i-pomodoro/"/>
    <updated>2013-07-24T04:38:21+04:00</updated>
    <id>http://vrybas.github.io/blog/2013/07/24/how-do-i-pomodoro</id>
    <content type="html"><![CDATA[<p>I&rsquo;m a big fan of <strong>Pomodoro</strong> time management technique, created by
Francesco Cirillo in the 80&rsquo;s. In this post I&rsquo;ll describe what it is,
how I use it (tools and their tweaks), and how it helps me to get
through my day.</p>

<!-- more -->


<h2>Interruptions</h2>

<p>Interruptions are horrible. They are your productivity&rsquo;s worst enemy.
Jason Fried gave excellent talk(<a href="http://www.ted.com/talks/jason_fried_why_work_doesn_t_happen_at_work.html">1</a>) on that. In fact, the most
important measurement of quality of your working environment (and
working style) is how much uninterrupted time you can have per day.</p>

<div class="embed-video-container"><iframe src="http://www.youtube.com/embed/5XD2kNopsUs "></iframe></div>


<p>There are external interruptions. A dozen of devices ring one after another.
Notifications pop up on the screen. People come talk to us.  It&rsquo;s hard to get
away even for 10 minutes.</p>

<p>And there&rsquo;s also interruptions of our own. We become &ldquo;tired&rdquo; and &ldquo;feel like
we deserve a break&rdquo;. We open up twitter and who knows if we come back
for next 30 minutes. Or some idea just hits us in the middle of the working
day, and we can&rsquo;t help but think it over and over again, while, at the same
time, trying to stay focused on current task.</p>

<p>The thing is, we can&rsquo;t just shut off all notifications and don&rsquo;t talk to
anybody. At least not for a long time. We are team workers, and have to
interact. And we can&rsquo;t work without breaks all day long, but we want to
control them, so a little trip to Facebook don&rsquo;t become a time-wasting
disaster. We don&rsquo;t want our brilliant ideas to get lost, but we want to work
on them on priority.</p>

<h2>Pomodoro is here to help</h2>

<p>So there&rsquo;s a Pomodoro Technique(<a href="http://en.wikipedia.org/wiki/Pomodoro_Technique">2</a>). In short, it offers you to work
for a fixed amount of time, and then have a fixed short break.</p>

<p>You set a timer to 25 minutes(canonical), and, during that time, you shut off
all your notifications. Nothing and no one can interrupt you, even your
lazy-self. You&rsquo;re don&rsquo;t let yourself to think of any extraneous thoughts. If
something pops up to your mind, you write it down somewhere (if it&rsquo;s worth it)
to come back to it later.</p>

<p>After timer rings, you immediately stop what you&rsquo;re doing, checking your email
and other messages, you go get that co-worker who had a question. And, more
importantly, you&rsquo;re having a scheduled break for 5 minutes or so. It&rsquo;s a good
time to stand up, walk outside, get some water and fresh air, limber
up(<a href="http://infographicsmania.com/wp-content/uploads/2012/09/Bathroom-Break-Yoga-infographic.jpg">3</a>), give your brain, eyes and whole body some rest.</p>

<p><a href="http://infographicsmania.com/wp-content/uploads/2012/09/Bathroom-Break-Yoga-infographic.jpg"><img src="http://f.cl.ly/items/0h1M0D2T3T2n1W1e0h2A/Screen%20Shot%202014-03-24%20at%2011.01.21%20PM.png" alt="" /></a></p>

<p>This is just the right balance between work and rest. It gives ability
to be interrupted and communicate only when you&rsquo;re available and when you don&rsquo;t mind.
It also makes you more self-disciplined in general, as well as it&rsquo;s a good will-power
exercise.</p>

<h2>But how &lsquo;bout my Flow?</h2>

<p>Yes, Flow(<a href="http://en.wikipedia.org/wiki/Flow_(psychology)">4</a>). The thing with Flow is hard to get in and so easy to get
out. Some of Pomodoro newbies complain that this forced break is a
Flow-killer. But I don&rsquo;t think it is. Well, Pomodoro helps you to get into the
Flow at the first place. And, again, you don&rsquo;t want to get lost for several
hours for the team, draining all of your brain &amp; body power, leaving yourself
completely exhausted. Pomodoro helps to stay productive all day
(days/weeks/months), not just a few Flow hours.</p>

<p>Yes, it is hard to go for a break when you&rsquo;re in a middle of something. But it
teaches you to respect your time and &ldquo;protect Pomodoro at all cost&rdquo;, so you
can do more. Also, when it&rsquo;s couple of minutes left on the timer, you should
start writing down what you&rsquo;re doing now, and what you&rsquo;re going to do next
(which is a good thing to do anyway). So, for me, that&rsquo;s never been a problem
to get back to the same point after a break.</p>

<p>But every workflow is different, and every mind is different. For example in
ability to stay focused on a task for a long period of time. So, I can suggest
to just try it out for a few days and see if it works for you.</p>

<h2>Start it slow</h2>

<p>A quick tip for Pomodoro beginners &ndash; start it slow. Maybe you would want
to start with 10 minutes of high quality Pomodoro. Or even 5 minutes. And
increase length over time. Because successful short Pomodoros are better than
few <a href="https://twitter.com/kubem/status/350390764515233792">#procastodoros</a>.</p>

<p><a href="https://twitter.com/kubem/status/350390764515233792"><img src="http://f.cl.ly/items/2L2Z2j2s243l3G1W0A2v/Screen%20Shot%202014-03-24%20at%2010.54.38%20PM.png" alt="" /></a></p>

<h2>There&rsquo;s an app for that</h2>

<p>In fact, that can be enough to use a kitchen timer (the one they&rsquo;re
offering(<a href="http://pomodorotechnique.com/timer/">5</a>) on the site) with just pen &amp; paper. I know a bunch of
developers who use pen &amp; notebooks as a primary GTD tool and it works
for them. And if it works for you &ndash; that&rsquo;s fine. But there sure some
apps. I&rsquo;ll describe my favorites. Each one works best in certain
circumstances.</p>

<h4><a href="http://tomato.es/">Tomato.es (Web)</a></h4>

<p>The best way to try Pomodoro out. You don&rsquo;t have to install anything. Just
open new tab with this application, sign in with Github or Twitter
(optionally), hit Space bar and see the timer going. It&rsquo;ll ring when Pomodoro
is over and send Chrome&rsquo;s desktop notification. And there&rsquo;s a nice daily,
weekly, monthly stats and even Leaderboards.  After all, it&rsquo;s
<a href="https://github.com/potomak/tomatoes">opensource</a>, so you can suggest (or
better develop) some new features.</p>

<h4><a href="http://teamodoro.com/">Teamodoro.com (Web)</a></h4>

<p>This one is for team synchronization. You can&rsquo;t stop and start Pomodoros,
they&rsquo;re just starting automatically and counter is the same for everybody. And
nice and simple fullscreen view to put on external monitor/device.</p>

<h4><a href="https://itunes.apple.com/us/app/wind-up-timer/id325610693?mt=8">Wind-up Timer (iOS)</a></h4>

<p>Swipe to set a timer to desired time. It ticks in the background. It rings in
the end. Nice design. It is always in your pocket or desk. Well, mostly. I was
happy with it at first. But there&rsquo;s no way to disable iOS notifications and
keep the screen unlocked. So you basically set a timer and enable airplane
mode or lock the screen. Still useful sometimes.</p>

<h4><a href="https://github.com/ugol/pomodoro">Pomodoro.app (OSX)</a></h4>

<p><img src="http://f.cl.ly/items/2w34273X191m1W1r1B2i/Screenshot_6_30_13_9_15_PM-2.png" alt="" /></p>

<p>The most advanced OSX Pomodoro I&rsquo;ve seen. You can configure everything.
Pomodoro length, break length, automatic restarts, sounds, Growl
notifications, Twitter integration, Calendar intergration and, finally,
execute applescripts on Pomodoro start/end/reset/interrupt/resume. And this
opens endless possibilities, like disabling all possible notifications on your
Mac &amp; Devices when Pomodoro starts, and automatically re-enable them when
Pomodoro ends. And, for instance, send message to Campfire. Check out my
set of <a href="https://github.com/vrybas/pomodoro-scripts">pomodoro-scripts</a>.</p>

<p>The app is <a href="https://github.com/ugol/pomodoro">opensource</a>, you can compile it with XCode. Or download
precompiled version <a href="http://cl.ly/3Q3H333y2132">here</a>.</p>

<h4><a href="https://itunes.apple.com/us/app/breaktime/id427475982?mt=12">BreakTime (OSX)</a></h4>

<p>This is not exactly a Pomodoro. It&rsquo;s just locks your screen after specified
amount of time, so you are forced to take a break. Sometimes I use it
standalone, or accompanied with other tools. Can execute applescripts.</p>

<h4><a href="http://thymerb.com/">Thyme (console)</a></h4>

<p>And, finally, the terminal solution. Which have nice progressbar, Tmux
integration, and can also execute applescripts (via <a href="https://github.com/vrybas/dotfiles/blob/4b0f29c0dd688f8bb78eec5d66f6c657a4bf9bb1/thyme/thymerc.symlink#L4">osascript</a>) or any
other terminal commands.</p>

<p>That&rsquo;s it. Be productive, respect your time, and happy Pomodoring!</p>

<h6>References:</h6>

<ul>
<li><a href="http://www.ted.com/talks/jason_fried_why_work_doesn_t_happen_at_work.html">Jason Fried &ndash; Why work doesn&rsquo;t happen at work</a></li>
<li><a href="http://en.wikipedia.org/wiki/Pomodoro_Technique">The Pomodoro Technique by Francesco Cirillo</a></li>
<li><a href="http://infographicsmania.com/wp-content/uploads/2012/09/Bathroom-Break-Yoga-infographic.jpg">Bathroom Break Yoga &ndash; infographic</a></li>
<li><a href="http://en.wikipedia.org/wiki/Flow_(psychology)">Flow &ndash; Wikipedia</a></li>
<li><a href="http://pomodorotechnique.com/timer/">The Pomodoro Technique official timer</a></li>
<li><a href="http://tomato.es/">Tomato.es</a></li>
<li><a href="http://teamodoro.com/">Teamodoro.com</a></li>
<li><a href="https://itunes.apple.com/us/app/wind-up-timer/id325610693?mt=8">Wind-up Timer</a></li>
<li><a href="https://github.com/ugol/pomodoro">Pomodoro.app</a></li>
<li><a href="https://github.com/vrybas/pomodoro-scripts">Pomodoro Scripts</a></li>
<li><a href="https://itunes.apple.com/us/app/breaktime/id427475982?mt=12">BreakTime</a></li>
<li><a href="http://thymerb.com/">Thyme</a></li>
</ul>

]]></content>
  </entry>
  
</feed>
