<?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</title>
	<atom:link href="http://www.jasonlotito.com/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>Simple, or why companies reinvent solutions</title>
		<link>http://www.jasonlotito.com/advice/why-companies-reinvent-solutions/</link>
		<comments>http://www.jasonlotito.com/advice/why-companies-reinvent-solutions/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 16:38:42 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Advice]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=144</guid>
		<description><![CDATA[A designer knows that he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away &#8211; Antoine de St-Expurey HipChat At work, we are currently experimenting with HipChat.  HipChat, if you aren&#8217;t aware, is chat client like IRC.  In fact, the similarities are great.  You [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>A designer knows that he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away &#8211; <em>Antoine de St-Expurey</em></p></blockquote>
<h2>HipChat</h2>
<p>At work, we are currently experimenting with <a href="http://www.hipchat.com">HipChat</a>.  HipChat, if you aren&#8217;t aware, is chat client like IRC.  In fact, the similarities are great.  You log in, you join a chat room, you can talk to people, it keeps a history.  Even there client reminds you of an IRC client.  So the question becomes, in a world with IRC freely available and at your disposal, with MSN and ICQ and various other chat applications available, how could HipChat hope to make an entrance.</p>
<p>Through simplicity.</p>
<p>How does HipChat work? Well, you log into your account, and then you can start chatting away in whatever room you want.  Its contrary to the way IRC works.  With IRC, you have to decide on a server.  Actually, before that, you have to figure out which IRC client you want.  And, of course, you have to learn to use the application.  You probably still have to set up an account on the IRC server (unless you don&#8217;t mind not &#8220;owning&#8221; your name).  Doing this means learning how nickserv works on that network.  Then you have to learn how to join a chatroom&#8230; well, it&#8217;s annoying.</p>
<p>How does your car work?</p>
<p>Specifically? How do all the computers in your car play a part?  Do you know?  Does it affect your ability to drive?</p>
<p>HipChat works by hiding the complexity.  Driving works because it hides the complexity of the engine and everything that makes it work.  Computers do this as well.  Considering all someone has to do to get a computer and OS up and running is turning the power on, it&#8217;s fairly simple from a user perspective.</p>
<p>So, HipChat is successful because it has removed everything unnecessary from the process of group chat.  It doesn&#8217;t even require a local client.  You can log in to a website like you do everywhere else and start chatting.  No download required.</p>
<h2>One Button</h2>
<p>Oddly enough, this is something Microsoft discovered and pioneered before Apple&#8217;s iPhone.  Once you started your computer up, there was a button: Start. This made further use of the computer simple.  It was the single starting point, and it even said &#8220;Start&#8221; so you had a good understanding that whenever you wanted to do something with Windows, you&#8217;d Start there.  This of course led to the joke that if you wanted to shut down, you&#8217;d want to Start the shutdown process with the Start button.</p>
<p>Regardless, both the Start button and the iPhone/iPad/iPod&#8217;s single home button have become popular.  The idea here is removing all unnecessary buttons from the device or system.  The less you have to do, the easier the action becomes.</p>
<p>Less is more, as they say.</p>
<p>Apple took this further with their Magic Mouse.  I&#8217;ll admit, I love the little thing.  The combination of a mouse and a touch interface makes it a pleasure to use.  The Magic Touch Pad is also another nice addition.  In this case, there really is no button on the device (that the actual device is an entire button is beside the point).  It&#8217;s just flat.</p>
<h2>Keyboards</h2>
<p>Keyboards are, in some respects, the anti-thesis. It has many different buttons, and modifiers that do different things. Luckily, for the most part, the buttons follow simple rules. The layout is in such a way that makes touch typing incredibly easy.  That touch typing is a skill that&#8217;s been around since before computers makes it an easy mechanism to use.  After all, why not build on top of what people already know.  We do this with most calendar applications, which are laid out in a way that is familiar to most everyone.</p>
<p>So, even with all those buttons, a keyboard is still fairly simple because it&#8217;s not necessarily attached to the keyboard.  It helps that the letters are printed on the keys in a standard order.  So, because of these simple rules and the fact that their is a history attached to the use of the keyboard, it&#8217;s an exception to the rule.  Somewhat.</p>
<p>Virtual keyboards, while following the same layout in most cases, generally don&#8217;t share some of the common properties with physical keyboards.  After all, one of the principles of touch typing is actually touching the keys, returning to the home row, and this is facilitated by the little bumps on the &#8216;F&#8217; and &#8216;J&#8217; key.  With these, we can easily return to our starting position (And, if you&#8217;ll note, on a numerical key pad, you have the same bump on the 5 key).  Virtual keyboards don&#8217;t actually have this (despite the &#8220;bumps&#8221; appearing drawn on.</p>
<p>I&#8217;m actually surprised by the number of people that don&#8217;t realize what these little bumps are for.</p>
<p>Regardless, people like <a href="http://37signals.com/svn/posts/1240-swype-keyboard-for-touchscreens">Swype</a> are looking at ways to improve the virtual keyboard experience.  In Swypes case, while the virtual buttons remain, the actual number of presses are reduced.  You just move the pointer, or your finger, over the keyboard.  This ceases to be a keyboard, and more a drawing tool.</p>
<h2>What does this all mean?</h2>
<p>I don&#8217;t think it&#8217;s surprising that simple works best.  Simple doesn&#8217;t just mean one button (HipChat isn&#8217;t just one button, and keyboards have many), efficiently usable by everyone (keyboards work best when touch typing, but other people can get by pecking along with their fingers), or copying what already exists (after all, the single button approach for the iPhone was completely different from any other phone at the time).</p>
<p>Simple is an odd combination of all these things.  More importantly, it&#8217;s about understanding what actually makes things difficult.  Having a common rule for keyboards make them easy to use.  The iPhone copied this style.  All those little apps do something, and they all work the same way.  HipChat works just like any other site.  It&#8217;s akin to email, and one of the first things people learn to use is email.  The difficulty in using a smartphone wasn&#8217;t the nature of a mobile computing device.  It was simply that applications suddenly had different rules, keyboards had to be modified to account for the size, and they tried to carry over the windows motif from the desktop to the mobile device.  Function keys, modifiers, special keys for different things.</p>
<p>And this leads us to the point: removing that difficulty is something people will pay for.  Keyboards haven&#8217;t changed much because the difficult is displayed by the keyboard literally spelling out what each button does.  The challenge now is how to solve the problem of typing on a smaller device.  For the vast majority of users, this isn&#8217;t an issue.  They don&#8217;t touch type anyways.  However, this doesn&#8217;t mean it&#8217;s the most efficient way for them.  Swype has one solution.  Apple has another solution: Siri.</p>
<p>HipChat tackled the chat arena.  It was a market that wasn&#8217;t exciting from my perspective, and felt saturated.  After all, when you thought about chat, you thought about IRC, and the various clients out there competing for users.  HipChat came in and made a business out of it.</p>
<p>This is important.  When we look for future solutions, one thing we often ignore are the current solutions we use.  Is it really as easy as it can be.  Could we improve on it? What are the painful parts?</p>
<p>It&#8217;s not easy to do this.  Google, in some ways, has tried this in the past with things like Buzz and Wave.  Twitter is an example of this same thing: a problem existing, and while solutions existing, they weren&#8217;t simplified.  Facebook is the same thing (in some regards).</p>
<p>This all goes back to the Unix mantra: do one thing and do it well.</p>
<p>When I want to run a chat room, I want to run a chat room.  I don&#8217;t want to setup a server, or manually set up users.  I don&#8217;t want to manage nickserv, or anything else.  I simply want to do something, not manage or setup.  Lots of companies understand this.  Amazon&#8217;s cloud hosting services, Netflix and their video streaming, and even Amazon with it&#8217;s 1 Click purchasing.</p>
<p>So, don&#8217;t be scared to solve something that&#8217;s already been solved.  Countless companies have built empires on top of &#8220;simply&#8221; removing things from existing solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/advice/why-companies-reinvent-solutions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ideas of March 2012</title>
		<link>http://www.jasonlotito.com/news/ideas-of-march-2012/</link>
		<comments>http://www.jasonlotito.com/news/ideas-of-march-2012/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 13:02:19 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[ideas of march]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=142</guid>
		<description><![CDATA[That time again.  I wrote something last year, covering the ideas of march.  Last year, bloggers came out and shared their ideas, and I imagine this year will be just as fun.  So, once again, I&#8217;d like to share my thoughts on blogs.  This year, I&#8217;d like to take on the point of view of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.phpdeveloper.org/?p=440">That time again.</a>  I <a title="Ideas of March" href="http://www.jasonlotito.com/programming/ideas-of-march/">wrote something last year</a>, covering the ideas of march.  Last year, <a href="http://phpdeveloper.org/news/16047">bloggers came out and shared their ideas</a>, and I imagine <a href="http://phpdeveloper.org/news/17678">this year will be just as fun</a>.  So, once again, I&#8217;d like to share my thoughts on blogs.  This year, I&#8217;d like to take on the point of view of a reader as opposed to a writer of blogs.  After all, far more people consume blogs then actually write them.</p>
<h2>Having trouble keeping up? Share!</h2>
<p>It&#8217;s hard.  Even with a feed reader, it&#8217;s hard to keep up with everything that is going on.  In the PHP world, I can head on over to good-ole <a href="http://www.phpdeveloper.org">PHPDeveloper</a> to find out what is going on in the PHP world, but these days, it&#8217;s more than just PHP.  I can of course subscribe to my favorite blogs, but as I said, there is far more out there to read than I have time to enjoy.  Twitter helps some, as I can follow a link from twitter from someone I followed, but even if they post something, I might miss it in the ever moving Twitter stream.</p>
<p>What really helps is when people share.  Between Reddit, HackerNews, people retweeting things they&#8217;ve found, DZone, and, of course, PHPDeveloper, there are numerous ways to share.  If you are a reader, and you&#8217;ve read something, share it!  A tweet about an article might only bring in a few people, but those are people that might not otherwise have found the article.  Share the article!  Help curate, and get it out there.</p>
<p>That&#8217;s something I&#8217;m trying to commit to.  And while sharing and those &#8216;social media&#8217; buttons on articles might seem annoying, they actually serve a purpose to remind us how we get our information.  Just because someone has tweeted an article doesn&#8217;t mean his followers will see it.  Too often I find myself following a retweet because I missed the original.</p>
<p>And don&#8217;t be afraid to submit to sites like Reddit or HackerNews.  Both are wonderful resources.</p>
<h2>Your voice matters</h2>
<p>Even if it&#8217;s a simple thank you.  You don&#8217;t need to spend much time, but every little bit helps.  If you read something, and enjoy it, let the author know.  Nothing makes us happier than when someone takes the time to actually respond.  Not everyone will comment, but getting just one comment usually spawns a few others.</p>
<p>If you are nervous about what you have to say, you can also always send an email.  I think it&#8217;s fair to say that unless they say otherwise, <a href="http://www.kalzumeus.com/standing-invitation/">bloggers have a standing invitation to contact them</a>.  We&#8217;re geeks just like you, and we&#8217;re writing about stuff you&#8217;re clearly interested in as well, so we already have at least one thing in common.</p>
<h2>Read, enjoy</h2>
<p>Most of all, and most important, is to read the articles.  Spend time and enjoy them.  All too often we get sucked into the 140-characters or less, StackOverflow-style short answers that let us get in and get out.  I find I enjoy blogs so much more when I just allow myself to read and focus.  I don&#8217;t just skim the headlines looking for the answer to a solution.  I sit back with a cup of coffee, and spend a little bit of time reading.  I don&#8217;t rush through.  I don&#8217;t start an internal debate with what&#8217;s being said.  I just read.  I just enjoy.</p>
<p>Too often we allow ourselves to become masters of multitasking.  And there are times this is good.  But we need to learn to shut this off.  Blogs give us this chance.  If it&#8217;s worth reading, it&#8217;s worth focusing on.</p>
<h1>To Bloggers</h1>
<p>This is a message to bloggers.  Don&#8217;t wait for the perfect article.  Just write.  Share stories of projects.  You don&#8217;t need to provide a tutorial or novel solution every time you write.  Simply recounting what you did, even if everything you did is what is normally done, can be just as revealing, just as interesting.  Just write.  Share your stories.  People want to read, and not every article need contain examples of code.</p>
<p>Share your mistakes, share your accomplishments.</p>
<p>I should take my own advice.  Recently we launched <a href="http://locals.myyearbook.com">Locals at myYearbook</a>.  The web version was my baby.  It&#8217;s an awesome product, and combined with the mobile version, makes for something an interesting story on the challenges we faced and how it works.  I haven&#8217;t written anything on it because, frankly, I worked on it so much that I can&#8217;t see the forest for the trees.  It&#8217;s completed, and remembering all the interesting details would take time.  And, wrongly, I feel that to do it justice, I would need to go over everything.</p>
<h1>Ideas of March</h1>
<p>Finally, here are some ideas I am working on for future articles.</p>
<ul>
<li>Basics of tmux, getting strated with the terminal multiplexer</li>
<li>vim maps based on file types</li>
<li>An approach for a DIC for PHP 5.4</li>
<li>And just for kicks, writing a PHP app without conditions or loops (not recommended for sanity!)</li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/news/ideas-of-march-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>2012 &#8211; Reflection and Resolutions</title>
		<link>http://www.jasonlotito.com/life/2012-reflection-and-resolutions/</link>
		<comments>http://www.jasonlotito.com/life/2012-reflection-and-resolutions/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 04:14:09 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[resolutions]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=131</guid>
		<description><![CDATA[The Story Thus Far It&#8217;s safe to say 2011 was a big year for me.  In 2001, I moved to Montreal, Quebec to be wife my now-wife, Piera.  I got my first real job programming PHP for an online casino company.  After the company went bankrupt, I started my own company with 2 friends.  A [...]]]></description>
			<content:encoded><![CDATA[<h2>The Story Thus Far</h2>
<p>It&#8217;s safe to say 2011 was a big year for me.  In 2001, I moved to Montreal, Quebec to be wife my now-wife, <a href="http://www.bypiera.com">Piera</a>.  I got my first real job programming PHP for an online casino company.  After the company went bankrupt, I started my own company with 2 friends.  A credit card processing company called myVirtualCard.  This lasted for a good number of years, but we finally had to shut down.  Meanwhile, in 2007, I&#8217;d married Piera.  In 2009, I started work again at Gamma Entertainment handling their credit card processing department.  In 2008, we had our first son, Joseph Michael Robert Lotito.  In 2010, we had our second son, Julian Alfonso Lotito.  We also learned that summer that Joseph might be autistic.  We then spent the next year fighting with the Quebec medical establishment to get whatever helped we could for him (We got no support).  In the sprint of 2011, we&#8217;d had enough, and I made up my mind to move back to the US.  After all, we had that option.  In a few short months, I&#8217;d interviewed, got hired, found a place, made several flights all over the country, and in August of 2011, moved to Telford, PA to begin work at <a href="http://www.myyearbook.com">myYearbook.com</a>.</p>
<h2>Back in the U.S.A.</h2>
<p>So, here I am, back home at the start of 2012.  If you&#8217;d asked me a year ago where I would be, here is not where I&#8217;d imagine.  But I&#8217;m glad we made the move.  As it turns out, Joseph does have Autism.  Had we waited a bit longer, he would be in much worse shape.  As it is, it will still require intense ABA therapy.  There is also some worry Julian might be autistic as well.  I believe the number was 90+ out of 150 autistic children are boys, and siblings of autistic children are 70% likely to be autistic as well.  Suffice it to say, we are keeping a close eye on him.  Luckily, he seems, if anything, to simply be mildly developmentally delayed.</p>
<p>The treatment we&#8217;ve received here in Bucks county in Pennsylvania has been outstanding.  Seeing the difference in care, I could never willingly return my children to Quebec.</p>
<h2>2011</h2>
<p>So for me, 2011 started with me in the US, on a trip to visit family, and it ended with me living here.  It&#8217;s rather humorous when you think about it, and the road it took to get here.  Most of 2011 can be summed up with this: &#8220;how did I manage to control this chaos.&#8221;  Between work, worrying about my son, finding a job, moving, getting acquainted with that new job, getting the wife and kids accustomed to a new country, and everything that entails: it was exhausting.  I had very little time for myself, it feels.  I put aside most of my hobbies since moving.  No painting, no gaming, no writing, very little exploratory programming (though it&#8217;s started getting better).</p>
<p>2011 was about the family.</p>
<h2>2012</h2>
<p>This year, with the family settled, I need to look at bettering myself.  Joseph is getting the help he needs to, I&#8217;m employed at a wonderful company and work with a lot of smart people, and I have immense opportunity to grow as a developer.</p>
<p>Resolutions are a traditional way to start the new year.  The problem is, this year started without me having thought of one.  I just simply forgot.  At my brother&#8217;s new years party, my sister-in-law handed me a card, and told me to fill it out with my resolutions.  She&#8217;d mail it to me at the end of the year.  This sounded like a great idea, except I had no idea what to write.  I simply hadn&#8217;t though of a resolution.</p>
<p>Since then, I&#8217;ve been pondering what I want to do.  There are the grand goals, the cliche goals: get in shape, make money, be happy, win the lottery.  I wanted something more concrete.  I want to change my life in small but meaningful ways.  Over the course of the year, I want to focus on one aspect, and carry over the habits I can.  My resolution isn&#8217;t to change any one thing.  Rather, my resolution is to better myself.</p>
<h2>January &#8211; Freedom</h2>
<p>I feel like I&#8217;ve already wasted half the month in coming up with what I want to do.  So, January is the month I&#8217;m giving to myself.  I&#8217;m not committing to anything in January.  No pressure.  I&#8217;m allowing myself to prepare for the coming months.</p>
<h2>February &#8211; Hobby</h2>
<p>In February, I want to start a hobby that isn&#8217;t related to programming or computers.  In fact, it shouldn&#8217;t involve computers at all.  This will most likely involve either wood working or miniature painting.  The former is possible with a garage, but I have no equipment.  The later I have everything for, including ample space.  However, the former involves me getting off my butt, which is something I should address at some point.</p>
<h2>March &#8211; Getting Healthy</h2>
<p>I really shouldn&#8217;t wait so long, but by March, I want to be on the road to health.  More importantly, this is the month when I want to start really learning more about getting healthy.  Eating right, exercising and enjoying it, and taking care of myself.  I&#8217;ve done this before, and by March, I was doing really well.  I can do this.</p>
<h2>April &#8211; Ludum Dare</h2>
<p>I want to write a game.  In my spare time, I&#8217;ve been playing with an event driven gaming framework in JavaScript.  It&#8217;s been a lot of fun, and come April, I want to take part in <a href="http://ludumdare.com/">Ludum Dare</a> 23.  I&#8217;ll probably take a couple days off before and after, and make sure the wife knows what I&#8217;m doing.  It will require training and practice before hand, but by April I should be ready to go.  This means, of course, having completed a game or two before hand.</p>
<h2>May &#8211; Open Source All the Things!</h2>
<p>I haven&#8217;t contributed to an open source project in a long time.  The goal here would be to contribute some libraries and code I&#8217;ve written (will write).  It&#8217;s not much, but I need to spend time putting them together and releasing them, or at least finishing them up.  I&#8217;d always want to release the JavaScript Game Event Engine I&#8217;m working on (for use with Ludum Dare).  May would be set aside for getting that up and released.</p>
<h2>June &#8211; iOS App</h2>
<p>By this time, I will have wanted to release an iOS App.  I have one in the works.  It&#8217;s a simple app, but great for learning the system, and something I would use (always a plus).  It&#8217;s also an app that isn&#8217;t covered well in the app store.  So, by June, I&#8217;ll want to have released it.</p>
<h2>July &#8211; Learn a new language</h2>
<p>I&#8217;m not really sure which language, but by this time, I&#8217;d want to try to learn something new.  I was introduced to Erlang back in August of 2011, and enjoyed it.  Maybe putting some more time into it would be fun.  Regardless, July should involve learning a new language.  The goal will be to write a simple HTTP server, I think.  That&#8217;s always fun, and a great way to learn the system, it&#8217;s libraries, and has a fairly practical value.</p>
<h2>August &#8211; Node</h2>
<p>If myYearbook holds Node the same time this year as the previous year, we&#8217;ll have it in August.  Node is the company developer retreat.  3-days away at some remote location having our own mini-conference.  2011 was fun, despite the storm and lack of power.  Besides of Node, however, I&#8217;ll also have time to continue learning that new language.</p>
<h2>September &#8211; Talk More</h2>
<p>I want to push myself to start giving more talks.  I&#8217;m already giving a talk this coming friday at a PHP meet up group, but it&#8217;s a small affair.  By September, I want to be in a position to give more talks, and really start pushing it.  I have no issue standing in front of people and talking (I actually enjoy it).  My issue has always been self-confidence.  I mean, do people really want to hear me give a talk?  By September, I want to make that a yes.  This will involve blogging more, writing more, contributing more of course.</p>
<h2>October &#8211; Master Smaller Tools</h2>
<p>There are a lot of tools out there in the Unix world, and I simply don&#8217;t know them all.  While over the course of the year, I&#8217;m sure I&#8217;ll learn many new things, I want to allow myself time in October to simply learning these smaller tools.  Maybe coming to better understand writing vim plugins, or getting a better handle on make or bash.  The goal here isn&#8217;t to learn anything in particular, but rather focused attention on learning these tools.</p>
<h2>November &#8211; NANOWRIMO</h2>
<p><a href="http://www.nanowrimo.org/">NANOWRIMO</a>.  Nuff said.</p>
<h2>December &#8211; Reflection</h2>
<p>With the holidays approaching, now is the time for reflection, relaxation, and discovering if I&#8217;ve achieved this rather grand list of things I want to accomplish.</p>
<p>Is it doable? Yes.  Can I do it? Sure.  Will I? Who knows?   So much can happen, but having a plan helps out.  I&#8217;d like to think that every every month now I have something to look forward to, something to keep me on track.  Many small goals that will, I imagine, improve myself.</p>
<p>I hope I am as resolved as I will need to be.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/life/2012-reflection-and-resolutions/feed/</wfw:commentRss>
		<slash:comments>0</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>I picked the destination</title>
		<link>http://www.jasonlotito.com/life/pick-a-destination/</link>
		<comments>http://www.jasonlotito.com/life/pick-a-destination/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 16:08:29 +0000</pubDate>
		<dc:creator>Jason Lotito</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[moving]]></category>

		<guid isPermaLink="false">http://www.jasonlotito.com/?p=111</guid>
		<description><![CDATA[In August, I moved from Montreal after almost 10 years to the small town of Telford, Pennsylvania, a little less than an hour north of Philadelphia.  It&#8217;s a beautiful little town.  Our home is nestled in a small development filled with couples with kids that are around the same age as Joseph and Julian.  Lots [...]]]></description>
			<content:encoded><![CDATA[<p>In August, I moved from Montreal after almost 10 years to the small town of Telford, Pennsylvania, a little less than an hour north of Philadelphia.  It&#8217;s a beautiful little town.  Our home is nestled in a small development filled with couples with kids that are around the same age as Joseph and Julian.  Lots of prospective baby sitters in the next few years as well.  Within 10 minutes, we have pretty much everything we could be looking for in terms of local shopping, as well as several wonderful local pizza joints.</p>
<p>It&#8217;s a wonderful place.</p>
<p>I work at <a title="No, it's no Classmates.com" href="http://www.myyearbook.com">myYearbook.com</a>, a wonderful social discovery site, in New Hope, Pennsylvania.  It&#8217;s a good hour commute in the morning, and a bit longer on the way back home.  The people that work there are incredibly smart, highly motivated, and know their field extremely well.  It&#8217;s wonderful working with them.  Every day is a learning experience.  It&#8217;s a wonderful place to grow and expand professionally.  Between the number of published authors and open source contributors, as well different technologies we are using, it&#8217;s hard for anyone not be learning something new every day.</p>
<p>But why did I move?</p>
<p>Joseph.  Born in November of 2008, he was pre-diagnosed with autism in the summer of 2010 at one-and-a-half years old.  For a year, my wife and I fought he Quebec government to get help for him.  Between both private and public care, he was constantly denied treatment.  Coming to Pennsylvania, the public care he receives as a child with autism is light years beyond what Quebec had to offer.  Before getting here, we&#8217;d gotten appointments.  Within the first month, he&#8217;d received more help for autism then he&#8217;d ever gotten in Quebec.  It was at the same time wonderful and sickening.  To know he was getting help, and to realize how many people are still suffering in Quebec.</p>
<p>My greatest hack was moving to Pennsylvania and getting my son help.  It was making that decision to move.  When you&#8217;ve lived in a place for 10 years, it&#8217;s hard to disrupt your life.  But one afternoon, after another major setback, I told my wife that I was going to move us to the US and get Joseph care.  It wasn&#8217;t a discussion.  It was a decision.  A few months later, we left Quebec hoping for the best.</p>
<p>There are times in your life you are given an opportunity, but those are far and few between.  Waiting on them is a mistake.  Instead, you need to make your own opportunities.  You need to make a decision.  There was a lot of work between deciding to move, and finally moving in to our new place.  I won&#8217;t say it was easy.  But it was easier than I thought it would be.  But still, it was a lot of work.  But the hardest part was finally admitting that the only person that could make this happen was my wife and me.  Making a decision.</p>
<p>We all make choices in our lives. They can be simple choices.  What to have for dinner?  What to name a variable?  What tree to use for this data set?  These are easy choices.  They are easy because we have control over them.</p>
<p>The decision to move, as big as it was, was easy to make when I understood I had to take control.  I couldn&#8217;t wait on a careless government.  Who could I trust, more than my wife and I, for my son&#8217;s well-being?  No one.</p>
<p>Since moving, Joseph has received a tremendous amount of real help tailored for his condition.  In November, when he turns 3, he goes in for more intensive treatment.  Everywhere doors are open.  The effort is on his treatment, not on finding treatment.</p>
<p>From the moment I made that decision until today, I never reflected on the difference of that decision as opposed to others I&#8217;ve made in my life. What, after all, truly made this decision different?</p>
<p>I made a choice about what I wanted.  After that was made, I simply had to make it happen.  Plot the course.  I didn&#8217;t make a choice to make a plan to reach an outcome.  I picked the destination.</p>
<p>People will tell you it&#8217;s not where we go but how we get there that matters.  And while I don&#8217;t disagree with the latter part, the destination had better matter, or else you&#8217;re just wandering aimlessly.  So what does all this mean?</p>
<p>Pick a destination.  Get there well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonlotito.com/life/pick-a-destination/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-19 23:59:07 -->
