<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jason Lotito &#187; Programming</title>
	<atom:link href="http://www.jasonlotito.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jasonlotito.com</link>
	<description>Programming, technology, and bits about my life</description>
	<lastBuildDate>Tue, 27 Mar 2012 20:24:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>vim faster with maps</title>
		<link>http://www.jasonlotito.com/programming/vim-faster-with-maps/</link>
		<comments>http://www.jasonlotito.com/programming/vim-faster-with-maps/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 03:27:48 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=139</guid>
		<description><![CDATA[At myYearbook, we pack our JS and CSS files.  Much of this is automated now with Jenkins, but we still have legacy code that must be packed by hand.  And, at the same time, it&#8217;s generally wise to test your product with a packed file regardless.  I also try to make it a habit to [...]]]></description>
			<content:encoded><![CDATA[<p>At myYearbook, we pack our JS and CSS files.  Much of this is automated now with Jenkins, but we still have legacy code that must be packed by hand.  And, at the same time, it&#8217;s generally wise to test your product with a packed file regardless.  I also try to make it a habit to phpcs my file as often as possible.  The goal here is to keep the file as tidy as possible, and not waiting for the end to fix everything.  Also, during the course of development, I might come across other little things I want to do, and I use vim to make things as easy as possible.</p>
<h2>maps</h2>
<p>I do this through maps.  I map is fairly simple to setup in vim.</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span><span style="color: #804040;">map</span> ,cs <span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;:</span>w\<span style="color: #000000;">|:!</span>phpcs <span style="color: #000000;">--</span>standard=myYearbook <span style="color: #000000;">%:</span>p</pre></div></div>

<p>Typing that into a vim instance means I can type &#8216;,cs&#8217; in normal mode, and vim will w my file to disk, then run phpcs using the myYearbook standard on the current file I&#8217;m working in.  Now, I use that often enough that I&#8217;ve added it to my .vimrc file.</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">map</span> <span style="color: #000000;">&lt;</span>Leader<span style="color: #000000;">&gt;</span>cs <span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;:</span>w\<span style="color: #000000;">|:!</span>phpcs <span style="color: #000000;">--</span>standard=myYearbook <span style="color: #000000;">%:</span>p</pre></div></div>

<p>If you aren&#8217;t aware, &lt;Leader&gt; is set to &#8216;,&#8217; using this bit of code in .vimrc:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">let</span> mapleader = <span style="color: #C5A22D;">&quot;,&quot;</span></pre></div></div>

<p>The comma is popular, but you can use whatever you want, really.</p>
<p>I can also pack my javascript and CSS files with similar ease using &#8220;,pj&#8221; and &#8220;,pc&#8221; (pack javascript, and pack css).</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">map</span> <span style="color: #000000;">&lt;</span>Leader<span style="color: #000000;">&gt;</span>pj <span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;:!</span>jspack <span style="color: #000000;">%:</span>p <span style="color: #000000;">&gt;</span> <span style="color: #000000;">%:</span>p<span style="color: #000000;">:</span>h<span style="color: #000000;">/%:</span>r<span style="color: #000000;">:</span>r<span style="color: #000000;">.</span><span style="color: #668080;">js</span>
<span style="color: #804040;">map</span> <span style="color: #000000;">&lt;</span>Leader<span style="color: #000000;">&gt;</span>pc <span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;:!</span>csspack <span style="color: #000000;">%:</span>p <span style="color: #000000;">&gt;</span> <span style="color: #000000;">%:</span>p<span style="color: #000000;">:</span>h<span style="color: #000000;">/%:</span>r<span style="color: #000000;">:</span>r<span style="color: #000000;">.</span>css</pre></div></div>

<p><iframe style="float: right;width: 120px; height: 240px; margin-left: 10px; margin-bottom: 10px" src="http://rcm.amazon.com/e/cm?lt1=_blank&amp;bc1=000000&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=jasonlotito-20&amp;o=1&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=059652983X" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="320" height="240"></iframe></p>
<p>You might be wondering what the &#8216;%:p&#8217; part is.  It&#8217;s actually part of the <a href="http://vimdoc.sourceforge.net/htmldoc/eval.html#expand()">expand functionality</a> of vim.  Basically, the important parts here to understand is that the % is the current file name.  The &#8220;:p&#8221; and &#8220;:r&#8221; parts are the modifiers.  In this case, &#8220;%:p&#8221; has us expand to the full path.  The &#8220;:h&#8221; modifier removes the last component.  So, &#8220;%:p:h&#8221; basically says &#8220;Give us the full path, and then remove the head.  The head in this case is the file name, so if the full path (&#8220;%:p&#8221;) is /Users/jasonlotito/test.php, &#8220;%:p:h&#8221; gives us &#8220;/Users/jasonlotito&#8221;.</p>
<p>&#8220;%:p:h/%:r:r.js&#8221; allows me to rename my file.  In our setup, unpacked files contain the extension &#8220;.unpack.&#8221;  By Simply using the &#8220;:r:r&#8221; modifiers, I strip off the &#8220;.js&#8221;, and then the &#8220;.unpack&#8221;, leaving me with just the file name.  So, if my javascript file was named Modifiers.unpack.js, I&#8217;d be left with Modifiers.  I prefix that with %:p:h/ which gives me the directory the file is in, and append &#8220;.css&#8221; to the path.</p>
<p>Being able to map commonly performed actions, especially to external tools, means I can stay in vim longer.  Sure, I&#8217;ll still background vim when I need to perform certain actions, but I&#8217;m more apt to start scripting repeated actions, and then mapping them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/vim-faster-with-maps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Someone thinks this is good</title>
		<link>http://www.jasonlotito.com/programming/someone-thinks-this-is-good/</link>
		<comments>http://www.jasonlotito.com/programming/someone-thinks-this-is-good/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 21:22:29 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=136</guid>
		<description><![CDATA[No.  How this is considered good, I don&#8217;t know.  Who wrote the code isn&#8217;t important &#8211; this is the way the code is supposed to be written.  At least, this is the way it&#8217;s being demonstrated.  With a straight face. I cannot fathom how anyone can honestly think this is good and want to encourage [...]]]></description>
			<content:encoded><![CDATA[<p>No.  How this is considered good, I don&#8217;t know.  Who wrote the code isn&#8217;t important &#8211; this is the way the code is supposed to be written.  At least, this is the way it&#8217;s being demonstrated.  With a straight face.</p>
<p>I cannot fathom how anyone can honestly think this is good and want to encourage its use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #666666; font-style: italic;">// lots of config stuff here</span>
&nbsp;
            <span style="color: #009933; font-style: italic;">/**
             * Routes
             */</span>
&nbsp;
            <span style="color: #0000ff;">'ZendMvcRouterRouteStack'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'parameters'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'routes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #0000ff;">'zfcuser'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                            <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Literal'</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'priority'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                <span style="color: #0000ff;">'route'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/user'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'defaults'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                    <span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'zfcuser'</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">'action'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span>
                                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                            <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'may_terminate'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'child_routes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                <span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                    <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Literal'</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">'options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                        <span style="color: #0000ff;">'route'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/login'</span><span style="color: #339933;">,</span>
                                        <span style="color: #0000ff;">'defaults'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                            <span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'zfcuser'</span><span style="color: #339933;">,</span>
                                            <span style="color: #0000ff;">'action'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'login'</span><span style="color: #339933;">,</span>
                                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// more config stuff here</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/someone-thinks-this-is-good/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning the vi language</title>
		<link>http://www.jasonlotito.com/programming/learning-the-vi-language/</link>
		<comments>http://www.jasonlotito.com/programming/learning-the-vi-language/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 21:58:55 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=132</guid>
		<description><![CDATA[I&#8217;m a long time user of vi (and I use vi interchangeably for both vi and vim), but it wasn&#8217;t until recently that I really started to understand the way vi works.  Oh, I could be productive in vi, but I wasn&#8217;t adept at learning vi.  I edit files in vi, I could open windows, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a long time user of vi (and I use vi interchangeably for both vi and vim), but it wasn&#8217;t until recently that I really started to understand the way vi works.  Oh, I could be productive in vi, but I wasn&#8217;t adept at learning vi.  I edit files in vi, I could open windows, I could move around, copy and paste, search and replace, and generally do the normal things you can do in other editors.  So when I had to open up vi, I didn&#8217;t freeze up and get lost.</p>
<h2>Don&#8217;t Repeat Yourself</h2>
<p>The problem here is two-fold.  First, I was looking for how to replicate actions I would perform in other editors. Saving a file, editing text, copy and paste, search and replace: all these things easily map to something you&#8217;d do in a traditional IDE.  You don&#8217;t replace a character in a traditional IDE.  You delete a character and enter a new one.  To copy and paste big blocks of text, you highlight that text with a mouse.  You navigate to different tabs with your mouse.  When you want to split your window, you probably have to do so with the menu.  You don&#8217;t append to a line.  You simply move your cursor and start typing.  You don&#8217;t jump to a character, and while you can, you rarely jump to a line.  You aren&#8217;t yanking lines, you don&#8217;t concern yourself with buffers, and you don&#8217;t repeat your actions.</p>
<p>Basically, an IDE more closely resembles a word processor for programming languages, whereas vi is a completely different beast (vi vi vi, editor of the beast, after all).</p>
<p>Therefore, the first thing I had to understand before really learning vi was I had to look past replacing what I did with an IDE, but rather, understand how to do things in vi.  And this is a fairly easy concept to understand once you learn one of the defining principles of using vi: if you have to keep pressing the same key to do something, you are probably doing it wrong.  Consider the basic movement keys: hjkl.  Using &#8216;j&#8217; and &#8216;k&#8217; to move more than one line should indicate to you there is a better way.  Moving to the top of the screen, the bottom, the middle, move up a page, half a page, or down.  These aren&#8217;t things you do in an IDE (you scroll), but in vi, that is an essential piece to the puzzle.</p>
<p>So, every time I wanted to do something, and I found myself repeating actions that seemed awkward, I&#8217;d search for a way to do it.</p>
<p>Find out more about <a href="http://blog.sanctum.geek.nz/vim-anti-patterns/">noticing and unlearning vi anti-patterns</a> with this excellent article from Tom Ryder.</p>
<h2>Learn the Language</h2>
<p>This leads us to the second thing I had to understand in order to really being learning vi.  You don&#8217;t learn vi by learning commands, you learn vi by learning a language.  Once you have a firm grasp of the language, you can start putting together your own commands.  Discovering new commands seems to become more intuitive.</p>
<p>Lets see this language in action.  Normally, if you want to delete a line, you&#8217;d type &#8216;dd&#8217;.  This deletes the line (and stores it away so you can paste it later if you want).  What if you want to delete more than one line?  You could repeat &#8216;dd&#8217; on each line you wanted deleted.  But let&#8217;s say you want to delete the next 10 lines.    This is where the language comes in.  What you want is to delete 10 lines down.  So, you start with &#8216;d&#8217;, then enter 10, and finally, &#8216;j&#8217; for:</p>
<pre>d10j</pre>
<p>This isn&#8217;t so much a command you need to remember.  Rather, it&#8217;s easier if you understand the language.  Deleting 5 lines up can be constructed the same way:</p>
<pre>d5k</pre>
<p>Once we learn how this language works, we can create our own commands</p>
<p>&#8216;==&#8217; will indent the current line.  We can follow the same pattern as delete to construct the following:</p>
<pre>=5j</pre>
<p>This properly indents the next 5 lines.  Of course, we can also do &#8216;gg=G&#8217;, which will move you to the first line, and then indent all lines until the last line.  We can do &#8216;ggdG&#8217; as well, which will delete all the lines in the file.</p>
<p>The actual commands aren&#8217;t important.  Rather, it&#8217;s the language.  Once you understand how &#8216;d&#8217; works, you can use the same language in other areas as well.</p>
<p>Want to copy the next five lines?  Just knowing that &#8216;yy&#8217; will copy the current line, you could easily discern that &#8216;y5k&#8217; will yank (copy) 5 lines up.</p>
<p>You can <a href="http://yanpritzker.com/2011/12/16/learn-to-speak-vim-verbs-nouns-and-modifiers/">learn about speaking the vi language</a> by reading an excellent article from Yan Pritzker.</p>
<h2>Investment of time</h2>
<p><iframe style="float: right; width: 120px; height: 240px;" src="http://rcm.amazon.com/e/cm?lt1=_blank&amp;bc1=000000&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=jasonlotito-20&amp;o=1&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=059652983X" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="320" height="240"></iframe><br />
Yes, learning vi will take time. It&#8217;s not something that will come naturally.  It&#8217;s a tool, and like any powerful tool, you have a lot to learn to truly start taking advantage of it.  It&#8217;s a professionals tool.  It&#8217;s a craftsman&#8217;s tool.  It&#8217;s something that, regardless of whatever else you might learn, will benefit you as long as you are programming.</p>
<p>Sure, you might enjoy your IDE, and I&#8217;m not here to suggest you drop your favorite tools.  Rather, it&#8217;s worth the investment to understand vi, it&#8217;s language, and how it works.  Even if you aren&#8217;t using it every single day, you&#8217;ll appreciate being able to open up the editor and using it without feeling constrained.</p>
<h2>Other Resources</h2>
<ul>
<li><a href="http://vimuniversity.com/">VIM University</a></li>
<li><a href="http://vimcasts.org/">VIM Casts</a></li>
<li><a href="http://yanpritzker.com/2011/12/16/learn-to-speak-vim-verbs-nouns-and-modifiers/">Learn to speak vim</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/learning-the-vi-language/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Installing PEAR on Lion</title>
		<link>http://www.jasonlotito.com/programming/php-programming/installing-pear-on-lion/</link>
		<comments>http://www.jasonlotito.com/programming/php-programming/installing-pear-on-lion/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 13:49:14 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[lion]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=124</guid>
		<description><![CDATA[Needed to install PEAR on Lion, as my laptop hadn&#8217;t yet needed it.  Wanted to see fi there was a way within Lion, and discovered the instructions. sudo cp /private/etc/php.ini.default /private/etc/php.ini php /usr/lib/php/install-pear-nozlib.phar pear config-set php_ini /private/etc/php.ini pecl config-set php_ini /private/etc/php.ini sudo pear upgrade-all]]></description>
			<content:encoded><![CDATA[<p>Needed to install PEAR on Lion, as my laptop hadn&#8217;t yet needed it.  Wanted to see fi there was a way within Lion, and <a href="http://blog.stealth35.com/2011/07/27/install-pear-on-osx-lion.html">discovered the instructions</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.ini.default <span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.ini
php <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>php<span style="color: #000000; font-weight: bold;">/</span>install-pear-nozlib.phar
pear config-set php_ini <span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.ini
pecl config-set php_ini <span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.ini
<span style="color: #c20cb9; font-weight: bold;">sudo</span> pear upgrade-all</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/php-programming/installing-pear-on-lion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Blocks</title>
		<link>http://www.jasonlotito.com/programming/blocks/</link>
		<comments>http://www.jasonlotito.com/programming/blocks/#comments</comments>
		<pubDate>Sat, 29 Oct 2011 13:50:54 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lessons]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=116</guid>
		<description><![CDATA[Do one thing. Do it well. Keep it concise. Use only what is given. Today we continue from Comments and explore how to refactor out code from blocks.  In Comments, we learned to extract code out into it&#8217;s own method whenever we use a comment to describe a block of code.  Today, we are going to [...]]]></description>
			<content:encoded><![CDATA[<blockquote><ol>
<li>Do one thing.</li>
<li>Do it well.</li>
<li>Keep it concise.</li>
<li>Use only what is given.</li>
</ol>
</blockquote>
<div>Today we continue from <a title="Comments" href="http://www.jasonlotito.com/programming/comments/">Comments</a> and explore how to refactor out code from blocks.  In Comments, we learned to extract code out into it&#8217;s own method whenever we use a comment to describe a block of code.  Today, we are going to look at how to do the same thing with blocks.</div>
<div>A block of code is any code that exists within a if, a loop, or a switch statement.  A good rule of thumb is every time you indent your code.  Our goal is to keep indents inside your code from going beyond 2 levels.  Let&#8217;s look at a typical method where we break this rule.</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$adminForm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dealership_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$mAdmin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mAdmin</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createAdmin</span><span style="color: #009900;">&#40;</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notify'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notifyPhone'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_PhoneNotification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Notification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$notification</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendNotification</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_forward <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'confirm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/admin/create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adminForm</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Before we start refactoring this, I want to cover a couple things.  First, the above is a small example, but it can still be used to explain our need.  Secondly, what we learn here can be applied to code, regardless of the size or complexity.</p>
<h2>Complexity</h2>
<p>Before we set out on this road, we need to define complexity.  Specifically, I&#8217;m referring to <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic Complexity</a>.  The best definition of Cyclomatic Complexity that I&#8217;ve found is from the <a href="http://phpmd.org/rules/codesize.html#cyclomaticcomplexity">PHPMD page on Cyclomatic Complexity</a>.</p>
<blockquote><p>Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are &#8216;if&#8217;, &#8216;while&#8217;, &#8216;for&#8217;, and &#8216;case labels&#8217;. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.</p></blockquote>
<p>Our goal is to keep what we write in the low complexity category.  At this point, we should go through the sample code from above, and figure out how much complexity there is.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$adminForm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dealership_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 2</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 3</span>
            <span style="color: #000088;">$mAdmin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mAdmin</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createAdmin</span><span style="color: #009900;">&#40;</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notify'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 4</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notifyPhone'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 5</span>
                    <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_PhoneNotification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 6</span>
                    <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Notification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$notification</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendNotification</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">// Complexity 7</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_forward <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'confirm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/admin/create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adminForm</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A total of 7 complexity, on the high end of moderate complexity.  You might be looking at this code and wondering what makes it so complex.  It is simple enough.  It&#8217;s built to handle a form.  It checks input, creates a new user if the form was submitted in a valid format, and sets up notifications that were obviously a choice in the form.  It finally forwards off to the confirmation.</p>
<p>But, that is actually a lot of complexity.  To test this function, we&#8217;d need at least 7 test cases.  Even more so, this method violates some of our rules.  Do one thing.  It does many.  It&#8217;s not concise.  And it&#8217;s also not just using what is given.</p>
<p>We are going to focus on the complexity portion today.  We are going to bring down the size of this method step by step.</p>
<h2>Indents are intents to modularize</h2>
<p>Every time you indent your code, you should be thinking about modularizing.  In other words, if you indent your code into a block, you should consider moving that code into it&#8217;s own method.</p>
<p>Let&#8217;s start with the extremely large if in the middle there.  The one that checks to see if the request is a post.  When we indent that block of code inside, we should ask ourselves if that code inside can be better moved to it&#8217;s own method.  The answer is a resounding yes.  Within that block of code, we have 5 levels of complexity.  By moving all that complexity out of the createAction method, we will help decrease the complexity of that single method.  More so, we make that method easier to read, and we are able to compartmentalize the code even more.  We make our code even more reusable.</p>
<p>Where with comments we used the comment as an indicator of a new method, here we use the ideated block of code as an indicator.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$adminForm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dealership_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 2</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">letFormHandleInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/admin/create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adminForm</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> letFormHandleInput <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 2</span>
&nbsp;
        <span style="color: #000088;">$mAdmin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mAdmin</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createAdmin</span><span style="color: #009900;">&#40;</span>
            <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notify'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 3</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notifyPhone'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 4</span>
                <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_PhoneNotification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 5</span>
                <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Notification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$notification</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendNotification</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 6</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_forward<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'confirm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Referring back to createAction, we see that the complexity has dropped to a mere 2 points.  Granted, letFormHandleInput is 6 points, but we aren&#8217;t done yet.  Each method should be easy to understand, and easy to test.  createAction is now incredibly easy to understand.  It creates a new form for display, and if it receives a post request, it let&#8217;s the form handle the input.</p>
<p>I&#8217;ll admit, I&#8217;m not too enamoured with the name of the new method, but it&#8217;s simple, and defines exactly what the method does.  More importantly, we&#8217;re also passing the form in.  This means that we can pass this method any form.  If something fails, we come out of the method, and continue with the display of the form.  Normally, I&#8217;d want to also define letFormhandleInput as</p>

<div class="wp_syntax"><div class="code"><pre class="php-brief" style="font-family:monospace;">protected <span style="color: #000000; font-weight: bold;">function</span> letFormHandleInput<span style="color: #009900;">&#40;</span>Zend_Validate_Interface <span style="color: #0000ff;">$adminForm</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Here, $adminForm would be explicit in what it is, and I&#8217;d pass in the $_POST data, rather than calling it.  In the method itself, I&#8217;d swap out the calls to $adminForm-&gt;getValue, and instead call directly from $data.  However, this goes beyond our discussion here.  However, I make mention of this because those simple changes are not only easy to make, but value in terms of maintainability, testing, and longevity.</p>
<p>Because letFormHandleInput is still complex, we need to extract some methods out from it as well.  letFormHandleInput is actually wrapped directly around a giant if.  A simple check to see if the $_POST data is valid.  After that point, everything is indented. It also happens that the form doesn&#8217;t do much after that.  We grab data from it, but as I noted above, that&#8217;s easily removed. Regardless, the sign is fairly clear: indented blocks are what we are looking for.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$adminForm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dealership_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 2</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validateNewUserDataInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/admin/create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adminForm</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> validateNewUserDataInput <span style="color: #009900;">&#40;</span>Zend_Validate_Interface <span style="color: #000088;">$adminForm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$adminForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 2</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addNewUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> addNewUser <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$mAdmin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mAdmin</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createAdmin</span><span style="color: #009900;">&#40;</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'role'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'notify'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 2</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'notifyPhone'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Complexity 3</span>
            <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_PhoneNotification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 4</span>
            <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Notification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$notification</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendNotification</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 5</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_forward<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'confirm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once again, we&#8217;ve reduced complexity, and in our resulting method, our complexity has gone down.  As well, we&#8217;ve also remove the task of creating a new user to another method.  addNewUser does exactly that, it adds a new user and all it&#8217;s associate baggage.  I also took this time to better define what we needed, and what we didn&#8217;t.  You&#8217;ll see I&#8217;m pulling directly from $data now.  No need to pass $adminForm around.  Also, letFormHandleInput only requires a Zend_Validate_Interface object now, further decoupling ourselves.  I also took the liberty of renaming letFormHandleInput to validateNewUserDataInput.  This name better describes exactly what it does.  Granted, it also goes directly to creating a new user, but that&#8217;s something for another day.</p>
<p>Back on topic, lets take the next step, and refactor addNewUser in the same manner we&#8217;ve been.  Let&#8217;s see, we have another block of code.  This block sets up notifications.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> addNewUser <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$mAdmin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mAdmin</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createAdmin</span><span style="color: #009900;">&#40;</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'role'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setupNotificationsForUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Complexity 2</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_forward<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'confirm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> setupNotificationsForUser <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> Application_Model_DbTable_Users <span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">// Complexity 1</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'notify'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 2</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'notifyPhone'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'yes'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 3</span>
            <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_PhoneNotification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Complexity 4</span>
            <span style="color: #000088;">$notification</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_DbTable_Notification<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mAdmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$notification</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendNotification</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In this case, I just decided to encapsulate the entire notification code into it&#8217;s own method.  Here now, setupNotificaitonsForUser can be passed proper data, and from that, we can setup notifications for a users outside the creation of a new user.  This means when you write your code for editing a users notification preferences, you already have your notification setup.  Complexity is at 4, but that brings us to the low category of complexity.  There is one more thing we can do to simplify this code.  However, by this point, you should already see what we can do, and how it will simplify the code even further.</p>
<p>By extracting out the second if/else clause in setupNotificationsForUser, we abstract away the creation process.</p>
<p>All these methods can be applied to other forms of blocks of code.  Every case in a switch should probably have its own method.  Any loop should call out to a method.  At every level of complexity, you want to keep things as simple as possible.  When you read a block of code, it should be simple.  It should do something specific.  It should do is quickly, and it should do it well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/blocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comments</title>
		<link>http://www.jasonlotito.com/programming/comments/</link>
		<comments>http://www.jasonlotito.com/programming/comments/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 19:25:13 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lessons]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=114</guid>
		<description><![CDATA[I once posted  “comments indicate future refactoring.” I want to reaffirm my belief in that, and clearly explain. In order to fully appreciate this, let’s lay down some fundamental beliefs. These are beliefs I have built on experience, and later, I’ll apply them to more general fundamental beliefs like SOLID, but for now, these are [...]]]></description>
			<content:encoded><![CDATA[<p>I once posted  “<a href="http://www.jasonlotito.com/programming/comments-are-always-failures/.">comments indicate future refactoring</a>.” I want to reaffirm my belief in that, and clearly explain. In order to fully appreciate this, let’s lay down some fundamental beliefs. These are beliefs I have built on experience, and later, I’ll apply them to more general fundamental beliefs like <a href="http://en.wikipedia.org/wiki/Solid_(object-oriented_design)">SOLID</a>, but for now, these are simple rules I feel are good to follow.</p>
<ol>
<li>Do one thing.</li>
<li>Do it well.</li>
<li>Keep it concise.</li>
<li>Use only what is given.</li>
</ol>
<p>These aren’t overly complex rules. In fact, they are pretty simple. We are going to aim to meet these rules in a rather simple manner: using comments.</p>
<p>Now, let’s define what type of comments I’m referring to. I’m not talking about DocBlock comments. These are comments that allow for parsing by specific tools and allow us to generate documents about the code.</p>
<p>I’m also not referring to comments that repeat the code.</p>

<div class="wp_syntax"><div class="code"><pre class="php-brief" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// increment a by 1</span>
<span style="color: #0000ff;">$a</span><span style="color: #339933;">++;</span></pre></div></div>

<p>That is a poor comment, and should be removed. It does nothing except add to confusion.</p>
<p>The comments I’m referring to, however, are a bit more involved. But let’s start with an exercise.</p>
<p>Code should explain what it does. It doesn’t always do this. For example, what’s the purpose of the following code?</p>

<div class="wp_syntax"><div class="code"><pre class="php-brief" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>EMAIL_REGEX<span style="color: #339933;">,</span> <span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> 
     <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> 
     <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>USERNAME_REGEX<span style="color: #339933;">,</span> <span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>It’s easy to see what the code is doing, but not the purpose. If we add a comment, it will help matters.</p>

<div class="wp_syntax"><div class="code"><pre class="php-brief" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Check to see if the user input matches one of the searchable data sets</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>EMAIL_REGEX<span style="color: #339933;">,</span> <span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
     <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> 
     <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>USERNAME_REGEX<span style="color: #339933;">,</span> <span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hey! That helps. The code checks to see if the user input matches a pattern for searching a data set. Okay, that helps. But it’s still problematic. The comment isn’t clear. Is it referring to just the if-clause, or is it referring to the block inside the if-statement. Can you trust the comment? After all, as time progresses and code enters maintenance phase, things can and will change. Is it easy to read? It’s not difficult, but when you are read the comment, you then have to mentally associate that with the code. It takes extra effort. And for what? Saying something explicit.</p>
<p>Maybe it’s better that we ask a simple question: What is the comment trying to tell us?</p>
<p>If the input matches a searchable data set, run the blocked code.</p>
<p>Luckily, we can say that succinctly.</p>

<div class="wp_syntax"><div class="code"><pre class="php-brief" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inputMatchesSearchableData</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$input</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #000000; font-weight: bold;">function</span> inputMatchesSearchableData <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$input</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>EMAIL_REGEX<span style="color: #339933;">,</span> <span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
           <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
           <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>USERNAME_REGEX<span style="color: #339933;">,</span> <span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using the extra method refactoring recipe, we’ve made our code much, much easier to read. inputMatchesSearchableData says exactly what the method does, and in our original block of code, it reads much easier than the comment did. We’ve also refactored the code that checks to see if the input is searchable. We’ve split responsibility, and suddenly, our original method does one less thing.</p>
<p>Every time I use a comment to explain what code is doing, I always ask myself if it’s code that should be removed. If what the code is doing isn’t directly related to the parent method, the answer is most likely yes.</p>
<p>Often times, the comment itself is a good indicator of the name of the function. Extract the method out, and keep each method small, precise, and simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/comments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to get an invite to Forrst</title>
		<link>http://www.jasonlotito.com/programming/how-to-get-an-invite-to-forrst/</link>
		<comments>http://www.jasonlotito.com/programming/how-to-get-an-invite-to-forrst/#comments</comments>
		<pubDate>Fri, 20 May 2011 21:05:38 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Forrst]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=104</guid>
		<description><![CDATA[If you don&#8217;t know, Forrst is a invite-online developer and designer community started by Kyle Bragger (@kylebragger).  It&#8217;s a fairly friendly community, with lots of great feedback from a wide variety of people with all levels of skill.  However, one thing Forrst strives for is a community for all levels of people.  From beginners to [...]]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t know, <a title="WTB Chainsw, pst" href="http://www.forrst.com">Forrst</a> is a invite-online developer and designer community started by <a title="Apparently, Kyle writes code" href="http://kylewritescode.com/">Kyle Bragger</a> (<a title="He tweets." href="http://twitter.com/#!/kylebragger">@kylebragger</a>).  It&#8217;s a fairly friendly community, with lots of great feedback from a wide variety of people with all levels of skill.  However, one thing Forrst strives for is a community for all levels of people.  From beginners to experts, designers and developers, Windows/Mac/Linux users, all are invited.  At least, as long as you are willing to put forth the effort.</p>
<p>To get an invite, an existing member must invite you (and invites only come to those who contribute back to the community), or you have to be voted in after having applied.  To join, you go to the &#8220;<a title="I/Want/To/Join!" href="http://forrst.com/i/want/to/join">I want to join</a>&#8221; page.  On that page, it asks for a few pieces of information, including a  link to something you&#8217;ve done.  A github page, open source project, some fancy design.  Something to show that you are a developer or designer oriented person.</p>
<p>The second part, however, is the most important part.  The Forrst join page asks you to provide sample feedback for a random post.</p>
<h2>Provide Good Feedback</h2>
<p>Listen.  Forrst is all about communication.  Communication between all types of people.  This means feedback is important.  Poor feedback is bad.  We don&#8217;t like it.  However, good feedback is cherished, loved, and rewarded.  So, when Forrst asks you to provide feedback for a post on the Join page, realise that real Forrst members are going to review this feedback.  It takes 3 votes to get you in, and chances of finding 3 people to agree that &#8220;That looks awesome&#8221; is good feedback is slim.</p>
<p>Let me try to make this clear: the feedback you provide on the join page indicates to us the type of feedback you&#8217;ll provide once a member.  If the feedback provided is essentially worthless, then making you a member won&#8217;t help the community.  A community built up around feedback.  What this means is that you should put some effort into your feedback.</p>
<p>So, here are some tips on how to write good feedback.</p>
<h3>1. Pick a post you can answer</h3>
<p>Forrst provides a nice little button that you can click on.  It will reload the post to provide feedback for.  Don&#8217;t just settle for the first post you see.  Instead, find something you are actually interested in commenting on.  You aren&#8217;t expected to comment on everything that gets posted to Forrst.  Not everyone that gets posted to Forrst is going to get good comments.  So, click a way and find something that will allow you to provide good feedback.</p>
<h3>2. Why, not what</h3>
<p>&#8220;That looks awesome!&#8221;</p>
<p>The person you are providing feedback too is probably not going to see your post, so you aren&#8217;t going to win brownie points for complimenting work.  Though, if the work is good, providing compliments is fine.</p>
<p>However, in this case, &#8220;That looks awesome&#8221; is fairly useless.  It just explains what you feel.  It doesn&#8217;t explain &#8220;why&#8221; it looks awesome.  Take a few minutes to go into detail about what you like and why.  Even if you can&#8217;t express it clearly, try.  Remember, effort is key here.  Explaining why you like something takes a bit more than just saying you like it, but it provides a lot more value.  Others can learn from it, the original poster can focus on those areas.  Who knows, in explaining why you like something, you might learn a little about yourself.</p>
<h3>3. We need Effortmore, not Effortless</h3>
<p>Okay, that was bad.  But, the point is, your feedback should take a bit of effort.  It is, after all, an application to join.  And regular members like me are going to read this feedback.  I&#8217;ll by frank, if I see a 10 word comment, I don&#8217;t bother looking at anything else and move on to the next person.  Maybe it&#8217;s unfair, but I figure it&#8217;s the same amount of effort put into the comment.</p>
<p>It doesn&#8217;t take long.  A couple minutes at most.  Ask yourself after you&#8217;ve finished the comment if it&#8217;s something you&#8217;d like to receive.  Does it actually share a unique idea.  Does it express an opinion? Is that opinion explained?</p>
<h2>The Elite</h2>
<p>Forrst isn&#8217;t about being among the elite.  It&#8217;s about being among people who are just like you.  Frankly, if you feel applying to the site and actually putting effort into the feedback is too much work, then Forrst probably won&#8217;t benefit you.  That&#8217;s fine.  However, if you truly want to join, then maybe this little piece of advice will help you avoid a pitfall you might not think about.  From the Forrst join page:</p>
<blockquote><p>Since Forrst is a very critique- and discussion-oriented community, we  ask prospective users to leave sample feedback on a post from one of our  members.</p></blockquote>
<p>We review this.  The members.  And I imagine those of us that review these potential members are very interested in keeping Forrst&#8217;s quality high.  We will be picky.  After all, we want to log into Forrst and enjoy what we read there.  We want to be part of a community of people that we&#8217;d go out drinking with.  A little bit of effort will go a long way.</p>
<p>Join us!  We aren&#8217;t elitist.  We just want you to know that this is the place for you, and we want to know that you&#8217;ll get a lot out of this community.  But only as long as you are willing to give.</p>
<p>Of course, as mentioned, the other way to join is to be invited by an existing member with invites.  Like myself.  In that case, beer is ALWAYS a welcomed bribe.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/how-to-get-an-invite-to-forrst/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>ssh connection sharing</title>
		<link>http://www.jasonlotito.com/programming/ssh-connection-sharing/</link>
		<comments>http://www.jasonlotito.com/programming/ssh-connection-sharing/#comments</comments>
		<pubDate>Fri, 20 May 2011 12:50:21 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=102</guid>
		<description><![CDATA[ControlMaster and ControlPath are the ssh config options you are looking to use here.  You&#8217;ll want to add them to your ssh config file, which is located in ~/.ssh/config You simply want to add the following lines to your config file. Host * ControlMaster auto ControlPath ~/.ssh/control_%r@%h:%p The Host command tells ssh that everything that [...]]]></description>
			<content:encoded><![CDATA[<p>ControlMaster and ControlPath are the ssh config options you are looking to use here.  You&#8217;ll want to add them to your ssh config file, which is located in</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>config</pre></div></div>

<p>You simply want to add the following lines to your config file.</p>

<div class="wp_syntax"><div class="code"><pre class="teraterm" style="font-family:monospace;">Host <span style="color: #ff00ff; font-weight: bold;">*</span>
ControlMaster auto
ControlPath <span style="color: #ff00ff; font-weight: bold;">~/</span>.ssh<span style="color: #ff00ff; font-weight: bold;">/</span>control_<span style="color: #ff00ff; font-weight: bold;">%</span>r@<span style="color: #ff00ff; font-weight: bold;">%</span>h:<span style="color: #ff00ff; font-weight: bold;">%</span>p</pre></div></div>

<p>The Host command tells ssh that everything that follows are rules for whatever host is defined.  In this case, the asterisk tells ssh that all hosts should use the conditions present.  ControlMaster enables the sharing of multiple sessions over a single network connection.  ControlPath defines the location the socket connections should be located.  In this case, we are placing them in ~/.ssh/.  %r is token for remote user login, %h is for the host, and %p is the port.  You&#8217;ll basically have files that look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="teraterm" style="font-family:monospace;"><span style="color: #ff00ff; font-weight: bold;">~/</span>.ssh<span style="color: #ff00ff; font-weight: bold;">/</span>control_username@example.com:<span style="color: #008080;">22</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/ssh-connection-sharing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Normalisation of data</title>
		<link>http://www.jasonlotito.com/programming/dont-confuse-validation-with-filtering/</link>
		<comments>http://www.jasonlotito.com/programming/dont-confuse-validation-with-filtering/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 13:00:19 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[assumptions]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=97</guid>
		<description><![CDATA[This post used to be called Don&#8217;t confuse validation with filtering.  This was meant to be about normalising data, but at the time, filtering was at the forefront of my mine due to work that did some of the things discussed here.  Filtering, validation, and normalising are all essential parts of handling data.  Finally, I [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post used to be called Don&#8217;t confuse validation with filtering.  This was meant to be about normalising data, but at the time, filtering was at the forefront of my mine due to work that did some of the things discussed here.  Filtering, validation, and normalising are all essential parts of handling data.  Finally, I want to stress that while normalisation is good, it&#8217;s not easy.  Making assumptions is fine as long as you make educated assumptions.  The reason you don&#8217;t see normalisation as much as you do is because it&#8217;s difficult to get right.  I&#8217;ve attempted to be careful in my examples, and have suggested seeking confirmation from the user before continuing.</em></p>
<p><a title="Amatuer chef" href="http://www.brandonsavage.net">Brandon Savage</a> posted an interesting article today about how <a title="See how I made the title of his article a part of my sentence.  I am 3-l33t" href="http://www.brandonsavage.net/validation-blind-spots-hurt-real-users/">validation blind spots hurt real people</a>.  He gave four succinct rules.</p>
<ol>
<li>Accept valid data in any form provided by the user.</li>
<li>Where possible, use well-developed validation libraries.</li>
<li>Do not place artificial limits on valid data.</li>
<li>Do place valid limits on specific data.</li>
</ol>
<p>All good advice.  However, it misses out on filtering.  You see, filtering is different from validation.  Filtering is the art of taking bad data and turning it into good data.  It might sound difficult, but it&#8217;s actually easier than you think, and will ease the pain in using your software.  Let&#8217;s see some examples.</p>
<h2>Usernames</h2>
<p>Passwords are secret.  Usernames, however, are usually not.  Even if they are private, like an email address, they are usually not the secure part of the application.  Couple this with mobile devices that auto-capitalize, if you&#8217;re requiring case-sensitive usernames, you are making things more difficult then they should be for your users.  It&#8217;s a pain having to go back, lower case a single letter at the beginning, and then retype a fairly long password just to log in because your site decided that &#8216;Jasonlotito&#8217; and &#8216;jasonlotito&#8217; are not the same user.</p>
<p>At the same time, if I do enter my email address, and you ask for a username, then just go with what I give you.  I presume that their is only one email address per account.  If I&#8217;ve forgotten my username for your site, but I know I have an email address tied to an account, just let me use that.  You&#8217;ve seen I&#8217;ve entered my email address, you can then look up by email address, get my username from there, and proceed to validate.</p>
<h2>Credit cards</h2>
<p>Credit card numbers follow simple rules.  If I enter my credit card number, you know what card I&#8217;m using, so asking me is silly.  You don&#8217;t even need to know the full number on the card to figure out the card type.  Once I&#8217;ve entered enough numbers in for you to know, simply change the pull down to the correct type, and change the icon.  It will give visual feedback that yes, we know what card you are using.  Their is no reason not to do this.</p>
<p>Also, if I put in dashes, or dots, or spaces, or whatever, you can deal with it.  Ignore them.  Simply use the numbers.  You don&#8217;t need to exclude characters, simply white list numbers.</p>
<p>While we are here, expiration dates are easy, and yet, somehow, people get them wrong.  Maybe I&#8217;m missing out on some details here, but every credit card I&#8217;ve ever seen lists expiration dates as MM/YY.  This is a sign that maybe your forms should follow suit.  This means your month should be &#8217;03&#8242;, not March (03).  Just &#8217;3&#8242; would suffice.  The year should be equally clear: &#8217;15&#8242;, not &#8217;2015&#8242;.  I promise you, no one is assuming 1915, or even 2115.</p>
<h2>Phone numbers</h2>
<p>Filtering phone numbers is hard.  I know.  I&#8217;ve had to do it.  The system needed a filtered number because we had to call back in real time from an automated system located in the US.  This mean we needed a properly formatted number that could be dialed.  This isn&#8217;t easy, because when you ask for a phone number, people are going to start adding in country codes (but they might).  My suggestion is, unless you are having to do some automated system is to simple not filter numbers unless you can be sure, 100%, of what you are doing.  Even then, you probably don&#8217;t need to do it.</p>
<p>Simply ask the user for their phone number, and store whatever they put.  If you need to call them, you can find their country code, or area code if you need to.</p>
<h2>Email addresses</h2>
<p>Email addresses can be tricky.  You want to make sure they enter in the right email address. A simple mistake can cause problems.  I&#8217;ve, on occasion, type in jasonlottio instead of jasonlotito, and the problem was painful to resolve.  On the username side, you can&#8217;t do much.  However, after you hit the ampersand, you can start some simple checks.  If someone types in gmail or hotmail or yahoo and leaves off the .com, you can make assumptions.  You can validate these assumptions with the user on the next page.  Transposing letters (htomail) might be something you should look out for, too.  Compile a list of common email providers, and then check how close they are to the users non-common provider name.  If they are close, verify with the user if they didn&#8217;t misspell the name.</p>
<h2>Assumption</h2>
<p>The key to all these filtering techniques is assumption.  It&#8217;s not always perfect, but it&#8217;s better than failing.  After all, if I&#8217;m in Canada, and my phone number is a 10 digit phone number, you can make the assumption that to call me long distance, you&#8217;ll need to add a 1.  You can assume that if my credit card number starts with 4111, I&#8217;m using a Visa.  Their are lots of assumptions you can make that will make things easier for the user.  Go ahead.  It&#8217;s okay.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/dont-confuse-validation-with-filtering/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Profiling a PHP Application</title>
		<link>http://www.jasonlotito.com/programming/profiling-a-php-application/</link>
		<comments>http://www.jasonlotito.com/programming/profiling-a-php-application/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 13:08:36 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[siege]]></category>
		<category><![CDATA[xdebug]]></category>
		<category><![CDATA[xhgui]]></category>
		<category><![CDATA[xhprof]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=96</guid>
		<description><![CDATA[Eric Hogue, a fellow PHP programmer up here in Montreal has written about Profiling a PHP Application.  It&#8217;s a solid step-by-step setup of XHProf, XHGui, Xdebug for profiling with kcachegrind, and siege.  Take a look!]]></description>
			<content:encoded><![CDATA[<p>Eric Hogue, a fellow PHP programmer up here in Montreal has written about <a title="He has a MONSTER LAPTOP" href="http://erichogue.ca/2011/03/30/profiling-a-php-application/">Profiling a PHP Application</a>.  It&#8217;s a solid step-by-step setup of XHProf, XHGui, Xdebug for profiling with kcachegrind, and siege.  Take a look!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/programming/profiling-a-php-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.jasonlotito.com @ 2012-05-20 00:11:51 -->
