<?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>Yan Pritzker &#187; code</title>
	<atom:link href="http://yanpritzker.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://yanpritzker.com</link>
	<description>photographer, entrepreneur, software engineer, musician, skier</description>
	<lastBuildDate>Thu, 26 Apr 2012 18:11:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<atom:link rel="next" href="http://yanpritzker.com/category/code/feed/?page=2" />

		<item>
		<title>5 minutes a day for exponential productivity</title>
		<link>http://yanpritzker.com/2012/04/25/5-minutes-a-day-for-exponential-productivity/</link>
		<comments>http://yanpritzker.com/2012/04/25/5-minutes-a-day-for-exponential-productivity/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 16:45:02 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[dotfiles]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=523</guid>
		<description><![CDATA[As a software developer, my day is taken by two primary activities: thought work and typing. Although I am a very fast typer, they pay me the big bucks ostensibly for what is inside my head, not my fingers. Every moment I spend typing something repetitive is therefore not only a waste of the time [...]]]></description>
			<content:encoded><![CDATA[<p>As a software developer, my day is taken by two primary activities: thought work and typing. Although I am a very fast typer, they pay me the big bucks ostensibly for what is inside my head, not my fingers. Every moment I spend typing something repetitive is therefore not only a waste of the time it takes to type it, but a waste of time away from thinking, developing, designing.  And every inch traveled by my hands brings me one inch closer to a potential repetitive strain injury.</p>
<p>So I decided to do something about this. Every single day, I spend at least 5 minutes learning something new that saves me time. I notice a repetitive motion, and I write a vim mapping, a shell alias, or a utility script, or just spend time looking at what <a href="https://github.com/search?q=dotfiles">other people are doing.</a> It&#8217;s sometimes hard to step back and be self aware about the things that I repeat, so I try to watch how others work too. Then I go and write a shortcut. This small investment every single day, pays huge dividends because I get exponentially faster at the mechanical parts of my job every day, leaving more time for thought and productivity.</p>
<p>I like doing this, and I like sharing my learnings with the world. If you want a peek at how I do things, check out <a href="http://skwp.github.com/dotfiles">my dotfiles project, YADR</a>. Fork it, copy it, steal the bits that you like, or contribute some of your own. Huge wins stand on the shoulders of lots of tiny ones.</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2012/04/25/5-minutes-a-day-for-exponential-productivity/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2012/04/25/5-minutes-a-day-for-exponential-productivity/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to win friends with Maintenance Driven Design</title>
		<link>http://yanpritzker.com/2012/04/04/how-to-win-friends-with-maintenance-driven-design/</link>
		<comments>http://yanpritzker.com/2012/04/04/how-to-win-friends-with-maintenance-driven-design/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 23:35:57 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=487</guid>
		<description><![CDATA[When I come to a design decision in writing an application, script, or interface. I ask myself only one question: What will the next guy think? I call this Maintenance Driven Design. By designing your software to be maintainable, you will earn many friends and praises. As software developers, ops guys, designers, it is a [...]]]></description>
			<content:encoded><![CDATA[<p>When I come to a design decision in writing an application, script, or interface. I ask myself only one question: </strong>What will the next guy think?</strong> I call this <strong>Maintenance Driven Design.</strong> By designing your software to be maintainable, you will earn many friends and praises.</p>
<p>As software developers, ops guys, designers, it is a known evil that we sometimes choose shortcuts, accruing tech debt with the knowledge that we&#8217;ll pay for it later with considerable interest. However, there are a few categories of design choices that we make every day that will make software easier or harder to maintain for the next guy who comes after us, which are specifically evil and malicious forms of tech debt. </p>
<p>Here are a few basic categories that most non-Maintenance Driven decisions fall into. Please don&#8217;t be that guy :)</p>
<ul>
<li><strong>duplication</strong> &#8211; This is the cardinal sin against Maintainable Design. If you duplicate code, it will instantly grow into piles of subtly divergent crap. It would have taken you an additional hour to figure out how to do it in a non-duplicated way. It will take your maintainers half a lifetime to understand why and how the code has diverged. My own anecdotal experience says that duplication has about a 1000% tech debt interest rate. That is, 1 hour saved by committing duplicated code will cost you 10 hours down the line. And this interest is compounded over time as the code diverges further.</li>
<li><strong>clever code</strong> &#8211; Clever is the enemy of Maintainable. Every time you cut and paste a really cool hack that you found on a blog in 2006, a unicorn dies. Follow conventions, and use existing and accepted practices instead of reinventing the wheel.</li>
<li><strong>personal aesthetics</strong> &#8211; Having a strong sense of personal style is awesome. You like snakeskin jackets and neon green pants? Sweet. But leave it out of the code. If you have a personal style that goes against accepted conventions, and you choose to prioritize your style over that of the community, you are not Maintenance Driven. When in doubt, look at the source code for a large community supported project (i.e. Rails) for style ideas.</li>
<li><strong>poor naming</strong> &#8211; Single letter variable names are impossible to grep for effectively. Names based on your favorite ninja turtles, though they will win you points with me, are bad for business. Same goes for commit messages &#8211; don&#8217;t write something clever and witty at the expense of clearly explaining your changes.</li>
</ul>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2012/04/04/how-to-win-friends-with-maintenance-driven-design/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2012/04/04/how-to-win-friends-with-maintenance-driven-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vim &#8211; Navigating files in vertical splits</title>
		<link>http://yanpritzker.com/2012/03/12/vim-navigating-files-in-vertical-splits/</link>
		<comments>http://yanpritzker.com/2012/03/12/vim-navigating-files-in-vertical-splits/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 16:44:22 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=468</guid>
		<description><![CDATA[The two most useful native vim mappings for navigation are arguably gf (go to file, use it over any reference to a file, such as a Rails partial), and Ctrl-] (go to tag, use it over any class or function name). But one problem in vim is that by doing these commands, you lose your [...]]]></description>
			<content:encoded><![CDATA[<p>The two most useful native vim mappings for navigation are arguably <code>gf</code> (go to file, use it over any reference to a file, such as a Rails partial), and <code>Ctrl-]</code> (go to tag, use it over any class or function name). But one problem in vim is that by doing these commands, you lose your previous place. Sure it&#8217;s easy to go back to with Ctrl-O or similar, but I find it much more useful to open the new location in a split, as you are usually exploring them while retaining the current context. Here are two mappings that do that for you:</p>
<p><script src="https://gist.github.com/2031016.js"> </script></p>
<p>If you like these, you might want to check out my <a href="https://github.com/skwp/dotfiles">Yet Another Dotfile Repo</a> project, which includes these and more!</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2012/03/12/vim-navigating-files-in-vertical-splits/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2012/03/12/vim-navigating-files-in-vertical-splits/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>The cleanest vimrc you&#8217;ll ever see</title>
		<link>http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/</link>
		<comments>http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 17:02:51 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=446</guid>
		<description><![CDATA[Ruby developer oriented dotfiles and vim configs to make your heart sing]]></description>
			<content:encoded><![CDATA[<p>So many of us use vimrc files that are a frankenstein&#8217;s monster, cobbled together from spare parts, gists, and snippets from wikis. One day I decided I was no longer going to treat my vimrc as a garbage dump. As programmers, we strive toward well factored code, so why do we accept such ugly vimrc&#8217;s that are impossible to understand, and even worse impossible to share because of their complexity. Let&#8217;s stop the insanity! It turns out it&#8217;s very easy&#8230;</p>
<p>Since vim automatically loads everything in ~/.vim/plugin/, you can just add a directory like ~/.vim/plugin/settings and put each semantic piece in its own snippet. Put your keymaps in ~/.vim/plugin/settings/keymap.vim. Put your visual setup like font and colors into ~/.vim/plugin/settings/gui.vim, etc. </p>
<p>After I did this, I ended up with a vimrc that&#8217;s only about 90 lines long including very detailed comments about every section. </p>
<p>I think this is the <a href="https://github.com/skwp/dotfiles/blob/master/vimrc">cleanest vimrc you&#8217;ll see on the web.</a> All my <a href="https://github.com/skwp/dotfiles/tree/master/vim/plugin/settings">vim plugin settings are broken down into individual files</a>, following a convention of one file per vim plugin that I use, plus some extra ones that are too small to be plugins. </p>
<p><strong>Think your vimrc is better? Show me! I want to learn :)</strong> </p>
<p>My vimrc and the rest of my vim setup is available as part of my <a href="http://github.com/skwp/dotfiles">YADR (Yet Another Dotfiles Repo) project</a>. Come check it out, I need alpha testers!</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Dotfiles for insane productivity in bash, git, and vim</title>
		<link>http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/</link>
		<comments>http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 22:59:57 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=356</guid>
		<description><![CDATA[Following the principles of insane productivity that I blogged about earlier, I am releasing my dotfile repository, containing customizations for bash, git, and vim. The guiding principles are two letter mnemonics for everything bash and git related, and single letter capital letter actions for all common Vim commands. The selection of vim plugins is best [...]]]></description>
			<content:encoded><![CDATA[<p>Following the principles of <a href="http://yanpritzker.com/2006/10/03/five-ways-to-get-insane-productivity-boosts/">insane productivity</a> that I blogged about earlier, I am releasing my <a href="http://github.com/skwp/dotfiles">dotfile repository</a>, containing customizations for bash, git, and vim.</a> The guiding principles are two letter mnemonics for everything bash and git related, and single letter capital letter actions for all common Vim commands. The selection of vim plugins is best of breed for navigation, color highlighting, and git integration. The vim setup is optimized for rails and git development. The vimrc contains my custom single-letter aliases and is pretty well commented, so that you can easily take or reject my modifications as you need. and I will continue to clean up the repo and move everything into tpope&#8217;s pathogen plugin format over time. Enjoy and please send feedback!</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>vim/rails/git: find references to current partial</title>
		<link>http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/</link>
		<comments>http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 16:34:57 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=344</guid>
		<description><![CDATA[Update 12/15/2011: Released as a plugin on github Blazing fast way of finding references to the current partial you&#8217;re viewing using this simple script. Requires git-grep.vim. Bonus: at the bottom of the script I also define &#8220;K&#8221; to git grep the current word. Very handy for fast function reference finding. Like this post? Here are [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update 12/15/2011: Released as a plugin <a href="https://github.com/skwp/vim-git-grep-rails-partial">on github</a></strong></p>
<p>Blazing fast way of finding references to the current partial you&#8217;re viewing using this simple script. Requires <a href="https://github.com/tjennings/git-grep-vim">git-grep.vim</a>. Bonus: at the bottom of the script I also define &#8220;K&#8221; to git grep the current word. Very handy for fast function reference finding.</p>
<p><script src="https://gist.github.com/1327904.js?file=git_grep_partial.vim"></script></p>
<p>Like this post? Here are some more you might enjoy:</p>
<ul>
<li><a href="http://yanpritzker.com/2006/10/03/five-ways-to-get-insane-productivity-boosts/">Five ways to get insane productivity boosts</a></li>
<li><a href="http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/">Colorful ruby tests and debugging</a></li>
</ul>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Colorful Vim Ruby Tests and Debugging</title>
		<link>http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/</link>
		<comments>http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 17:07:53 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=286</guid>
		<description><![CDATA[Update 12/15/2011: released as a plugin on github Bet your TextMate can&#8217;t do this! Screenshot first, how-to follows :) And now the how-to&#8230; ConqueTerm for a color-enabled embedded vim terminal ConqueTerm is pretty magical. It gives you an embedded shell where you can run bash, rib, rake, really..you name it. All with full ansi color [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update 12/15/2011: released as a plugin <a href="https://github.com/skwp/vim-ruby-conque">on github</a></strong></p>
<p>Bet your TextMate can&#8217;t do this! Screenshot first, how-to follows :)</p>
<p><a href="http://i.imgur.com/dhM1z.png"><img src="http://i.imgur.com/dhM1z.png" style='width:400px'/></a></p>
<p>And now the how-to&#8230;</p>
<h3>ConqueTerm for a color-enabled embedded vim terminal</h3>
<p>ConqueTerm is pretty magical. It gives you an embedded shell where you can run bash, rib, rake, really..you name it. All with full ansi color support. This lets us run red green tests that actually look green and not as a bunch of ansi escape codes.</p>
<ul>
<li><a href="http://code.google.com/p/conque/">Homepage</a></li>
<li><a href="http://conque.googlecode.com/files/conque_2.3.vmb">Direct Download Link</a></li>
<li>Install: follow directions on homepage or just <code>:so conque_2.3.vmb</code> from vim and restart</li>
</ul>
<h3>Setting up aliases for running bash, rails console, and irb</h3>
<p>Put this snippet into your .vimrc. This will give you a few aliases to use in vim:</p>
<ul>
<li><code>\r</code> for <strong>r</strong>uby on the current file as a ruby executable (great for unit tests)</li>
<li><code>\R</code> for <strong>R</strong>ake, great for running the entire test suite</li>
<li><code>\S</code> for R<strong>S</strong>pec on the current file</li>
<li><code>\i</code> for <strong>i</strong>rb (actually pry)</li>
<li><code>\c</code> for the Rails <strong>c</strong>onsole </li>
<li><code>\b</code> for <strong>b</strong>ash</li>
<li><code>\e</code> to <strong>e</strong>valuate the current selection in the already open console</li>
</ul>
<p><script src="https://gist.github.com/1316994.js"> </script></p>
<p>To get your tests to run red and green, <code>gem install redgreen</code> and <code>require 'redgreen'</code> in your test helper. By using the above <code>\r</code> alias with your ruby files, you will now see the output of your red green tests inside the colorized ConqueTerm. You can also run rspec colorized in ConqueTerm.</p>
<h3>Variable inspection and debugging with Pry</h3>
<p><code>gem install pry</code> is one of the most brilliant and useful things I&#8217;ve seen. Simply by requiring this gem in your code and then adding a <code>binding.pry</code> in the spot where you want to break, you will be dumped into a very nice (and again colorized if you use the ConqueTerm) debugging context where you can inspect any variable by simply typing its name. </p>
<p>The other cool thing is that Pry lets you treat your code like a filesystem (try <code>ls</code> and <code>cd [varname]</code> to dive into complex objects). Try it out! You can also use pry as an irb substitute. Simply type <code>pry</code> at the bash command line, or use <code>script/console --irb=pry</code> with your Rails 2.x project. Here is an <a href="https://github.com/pry/pry/wiki/Setting-up-Rails-to-use-Pry">page on pry&#8217;s github site on how to set it up in other environments</a>. </p>
<p>Pry does <a href="https://github.com/pry/pry/wiki">a whole ton more</a> including the ability to create &#8216;macros&#8217; in your irb, but that&#8217;s a whole different post. </p>
<p>Enjoy!</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fear and loathing in code</title>
		<link>http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/</link>
		<comments>http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 16:24:32 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=236</guid>
		<description><![CDATA[When we develop code from a position of fear rather than one of confidence, we make design decisions that lead to unmaintainable code. A developer who is afraid to mess things up asks himself: what is the absolute minimal change I can make to fix the symptoms without affecting underlying behavior. The answer involves creating [...]]]></description>
			<content:encoded><![CDATA[<p>When we develop code from a position of fear rather than one of confidence, we make design decisions that lead to unmaintainable code. </p>
<p>A developer who is afraid to mess things up asks himself: <em>what is the absolute minimal change I can make to fix the symptoms without affecting underlying behavior.</em> The answer involves creating some workaround, patch, or quick fix that hides the problem at hand without addressing the underlying cause. The upside is the developer feels safe &#8211; by making a minimal change he has taken on minimal risk, and he probably won&#8217;t get yelled at for breaking things.</p>
<p>But fear of change has a big price. Every piece of code that patches a symptom instead of fixing deep seated issues is moving the codebase towards less maintainability. Even if it lets us squeeze a release out today, it&#8217;s a hidden burden on future releases, and it&#8217;s a huge burden on every developer that comes to fix it later. </p>
<p>When you don&#8217;t continuously work to improve old code, you risk the failure not only of the product, but the entire team. Developers throw their hands up, leave the company, and new developers don&#8217;t want to touch legacy code. The Rewrite is declared, but without addressing any of the underlying Fear in developer DNA, we are bound to repeat the same mistakes.</p>
<p>The correct question to ask is: <em>what is the root of the problem, what else in the system may be affected by it, and what do I need to change in order for this system to be more maintainable tomorrow than it is today.</em> The answer to this question is usually not easy, takes longer to implement, and is harder to explain to your boss, who wants the product out next week. But it&#8217;s nevertheless the right question to ask.</p>
<p>To create maintainable systems that survive the test of time and, perhaps more importantly to the company&#8217;s survival, create code that is inviting to new developers as we grow the team, we have to develop from a position of confidence rather than fear. Here&#8217;s how we get there:</p>
<ul>
<li><strong>Declare one day of the week testing/refactoring day</strong> where no new functionality is developed, and everyone is asked to pick a part of the system that is turning into unmaintainable legacy code and bring it back to life. This is a minimum, not a maximum. Refactoring should be constant and ongoing.</li>
<li><strong>Add tests to provide an insurance policy against breakage.</strong> If working on a part of system that is untested, write tests first before making your changes. Change is less risky with the safety net of tests underneath you.</li>
<li><strong>Encourage collective code ownership.</strong> It&#8217;s fine for one person to be the thought and code leader on a project but if no one else ever looks at that code, it is feared by the rest of the team, and the success of the project rests on a single developer. If he leaves the company, the project is finished.</li>
<li><strong>Use code reviews to minimize risk of large changes</strong> and encourage a team effort in cleaning up the system. Ask developers to spend half of their refactoring time reviewing and/or refactoring code from a part of the system that is not their own.</li>
<li><strong>Encourage code walkthroughs</strong> (distributed teams, use screencasts). Create a time and place for people to show off techniques they used, or just introduce others to parts of the system they may not be familiar with.</li>
</ul>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Five Rules for Writing Good Code</title>
		<link>http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/</link>
		<comments>http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 21:22:55 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[conventions]]></category>
		<category><![CDATA[habits]]></category>

		<guid isPermaLink="false">http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/</guid>
		<description><![CDATA[1. Write for an audience Code tends to outlive jobs. When we create code, we are not writing for ourselves, but for an audience of peers and progeny that will look upon it and have to maintain it. Do we want them to do so with awe and respect, or with fear and disgust? Writing [...]]]></description>
			<content:encoded><![CDATA[<p>1. Write for an audience</p>
<p>Code tends to outlive jobs. When we create code, we are not writing for ourselves, but for an audience of peers and progeny that will look upon it and have to maintain it. Do we want them to do so with awe and respect, or with fear and disgust? Writing maintainable code not only makes our life easier, but the lives of those around us, and garners admiration, praise, and rewards &#8211; if not always financial, at the very least karmic.</p>
<p>2. Establish a clean framework for future changes</p>
<p>If we start writing new code, and the pattern we establish is that we&#8217;ve copied and pasted a line across ten functions, what will happen when someone else comes along to add an eleventh function? Let&#8217;s face it, even the neatest programmers can get lazy. If something has been copy-pasted ten times, it will get copy-pasted for the 11th time, and a year later, when we find out we have to change the logic or content of that line, we are now changing 50 lines where we could have had one. Untangling code is always a lot harder than writing it, and we could have prevented the spread of the copy-paste disease by evaluating our code initially for repetitive statements. Always establish a <i>framework</i> for others to follow by writing clean code that reduces repetition, and encourages maintainability by isolating each piece of logic and content to one spot only.</p>
<p>3. Be brief, self-descriptive, and avoid inline comments</p>
<p>Five line functions with descriptive names are easy to understand. They require no comments. Thirty line functions take quite a bit of brainpower to digest, and usually have smelly comments scattered all throughout trying to explain bits of the function. Hundred line functions are an assault on all that is holy and stink to high heaven. If we see lots of comments interspersed in a method, it is a good sign that the code cannot be easily understood. Break it down into lots of little functions with descriptive names, and all of a sudden our code reads like a very clear and concise recipe, and we find that the comments become redundant. Most comments that live inside functions are parasitic organisms, treat them with suspicion.</p>
<p>4. Follow language standards and community conventions</p>
<p>If we break conventions, the next person to read our code will wonder why we did so. When our code raises questions about its style, the reader may start wondering if there was some specific reason that we coded it that way. Worse, they may perpetuate our unusual style through imitation and copy-paste tactics. This makes code harder to understand and wastes other people&#8217;s time. I&#8217;ve heard an argument from more than one person that they will code with the style they are comfortable with rather than follow language convention. This is especially true of people migrating from one language to another without investing their time in learning new habits and techniques. Remember that most of the time, we are writing code that <i>someone else</i> will be maintaining. Make their job easier by conforming to widely acknowledged standards, so they don&#8217;t have to spend any extra mental energy reading through it. It may take we a larger effort initially, but it will pay off with dividends when it comes to maintenance.</p>
<p>5. Really learn the language and the framework</p>
<p>Lots of bad code is written because of language or framework ignorance. If we don&#8217;t know the framework we use, we might reinvent the wheel or write obtuse code because we&#8217;re not taking advantage of the conventions and helpers already created for us. No need to be a walking encyclopedia, but remember to occasionally open up that encyclopedia and read through it so that you know at least what&#8217;s out there. I am sometimes surprised by new things I find in a framework I&#8217;ve used for quite a while, that makes my life a whole lot easier. Don&#8217;t neglect the docs, and don&#8217;t neglect to keep up with blogs that discuss new techniques.</p>
<p>I&#8217;m not really back, I&#8217;m just pretending. That&#8217;s all for now :)</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Long running Threads in Rails and metaprogramming fun</title>
		<link>http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/</link>
		<comments>http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 03:07:41 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[background]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[thoughts]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://skwpspace.com/?p=166</guid>
		<description><![CDATA[Disclaimer: This post contains evil (but highly fun!) code. Proceed at your own peril&#8230; I was recently designing an application that needed to execute some long running requests against an external host. If you&#8217;ve ever tried doing something like this in Rails, you&#8217;ll find your mongrels will block up waiting for the request to complete, [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: This post contains evil (but highly fun!) code. Proceed at your own peril&#8230;</p>
<p>I was recently designing an application that needed to execute some long running requests against an external host. If you&#8217;ve ever tried doing something like this in Rails, you&#8217;ll find your mongrels will block up waiting for the request to complete, bringing the experience for all other users to a halt. </p>
<p>I wanted to dispatch my long running request, return to the user, and then poll for results using AJAX. There are many ways to do background tasks in Rails, most of which require running an out of process background server with which you will communicate over some sort of queue or memcached. There&#8217;s <a href="http://backgroundrb.rubyforge.org/">BackgroundRb</a>, <a href="http://agilewebdevelopment.com/plugins/bj">Bj</a>, <a href="http://playtype.net/past/2008/2/6/starling_and_asynchrous_tasks_in_ruby_on_rails/">workling</a>, and so on, but this seemed overkill for my problem.</p>
<p>After reading a <a href="http://bibwild.wordpress.com/2007/08/28/threading-in-rails/">post on using Ruby Threads</a>, I decided to be brave and try this approach. I implemented a simple action which would spawn a thread and proceed to return the result whether it was ready or not. This action is polled via AJAX and on the next poll the result will be correct. The pseudocode looks something like</p>
<pre class="textmate-source black_pearl"><span class="source source_ruby"><span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">long_running_action</span></span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span>spawn a thread
</span>  precache_the_results

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> This action throws DataNotAvailableException
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> if file is missing/unreadable
</span>  results <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> read_cached_results

<span class="keyword keyword_control keyword_control_ruby">rescue</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">DataNotAvailableException</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> This tells me that when I load the page
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> I should invoke an ajax a couple seconds
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> later to check for results again
</span>  flash<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>update_right_away</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="constant constant_language constant_language_ruby">true</span>
<span class="keyword keyword_control keyword_control_ruby">ensure</span>
  respond_to <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">wants</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">    <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> render an RJS update with the results
</span>  <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">precache_the_results</span></span>
  <span class="support support_class support_class_ruby">Thread</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block">
</span>    expensive_action_outputs_to<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>file.txt<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
  <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>
<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>Because I didn&#8217;t <code>join</code> the Thread to the request thread, it lives on after the request completes, which is just what I needed. Since the code inside my Thread is a call to an external provider and doesn&#8217;t write to the database, I am not concerned with ActiveRecord threading issues.</p>
<p><b>The only problem</b> with this approach is that in development mode, Rails likes to reload your classes on every request. But if your thread runs past the request lifetime, the class that&#8217;s running it may be unloaded while it&#8217;s running, wreaking all sorts of havoc. But Ruby allows us the power to be <em>truly evil:</em>. What if I just prevent Threads from doing what they want to in development mode? Turns out I can!</p>
<pre class="textmate-source black_pearl"><span class="source source_ruby source_ruby_rails"><span class="keyword keyword_control keyword_control_ruby">if</span> <span class="meta meta_environment-variable meta_environment-variable_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">ENV</span>[<span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>RAILS_ENV<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>]</span> <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">==</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>development<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>
<span class="meta meta_class meta_class_ruby">  <span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">Thread</span></span>
    <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">initialize</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby"><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">&amp;</span>block</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
      block<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>call
    <span class="keyword keyword_control keyword_control_ruby">end</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="keyword keyword_control keyword_control_ruby">end</span>
</span></pre>
<p>This code is defined in the class where I&#8217;m doing the magic. Do NOT just slap this into your environment.rb as you&#8217;ll horribly break the Rails startup logic. There&#8217;s probably a slightly smarter and safer way to do this by using a Factory pattern to create the threads and explicitly specifying the implementation you want. But this is my party and I&#8217;ll monkeypatch if I want to.</p>
<p>So..comments, suggestions, complaints? Is this going to die horribly in production? I guess we&#8217;ll have to see!</p>
<div class="plus-one-wrap"><g:plusone href="http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

