<?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>Samson&#039;s Weblog &#187; Share</title>
	<atom:link href="http://blog.samsonis.me/tag/share/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.samsonis.me</link>
	<description>天与弗取，反受其咎；时至不行，反受其殃</description>
	<lastBuildDate>Sun, 29 Jan 2012 13:53:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Sublime Text 2 Upgrade Problem</title>
		<link>http://blog.samsonis.me/2011/12/sublime-text-2-upgrade-problem/</link>
		<comments>http://blog.samsonis.me/2011/12/sublime-text-2-upgrade-problem/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 03:26:27 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Share]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[Sublime Text 2]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3670</guid>
		<description><![CDATA[搞了半天发现好还毕竟不是sublime本身的错，要不就完全毁了这几个星期来对之的美好印象了…… btw，sublime text 2着实是个相当promising的editor，各位看官有闲情雅致且有信心记住更多的key shortcuts的话不妨一试（其实也没有多少啦），鄙人highly recommended（路人甲：你算哪跟葱……；我：……），除了其License价格不菲之外（比textmate多1刀），其余很多feature都可圈可点，好消息是beta版可以免费使用，就是时不时会有dialog跳出来提醒抑或是恶心一下你，呵呵。 废话有点多，不过鉴于某些同学口味比较重比较独特只喜欢看本人的废话部分，所以现在我基本把重要的事情都放在废话部分讲…… anyway，本次遇到的问题是sublime无法启动，报出以下颇为technical的error dialog，然后便quit，想必是目前用此editor的大多还是developer居多，所以报这种样式的error通常会被认为更为friendly： Unable to run package setup: Traceback &#40;most recent call last&#41;: File &#34;./PackageSetup.py&#34;, line 155, in upgrade upgradePackage&#40;pkg, pristinedir, datadir, backupdir&#41; File &#34;./PackageSetup.py&#34;, line 148, in upgradePackage os.path.join&#40;backupdir, base&#41;, inhibitOverwrite&#41; File &#34;./PackageSetup.py&#34;, line 31, in upgradeArchive newar = zipfile.ZipFile&#40;src&#41; File &#34;.\zipfile.py&#34;, line 696, in __init__ File &#34;.\zipfile.py&#34;, ...]]></description>
			<content:encoded><![CDATA[<p>搞了半天发现好还毕竟不是sublime本身的错，要不就完全毁了这几个星期来对之的美好印象了……</p>
<p>btw，<a href="http://www.sublimetext.com/" title="Sublime Text 2" target="_blank">sublime text 2</a>着实是个相当promising的editor，各位看官有闲情雅致且有信心记住更多的key shortcuts的话不妨一试（其实也没有多少啦），鄙人highly recommended（路人甲：你算哪跟葱……；我：……），除了其License价格不菲之外（比textmate多1刀），其余很多feature都可圈可点，好消息是beta版可以免费使用，就是时不时会有dialog跳出来提醒抑或是恶心一下你，呵呵。</p>
<p>废话有点多，不过鉴于某些同学口味比较重比较独特只喜欢看本人的废话部分，所以现在我基本把重要的事情都放在废话部分讲……</p>
<p>anyway，本次遇到的问题是sublime无法启动，报出以下颇为technical的error dialog，然后便quit，想必是目前用此editor的大多还是developer居多，所以报这种样式的error通常会被认为更为friendly：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Unable to run package setup:<br />
<br />
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:<br />
File <span style="color: #ff0000;">&quot;./PackageSetup.py&quot;</span>, line <span style="color: #000000;">155</span>, <span style="color: #000000; font-weight: bold;">in</span> upgrade<br />
upgradePackage<span style="color: #7a0874; font-weight: bold;">&#40;</span>pkg, pristinedir, datadir, backupdir<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
File <span style="color: #ff0000;">&quot;./PackageSetup.py&quot;</span>, line <span style="color: #000000;">148</span>, <span style="color: #000000; font-weight: bold;">in</span> upgradePackage<br />
os.path.join<span style="color: #7a0874; font-weight: bold;">&#40;</span>backupdir, base<span style="color: #7a0874; font-weight: bold;">&#41;</span>, inhibitOverwrite<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
File <span style="color: #ff0000;">&quot;./PackageSetup.py&quot;</span>, line <span style="color: #000000;">31</span>, <span style="color: #000000; font-weight: bold;">in</span> upgradeArchive<br />
newar = zipfile.ZipFile<span style="color: #7a0874; font-weight: bold;">&#40;</span>src<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
File <span style="color: #ff0000;">&quot;.\zipfile.py&quot;</span>, line <span style="color: #000000;">696</span>, <span style="color: #000000; font-weight: bold;">in</span> __init__<br />
File <span style="color: #ff0000;">&quot;.\zipfile.py&quot;</span>, line <span style="color: #000000;">716</span>, <span style="color: #000000; font-weight: bold;">in</span> _GetContents<br />
File <span style="color: #ff0000;">&quot;.\zipfile.py&quot;</span>, line <span style="color: #000000;">728</span>, <span style="color: #000000; font-weight: bold;">in</span> _RealGetContents<br />
zipfile.BadZipfile: File is not a <span style="color: #c20cb9; font-weight: bold;">zip</span> <span style="color: #c20cb9; font-weight: bold;">file</span></div></div>
<p><span id="more-3670"></span><br />
最为简单粗暴的方法是重装，鉴于我装的plugin/package有点多，且都做了各种配置，所以对此心有余悸，不过作为backup plan倒未尝不可。</p>
<p>另一种方法便是看error的trackback了，瞧，人家就是friendly，error分明是在complain读了一个bad zip file，那究竟那个坏小子藏在什么地方呢，root的package我知道，就是sublime的library目录（位于~/Library/Application Support/Sublime Text 2），但事实证明这个还真不好找，因为人家虽然是zip格式，但并不以.zip结尾……</p>
<p>最后找了半个多小时，终于在Installed Packages目录下找到了元凶，貌似是一个codeIntel插件出了点问题，这可能跟我之前装完之后又手动build有关，anyway，干掉它先，毕竟我需要开起来工作先，完美的解决办法留给以后有空再慢慢找吧。</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">mv</span> SublimeCodeIntel.sublime-package <span style="color: #000000; font-weight: bold;">/</span>tmp</div></div>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/08/%e8%af%b4%e8%b0%8e/" title="说谎 (August 9, 2010)">说谎</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e8%8a%82%e9%80%89%e4%b8%a4%e6%ae%b5%e5%a5%a5%e5%b7%b4%e9%a9%ac2011%e5%9b%bd%e6%83%85%e5%92%a8%e6%96%87/" title="节选两段奥巴马2011国情咨文 (January 26, 2011)">节选两段奥巴马2011国情咨文</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2010/07/%e7%8b%ae%e5%ad%90%e5%ba%a7/" title="狮子座 (July 31, 2010)">狮子座</a> (16)</li>
	<li><a href="http://blog.samsonis.me/2010/02/%e6%9c%aa%e6%88%90%e5%b9%b4%e5%85%88%e5%88%ab%e5%84%aa/" title="未成年先别優 (February 23, 2010)">未成年先别優</a> (1)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e6%8e%a8%e8%8d%90%e4%b8%80%e4%b8%aareader-app%e5%92%8c%e4%b8%80%e4%b8%aagreasemonkey%e8%84%9a%e6%9c%ac/" title="推荐一个reader app和一个Greasemonkey脚本 (January 26, 2011)">推荐一个reader app和一个Greasemonkey脚本</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/12/sublime-text-2-upgrade-problem/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Felix’s Node.js Style Guide (Part 2)</title>
		<link>http://blog.samsonis.me/2011/10/felix%e2%80%99s-node-js-style-guide-part-2/</link>
		<comments>http://blog.samsonis.me/2011/10/felix%e2%80%99s-node-js-style-guide-part-2/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 04:20:49 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3614</guid>
		<description><![CDATA[接上篇：Felix’s Node.js Style Guide (Part 1) 创建Object / Array 逗号应放在行尾而不是行首，每一行放置简短的变量声明。只在你的解释器抱怨的时候才应该去给array的key上加上引号。 正确： var a = &#91;'hello', 'world'&#93;; var b = &#123; &#160; good: 'code', &#160; 'is generally': 'pretty', &#125;; 错误： var a = &#91; &#160; 'hello', 'world' &#93;; var b = &#123;&#34;good&#34;: 'code' &#160; &#160; &#160; &#160; , is generally: 'pretty' &#160; &#160; &#160; &#160; &#125;; ...]]></description>
			<content:encoded><![CDATA[<p>接上篇：<a href=" http://blog.samsonis.me/2011/10/felixs-node-js-style-guide-part-1/" title="  Felix’s Node.js Style Guide (Part 1)">Felix’s Node.js Style Guide (Part 1)</a></p>
<h3>创建Object / Array</h3>
<p>逗号应放在行尾而不是行首，每一行放置简短的变量声明。只在你的解释器抱怨的时候才应该去给array的key上加上引号。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'world'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; good<span style="color: #339933;">:</span> <span style="color: #3366CC;">'code'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'is generally'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'pretty'</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; <span style="color: #3366CC;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'world'</span><br />
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;good&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'code'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">is</span> generally<span style="color: #339933;">:</span> <span style="color: #3366CC;">'pretty'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p><span id="more-3614"></span></p>
<h3>等号操作符</h3>
<p>编程不是仅仅记住那些愚蠢的规则。比如我们应该使用三个等号的相等操作符，因为它会有我们意料之中的行为。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">===</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'winning'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'losing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>原型扩展</h3>
<p>不要扩展任何对象的prototype原型，特别是native的对象。如果你不遵守这条准则，地狱里面某个特殊的地方正在等着你。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>a.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'winning'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">empty</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">empty</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><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'losing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>条件判断</h3>
<p>任何只要不是没用的条件判断都应该被赋予一个有意义具有描述性的变量名：</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> isAuthorized <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>user.<span style="color: #660066;">isAdmin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> user.<span style="color: #660066;">isModerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isAuthorized<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'winning'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>user.<span style="color: #660066;">isAdmin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> user.<span style="color: #660066;">isModerator</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><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'losing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>函数长度</h3>
<p>函数尽量保持简短。一个好的函数应该能够完整的放到一张幻灯片里面，且能让一个大会议室的最后一排观众可以轻松舒适的阅读到。所以不要指望他们都有很好的视力，你应该限制你的每个函数都在10行左右。</p>
<h3>Return语句</h3>
<p>为了避免if语句的深层次嵌套，函数内部尽量越早return越好。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> isPercentage<span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>val <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>val <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> isPercentage<span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>val <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>val <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>就这个例子来说，我们还可以更加简化：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> isPercentage<span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> isInRange <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>val <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> val <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> isInRange<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>命名闭包</h3>
<p>可以给你的闭包取个名字。这意味着你很关心它们，且会产生更好的堆栈跟踪（stack trace）。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">req.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'end'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> onEnd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'winning'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">req.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'end'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'losing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>嵌套闭包</h3>
<p>使用闭包，但不要嵌套它们。否则你的代码会变的一团糟。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; client.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>afterConnect<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> afterConnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'winning'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; client.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'losing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>回调（Callbacks）</h3>
<p>由于node意味着到处都是非阻塞I/O，函数通常都会使用回调函数来返回结果。node核心开发小组的惯例是保留回调函数的第一个参数，以用来存储可能存在的error对象。</p>
<p>你应该在你自己的回调函数里使用相同的方法。</p>
<h3>Object.freeze, Object.preventExtensions, Object.seal, with, eval</h3>
<p>这些都是些疯狂的东西，你很有可能永远不需要它们。远离它们。</p>
<h3>Getters and setters</h3>
<p>不要使用setters，它们会产生远比它能解决的麻烦多的多的麻烦。</p>
<p>可以在不产生<a href="http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29" title="side effects" target="_blank">副作用</a>的时候使用getters，比如给你的集合类提供一个length属性。</p>
<h3>EventEmitters</h3>
<p>Node.js自带了一个简单的EventEmitter类，它可以从&#8217;event&#8217;模块里面进行include：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> EventEmitter <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'events'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">EventEmitter</span><span style="color: #339933;">;</span></div></div>
<p>在创建复杂的类的时候，继承这个EventEmitter类来用来产生事件是很常见的做法。这基本上就是一个简单的<a href="http://en.wikipedia.org/wiki/Observer_pattern" title="Observer Pattern" target="_blank">观察者模式</a>的实现。</p>
<p>不过，我强烈建议你不要在你自己的类里面监听它自己所发出的事件。让一个对象监听它自己是很不自然的。它经常还会以你不希望的形式将你的实现细节暴露出来，同时让你的代码更难看懂。</p>
<h3>继承 / 面向对象编程</h3>
<p>继承和面向对象编程它们本身各自都是主题。如果你对这个流行的编程模型有兴趣，请阅读我的<a href="http://nodeguide.com/object_oriented_programming.html" title="Object oriented programming guide" target="_blank">面向对象编程指南</a>。</p>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/" title="Felix的Node.js初学者入门指南 (October 26, 2011)">Felix的Node.js初学者入门指南</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2011/10/felixs-node-js-style-guide-part-1/" title="Felix&#8217;s Node.js Style Guide (Part 1) (October 27, 2011)">Felix&#8217;s Node.js Style Guide (Part 1)</a> (4)</li>
	<li><a href="http://blog.samsonis.me/2011/06/drink-coffee-then-write-coffeescript/" title="喝coffee写coffeescript (June 7, 2011)">喝coffee写coffeescript</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2011/05/live-fortune-node-js-that-do-not-crash/" title="Live Fortune (Node.js) that do not crash (May 31, 2011)">Live Fortune (Node.js) that do not crash</a> (18)</li>
	<li><a href="http://blog.samsonis.me/2011/06/deploying-live-fortune-node-js-with-nginx-and-monit/" title="Deploying Live Fortune (Node.js) with Nginx and Monit (June 1, 2011)">Deploying Live Fortune (Node.js) with Nginx and Monit</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/10/felix%e2%80%99s-node-js-style-guide-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Felix&#8217;s Node.js Style Guide (Part 1)</title>
		<link>http://blog.samsonis.me/2011/10/felixs-node-js-style-guide-part-1/</link>
		<comments>http://blog.samsonis.me/2011/10/felixs-node-js-style-guide-part-1/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 13:09:39 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3583</guid>
		<description><![CDATA[本文为译文，翻译自原文 http://nodeguide.com/style.html 标题咱就不翻了吧，说实话guide这个词还真不好翻，不能显得很高深又不能显得很for dummy的样子，索性就不翻了…… 本guide应该较上篇略有意思，作者泰国海滩度假的时候写出来的东西想来也不会多么干巴巴（海滩不是到处都是水吗……），当然本文所涉及的编程风格之类的选择应该完全是作者自己的opinion，不代表我台观点。 ============================== 我是分割线 ============================== 本文不是node.js应用程序所应该遵循风格的官方文档。本guide只是试图带给你一整套instructions，虽说本人有点固执己见，但这套东西确实能让你创建美观且统一的软件。 本guide假设你只针对node.js。如果你的代码需要在浏览器或者其它环境里面运行，请勇敢的忽略其中一些部分。 另外请注意node.js和围绕node的其它很多package都有其自身略显不同的风格，所以如果你对那些项目感兴趣，请遵守他们那边的规则。（别到时被砍……译者注） Tabs vs Spaces 让我们先从宗教式的信仰问题开始。我们仁慈的独裁者在node的核心代码里选择了用2个空格来缩进，所以跟着他是不会有错的。 分号 有很多非暴力不合作反抗分子试图偷走你的分号。不过不要犯傻，我们的传统美德还是有它存在的道理的，且确实存在的好好的。所以跟随社区，使用分号！ 编辑器 你可以使用任何编辑器。不过如果有JS语法高亮并能用node.js执行当然正打开的文件的功能的话则会比较顺手。诚然vim可能不会给女生们留下深刻印象，但我们的BDFL却会很愉快，且你的爷爷（不是你爷爷的……译者注）也不会有什么意见。 这份文档是在我的iPad上的Notes程序里面打出来的，那是因为我那时正在泰国的某个海滩边上。所以你的工作环境很有可能也会影响到你的编辑器选择。（我知道你不就是想告诉我们你到泰国海滨度假了嘛，怎么跟我国很多mm一副德行…… 译者注） 结尾的空白字符 就像每顿饭之后要刷牙一样，你也会在每次提交你的JavaScript文件之前清理掉你的行末空白字符。否则这种粗心大意的疏漏使代码所散发出的某种异味会最终把你潜在的contributors和同事吓跑。 每行长度 每行限制在80个字符。是的我知道，和过去几年比，如今我们的屏幕已经大了好多，但是你的大脑可没有。屏幕其余空白的地方可以用来进行分列显示，你的编辑器应该支持分割screen的，对吧？（TextMate好像不支持，ft…… 译者怨念中……） 引号 除非你在写JOSN，不然请使用单引号。 正确： var foo = 'bar'; 错误： var foo = &#34;bar&#34;; 大括号 你的前大括号应该放在和前面最近的那句语句的同一行上。 正确： if &#40;true&#41; &#123; &#160; console.log&#40;'winning'&#41;; &#125; 错误： if &#40;true&#41; &#123; &#160; console.log&#40;'losing'&#41;; &#125; 同时请留意一下，上面条件语句的前后都留有空格。 ...]]></description>
			<content:encoded><![CDATA[<p>本文为译文，翻译自原文 <a href="http://nodeguide.com/style.html" target="_blank">http://nodeguide.com/style.html</a></p>
<p>标题咱就不翻了吧，说实话guide这个词还真不好翻，不能显得很高深又不能显得很for dummy的样子，索性就不翻了……</p>
<p>本guide应该较<a href="http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/" title="Felix的Node.js初学者入门指南">上篇</a>略有意思，作者泰国海滩度假的时候写出来的东西想来也不会多么干巴巴（海滩不是到处都是水吗……），当然本文所涉及的编程风格之类的选择应该完全是作者自己的opinion，不代表我台观点。</p>
<p>============================== 我是分割线 ==============================</p>
<p>本文不是node.js应用程序所应该遵循风格的官方文档。本guide只是试图带给你一整套instructions，虽说本人有点固执己见，但这套东西确实能让你创建美观且统一的软件。</p>
<p>本guide假设你只针对node.js。如果你的代码需要在浏览器或者其它环境里面运行，请勇敢的忽略其中一些部分。</p>
<p>另外请注意node.js和围绕node的其它很多package都有其自身略显不同的风格，所以如果你对那些项目感兴趣，请遵守他们那边的规则。（别到时被砍……译者注）<br />
<span id="more-3583"></span></p>
<h3>Tabs vs Spaces</h3>
<p>让我们先从宗教式的信仰问题开始。我们<a href="http://nodeguide.com/community.html#ryan-dahl" title="benevolent dictator" target="_blank">仁慈的独裁者</a>在node的核心代码里选择了用2个空格来缩进，所以跟着他是不会有错的。</p>
<h3>分号</h3>
<p>有很多<a href="http://nodeguide.com/community.html#isaac-schlueter" title="rebellious forces" target="_blank">非暴力不合作反抗分子</a>试图偷走你的分号。不过不要犯傻，我们的传统美德还是有它存在的道理的，且确实<a href="http://news.ycombinator.com/item?id=1547647" target="_blank">存在的好好的</a>。所以跟随社区，使用分号！</p>
<h3>编辑器</h3>
<p>你可以使用任何编辑器。不过如果有JS语法高亮并能用node.js执行当然正打开的文件的功能的话则会比较顺手。诚然<a href="http://www.vim.org/" title="vim" target="_blank">vim</a>可能不会给女生们留下深刻印象，但我们的<a href="http://en.wikipedia.org/wiki/BDFL" title="BDFL" target="_blank">BDFL</a>却会很愉快，且你的爷爷（不是你爷爷的……译者注）也不会有什么意见。</p>
<p>这份文档是在我的iPad上的Notes程序里面打出来的，那是因为我那时正在泰国的某个海滩边上。所以你的工作环境很有可能也会影响到你的编辑器选择。（我知道你不就是想告诉我们你到泰国海滨度假了嘛，怎么跟我国很多mm一副德行…… 译者注）</p>
<h3>结尾的空白字符</h3>
<p>就像每顿饭之后要刷牙一样，你也会在每次提交你的JavaScript文件之前清理掉你的行末空白字符。否则这种粗心大意的疏漏使代码所散发出的某种异味会最终把你潜在的contributors和同事吓跑。</p>
<h3>每行长度</h3>
<p>每行限制在80个字符。是的我知道，和过去几年比，如今我们的屏幕已经大了好多，但是你的大脑可没有。屏幕其余空白的地方可以用来进行分列显示，你的编辑器应该支持分割screen的，对吧？（TextMate好像不支持，ft…… 译者怨念中……）</p>
<h3>引号</h3>
<p>除非你在写JOSN，不然请使用单引号。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bar'</span><span style="color: #339933;">;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">;</span></div></div>
<h3>大括号</h3>
<p>你的前大括号应该放在和前面最近的那句语句的同一行上。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'winning'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'losing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>同时请留意一下，上面条件语句的前后都留有空格。</p>
<h3>变量声明</h3>
<p>每句var语句定义一个变量，这样会便于之后各行的重新排序。不一定非要把var定义在函数的最上面，<a href="http://javascript.crockford.com/code.html" title="Crockford" target="_blank">Crockford</a>那里曾强调过这点，但我们这里可以忽略，你觉得变量定义在哪里更说的通就定义在哪里。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">23</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">42</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> object <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>items.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> keys.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; object<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> values.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; values <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">23</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">42</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; object <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; key<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>items.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; key <span style="color: #339933;">=</span> keys.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; object<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> values.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>变量名和属性名</h3>
<p>变量和属性名应该使用小驼峰式命名法（就是新单词的首个字母要大写，其余均小写，详见<a href="http://zh.wikipedia.org/wiki/%E9%A7%9D%E5%B3%B0%E5%BC%8F%E5%A4%A7%E5%B0%8F%E5%AF%AB" title="駝峰式大小寫" target="_blank">wiki</a>，译者注）。它们还得有描述性。通常需要避免使用单个字符的变量或者不常用的缩写来作为变量或者属性名。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> adminUser <span style="color: #339933;">=</span> db.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SELECT * FROM users ...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> admin_user <span style="color: #339933;">=</span> d.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SELECT * FROM users ...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>类名</h3>
<p>类名应该使用<a href="http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms" target="_blank">大驼峰式命名法</a>。</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> BankAccount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> bank_Account<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>常量</h3>
<p>常量应该被定义为普通变量或者静态的类属性，且应该全部采用大写字母。</p>
<p>Node.js / V8 实际上支持mozilla的const扩展，但是不幸的是这并不能应用到类的成员变量上（译者水平太搓，本句翻译着实有待考证，所以同时附上英文原文，待译者有空去查阅下mozilla的const extension究竟是啥么东东再做分解……），况且这也不是ECMA标准的一部分。<br />
Node.js / V8 actually supports mozilla&#8217;s const extension, but unfortunately that cannot be applied to class members, nor is it part of any ECMA standard.</p>
<p>正确：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> SECOND <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> File<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span><br />
File.<span style="color: #660066;">FULL_PERMISSIONS</span> <span style="color: #339933;">=</span> 0777<span style="color: #339933;">;</span></div></div>
<p>错误：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">const</span> SECOND <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> File<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span><br />
File.<span style="color: #660066;">fullPermissions</span> <span style="color: #339933;">=</span> 0777<span style="color: #339933;">;</span></div></div>
<p>未完待续……</p>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/" title="Felix的Node.js初学者入门指南 (October 26, 2011)">Felix的Node.js初学者入门指南</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2011/10/felix%e2%80%99s-node-js-style-guide-part-2/" title="Felix’s Node.js Style Guide (Part 2) (October 28, 2011)">Felix’s Node.js Style Guide (Part 2)</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/06/drink-coffee-then-write-coffeescript/" title="喝coffee写coffeescript (June 7, 2011)">喝coffee写coffeescript</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2011/05/live-fortune-node-js-that-do-not-crash/" title="Live Fortune (Node.js) that do not crash (May 31, 2011)">Live Fortune (Node.js) that do not crash</a> (18)</li>
	<li><a href="http://blog.samsonis.me/2011/06/deploying-live-fortune-node-js-with-nginx-and-monit/" title="Deploying Live Fortune (Node.js) with Nginx and Monit (June 1, 2011)">Deploying Live Fortune (Node.js) with Nginx and Monit</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/10/felixs-node-js-style-guide-part-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Felix的Node.js初学者入门指南</title>
		<link>http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/</link>
		<comments>http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 15:49:26 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3551</guid>
		<description><![CDATA[本文为译文，翻译自原文 http://nodeguide.com/beginner.html 网上有很多关于node.js的资料，但鉴于其快速的开发进度，初学者可能会很难找到比较好且最新的入门资料。本文就是在力求提供这份资料，并且力求时刻和最新的node.js稳定版本保持同步。 本guide已经针对node 0.4.x的最新改动做过更新，node 0.4.x也是当前node的稳定分支。 学习JavaScript 本guide假设你已经对JavaScript有所熟悉。如果你不是很熟悉，你可以去读读：Marijn Haverbeke写的Eloquent JavaScript，它可以在网上免费阅读。 Hello World Tutoriali 本tutorial会指导你如何安装node.js，其中包括构建一个简单的所谓“hello world”的http服务器。 安装 首先：当前为了使用node.js，你还得有一台运行着*nix操作系统的电脑。虽然推荐使用Linux和OSX，但想在FreeBSD或cygwin（windows上）上跑node.js应该也不是什么不可能的事情。目前，一个完整的windows版本的nodejs移植正在如火如荼的进行之中，只是还尚未到可以公开给public使用的程度。 常见的安装node.js的方法是从源代码直接编译。不同的包管理系统，催生出目前所存在的很多种不同的安装包，由于它们各自的更新频率又各有不同，我还是推荐从源代码安装。 你可以在nodejs.org获得最新的源代码。使用下面的命令可以下载并安装v0.4.4版本的node.js。 $ wget http://nodejs.org/dist/node-v0.4.4.tar.gz $ tar -xzf node-v0.4.4.tar.gz $ cd node-v0.4.4.tar.gz $ ./configure $ sudo make install Node.js本身除了依赖一些常用的构建工具（依赖python是因为构建系统本身需要）并没有别的更多的外部依赖。在OSX上，需要先安装Xcode才能确保安装成功，在Ubuntu上你则可能需要运行： $ apt-get -y install build-essential 交互式的node.js shell 如果一切正常，你应该可以像下面这样开始调用交互式的node.js shell了： $ node &#62; console.log&#40;'Hello World'&#41;; Hello World 这个交互式的shell（也被称为REPL）不仅是个可以测试简单如一行的代码的好地方，也可以被直接嵌入到你的node.js应用程序里面。如果想退出，只要按下Ctrl + C即可。 ...]]></description>
			<content:encoded><![CDATA[<p>本文为译文，翻译自原文 <a title="Felix's Node.js Beginners Guide" href="http://nodeguide.com/beginner.html" target="_blank">http://nodeguide.com/beginner.html<br />
</a><br />
网上有很多关于node.js的资料，但鉴于其快速的开发进度，初学者可能会很难找到比较好且最新的入门资料。本文就是在力求提供这份资料，并且力求时刻和最新的node.js稳定版本保持同步。</p>
<p>本guide已经针对node 0.4.x的最新改动做过更新，node 0.4.x也是当前node的稳定分支。</p>
<h3>学习JavaScript</h3>
<p>本guide假设你已经对JavaScript有所熟悉。如果你不是很熟悉，你可以去读读：<a href="http://twitter.com/marijnjh" target="_blank">Marijn Haverbeke</a>写的<a href="http://eloquentjavascript.net/" title="Eloquent JavaScript" target="_blank">Eloquent JavaScript</a>，它可以在网上免费阅读。<br />
<span id="more-3551"></span></p>
<h3>Hello World Tutorial<sup><a href="http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/#footnote_0_3551" id="identifier_0_3551" class="footnote-link footnote-identifier-link" title="这个我就不翻了吧。。。">i</a></sup></h3>
<p>本tutorial会指导你如何安装node.js，其中包括构建一个简单的所谓“hello world”的http服务器。</p>
<h4>安装</h4>
<p>首先：当前为了使用node.js，你还得有一台运行着*nix操作系统的电脑。虽然推荐使用Linux和OSX，但想在FreeBSD或cygwin（windows上）上跑node.js应该也不是什么不可能的事情。目前，一个完整的windows版本的nodejs移植正在如火如荼的进行之中，只是还尚未到可以公开给public使用的程度。</p>
<p>常见的安装node.js的方法是从源代码直接编译。不同的包管理系统，催生出目前所存在的很多种不同的安装包，由于它们各自的更新频率又各有不同，我还是推荐从源代码安装。</p>
<p>你可以在<a href="http://nodejs.org/" title="nodejs.org" target="_blank">nodejs.org</a>获得最新的源代码。使用下面的命令可以下载并安装v0.4.4版本的node.js。</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>nodejs.org<span style="color: #000000; font-weight: bold;">/</span>dist<span style="color: #000000; font-weight: bold;">/</span>node-v0.4.4.tar.gz<br />
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzf</span> node-v0.4.4.tar.gz<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> node-v0.4.4.tar.gz<br />
$ .<span style="color: #000000; font-weight: bold;">/</span>configure<br />
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>Node.js本身除了依赖一些常用的构建工具（依赖python是因为构建系统本身需要）并没有别的更多的外部依赖。在OSX上，需要先安装Xcode才能确保安装成功，在Ubuntu上你则可能需要运行：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #660033;">-y</span> <span style="color: #c20cb9; font-weight: bold;">install</span> build-essential</div></div>
<h4>交互式的node.js shell</h4>
<p>如果一切正常，你应该可以像下面这样开始调用交互式的node.js shell了：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ node<br />
<span style="color: #000000; font-weight: bold;">&gt;</span> console.log<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'Hello World'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
Hello World</div></div>
<p>这个交互式的shell（也被称为<a href="http://en.wikipedia.org/wiki/REPL" title="REPL" target="_blank">REPL</a>）不仅是个可以测试简单如一行的代码的好地方，也可以被直接<a href="http://nodejs.org/docs/v0.4.4/api/repl.html" target="_blank">嵌入</a>到你的node.js应用程序里面。如果想退出，只要按下Ctrl + C即可。</p>
<p>这个REPL还自带了很多非常好的功能，比如最重要的tab补全功能。</p>
<h4>你的第一个程序</h4>
<p>简单的说，编写一个node.js程序就是创建一个以&#8217;.js&#8217;扩展名结尾的文件。比如你可以用下面的内容创建一个简单的&#8217;hello_world.js&#8217;文件：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>保存文件后，你可以像下面这样在终端里面执行它：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ node hello.js<br />
Hello World</div></div>
<h4>&#8220;hello world&#8221; http服务器</h4>
<p>只在终端里面打印hello world并没有那么振奋人心，让我们更进一步来编写一个通过http协议来返回&#8217;hello world&#8217;的程序。我们把这个程序命名为&#8217;hello_http.js&#8217;，这个文件包含以下的内容：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> server <span style="color: #339933;">=</span> http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello Http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
server.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8080</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>现在让我们来运行它，在终端里面敲入：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ node hello_http.js</div></div>
<p>首先注意到的是，不同于我们的第一个程序，这个程序不会马上退出。这是因为node程序会一直运行，直到它确认不再会有更多的事件发生后才会退出。就我们这个case来讲，那个一直开放着的http服务器就可以源源不断的产生事件，以保持程序继续运行。</p>
<p>测试这个服务器非常简单，只要打开一个新的浏览器tab，访问：http://localhost:8080/ 即可。事遂人愿，你应该可以在浏览器页面上看到那个包含字符串&#8217;Hello Http&#8217;的服务器应答。</p>
<p>或者，你也可以尝试在终端里面使用curl来测试你的服务器：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ curl localhost:<span style="color: #000000;">8080</span><br />
Hello Http</div></div>
<p>下面让我们来深入看看我们的这个小程序里面所包含的各个步骤。在第一行，我们include了http核心模块，并把它赋值给了一个叫做http的变量。在后面的section，我们还会提到更多有关模块系统的信息。</p>
<p>然后我们定义了一个变量server，并把http.createServer的返回值赋给它。传给http.createServer的参数是个闭包，每当有新的http请求过来的时候，这个闭包都会被调用。</p>
<p>最后我们调用server.listen(8080)，以告诉node.js我们想让我们的服务器监听在哪个端口。如果想监听80端口，你的程序需要以root权限来运行。</p>
<p>现在你在浏览器里面访问&#8217;localhost:8080&#8242;，那个connection闭包就会被调用，调用的同时系统还会传入一个req和一个res对象。这个req对象是个可读的流，每当有部分数据从客服端过来的时候，它便会产生一个&#8217;data&#8217;事件（就像表单提交或者文件上传）。res对象则是个可写的流，被用来向客户端发回数据。在上面那个例子里，我们只是简单的返回了200 OK的header以及&#8217;Hello Http&#8217;的body内容。</p>
<h3>模块系统</h3>
<p>node.js提供了一个简单的模块系统，以便于我们组织程序，把各部分功能分开放到不同的文件里面。</p>
<p>让我们创建一个叫&#8217;main.js&#8217;的文件以示说明，文件内容如下：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> hello <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
hello.<span style="color: #660066;">world</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>你可能已经猜到，那句require(&#8216;./hello&#8217;)便是用来加载其它JavaScript文件的内容的。开头的&#8217;./&#8217;表示这个将要被加载的文件就在&#8217;main.js&#8217;所在的目录下面。另外值得注意的是，我们并不需要提供文件扩展名，因为&#8217;.js&#8217;就是默认的后缀。</p>
<p>接下来让我们来创建这个将被加载的&#8217;hello.js&#8217;文件，内容如下：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exports.<span style="color: #660066;">world</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>这儿值得注意的是，我们给一个叫做&#8217;exports&#8217;的对象赋上了一个叫&#8217;world&#8217;的属性。这样的一个&#8217;exports&#8217;对象在各个模块里面都存在，每当调用require函数来include一个模块时，这个&#8217;exports&#8217;对象就会被当作返回值返回。如果我们现在运行&#8217;main.js&#8217;，应该会看到意料中的输出：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ node main.js<br />
Hello World</div></div>
<p>有一点值得提一下，现在很多的node用户都喜欢像下面这样直接修改覆盖exports对象：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// ...</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>如你所料，这样的改写会使require调用直接返回上面所赋值的function。这在你使用<a href="http://nodeguide.com/object_oriented_programming.html" title="object oriented programming" target="_blank">面向对象编程</a>时变的非常有用，因为每个文件的exports便是那个类的构造函数。</p>
<p>接下了你需要知道的是，如果require调用里面不包含所include文件是相对地址的暗示时，模块系统又是怎么处理的。比如：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>在这种情况下，node.js会首先查看一下是不是有一个叫作http的核心模块存在，如果存在（http核心模块确实存在）便直接返回。但是如果不存在这样的核心模块呢，比如说&#8217;mysql&#8217;？</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> mysql <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mysql'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>这种情况下，node.js会遍历这个目录树，从里到外依次遍历各个父目录，检查是否有个叫&#8217;node_modules&#8217;的目录存在。如果找到这样命名的目录，node.js便会在这个目录下查找一个叫&#8217;mysql.js&#8217;的文件。如果很不幸没有找到，并且已经遍历到达根目录&#8217;/'，node.js只能放弃并抛出异常。</p>
<p>当前，除了各级父目录，node.js查找模块文件的时候还会遍历另外一个目录列表里存放的所有目录，这个目录列表可以通过require.paths访问和修改。然而，关于要不要移除这个功能，目前正有着激烈的讨论，所以你可能最好还是忽略它比较好。</p>
<p>最后但也同样重要的是，node.js也支持你创建一个叫&#8217;index.js&#8217;的文件，以作为一个目录的主include文件。也就是说，如果你调用require(&#8216;./foo&#8217;)，&#8217;foo.js&#8217;文件和foo目录下的index.js文件&#8217;foo/index.js&#8217;都会被检索到，在不是相对路径的include调用里也是如此。</p>
<h3>使用EventEmitters</h3>
<p>Node.js以一个叫EventEmitter的类实现了<a href="http://en.wikipedia.org/wiki/Observer_pattern" title="Observer Pattern" target="_blank">观察者模式</a>。每当有对象会发出或产生出着各种不同事件的时候，node.js经常让这些对象背后的类继承EventEmitter。</p>
<p>使用EventEmitter还是相当直观的。你可以通过调用对象的&#8217;on()&#8217;函数来监听某个特定事件，&#8217;on()&#8217;函数需要你提供这个事件的名字，以及一个回调的闭包以作为参数。比如：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span><br />
req<br />
&nbsp; .<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>chunk<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; data <span style="color: #339933;">+=</span> chunk<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'end'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'POST data: %s'</span><span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></div>
<p>如你所见，on()函数会同时返回自己所属的对象的引用，这可以方便你把很多这样的事件监听调用串联起来而不是写多行调用。（和jquery一样，译者注……）</p>
<p>如果你只是想在第一次事件发生时有所动作，你还可以使用once()函数。</p>
<p>最后，通过调用removeListener函数，你还可以移除某个事件监听者。请注意这个函数的参数是你正想要移除的回调函数的引用，而不是这个事件的名字：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> onData <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>chunk<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>chunk<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; req.<span style="color: #660066;">removeListener</span><span style="color: #009900;">&#40;</span>onData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
req.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #339933;">,</span> onData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>上面的例子本质上和once()函数是相同的。</p>
<h3>下一步</h3>
<p>现在你已经知道了node.js的基础，你可能最好开始自己写一些小的程序。<a href="http://nodejs.org/docs/v0.4.4/api/" title="node's api documentation" target="_blank">node的api文档</a>是个开始的好地方，它可以被用来作为你灵感的源泉。</p>
<h3>调试node.js程序</h3>
<p>有很多种方法可以用来调试node.js应用程序。我个人崇尚越少的调试越好，所以我严格的遵循<a href="http://nodeguide.com/test_driven_development.html" title="test driven development" target="_blank">测试驱动开发</a>的建议。</p>
<p>不过话说回来，如果在现有已经存在的应用程序里面定位调试某个很tricky的bug的时候，还是有一些方法可以帮到你的。</p>
<h4>使用console.log()</h4>
<p>理解一个问题最简单粗暴的办法就是使用console.log()来查看这个对象。你可以直接将这个对象作为参数传入：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>bar<span style="color: #339933;">:</span> <span style="color: #3366CC;">'foobar'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>也可以使用类似sprintf()的的格式化方法来格式化debug输出：</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>bar<span style="color: #339933;">:</span> <span style="color: #3366CC;">'foobar'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello %s, this is my object: %j'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'World'</span><span style="color: #339933;">,</span> foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h4>使用node的debugger</h4>
<p>如果你不是很喜欢console.log()（觉得它太简单粗暴，译者注……译者怎么这么烦……），或者你觉得你的问题使用断点调试会更有助于你分析时，node自带的debugger是个很好的选择。调用这个debugger只需要：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ node.js debug my_file.js</div></div>
<p>还在编辑中，请稍后再来……（广告很短，请不要离开……）</p>
<h4>使用WebKit Inspector</h4>
<p>还在编辑中，请稍后再来……（这就是译文啊，大哥，我容易么我……）</p>
<h3>框架</h3>
<p>如果你只是刚刚接触node.js，到了需要解析POST请求，处理url路由或者页面渲染的时候你可能并不想再重造轮子。如果是这样的话，你可能会考虑使用一些比较流行的web框架。这个section会对这些流行的选择做一个简单的概述，并且附上我的看法。</p>
<h4>Express</h4>
<p>当前，<a href="http://expressjs.com/" title="express" target="_blank">express</a>可能是很多node.js开发人员的不二选择。它相对成熟，而且包括了<a href="https://github.com/senchalabs/connect" title="connect" target="_blank">connect</a>（联想一下rack）中间件。这个框架自带了url路由，配置选项，模板引擎，POST请求解析和其它很多功能。</p>
<p>虽说express是个可靠的框架，但相对于Rails，CakePHP或者Django来说，它目前的规模还是相对较小。从这点来看它更接近于Sinatra，不走运的是，它似乎并没有花很大的力气来以区别于它的Ruby前辈们所做的事情，所以在JavaScript里面使用的时候还是让人感觉不是十分自然。当然不管怎么，目前除了重造轮子编写你自己的框架以外，它绝对是个很好的选择。</p>
<h4>fab.js</h4>
<p>你觉得你懂JavaScript？再想想。最初受到jQuery的串式调用的启发，fabs.js走了一条非常不同寻常的路（美特斯邦威啊。。译者注），它对JavaScript所做的各做扭曲改动甚至超出了很多人可以理解的范围。每个函数会返回另外一个函数，完全免去了方法或者函数名称的必要性，这种做法的产物便是各种感官上都很像lisp的代码。</p>
<p>目前我并不认为fab.js完全适用于生产环境，当然如果你还在探索node.js的世界中，你绝对应该尝试哪怕一次。不说别的，fab.js至少展示了在web框架领域，JavaScript不一定非要复制Ruby，Python和PHP，它完全可以有它自己独特的路线。（那就是美特斯邦威……译者那啥……）</p>
<h3>主机托管和部署</h3>
<h4>快速简单部署</h4>
<p>如果你只是刚开始编写node.js应用，并且你想尽快让它娘的跑起来，可以这样做：</p>
<p>1. 将你的程序copy到要准备部署的服务器。如果你使用git，这可能仅仅就是从另外一台服务器或者像<a href="http://github.com/" title="GitHub" target="_blank">GitHub</a>这样的服务那里clone一个repository。</p>
<p>2. 假设你的项目中包含一个叫&#8217;server.js&#8217;的文件，进入包含这个文件的目录，并输入：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">screen</span><br />
$ node server.js</div></div>
<p>这会在一个所谓的screen会话中启动你的&#8217;server.js&#8217;程序。Screen可以让你的shell拥有保持当前状态的功能，即便你退出了那个你用来登陆服务器的终端，这种状态仍旧会被保持。</p>
<p>所以你可以安全的关闭你的终端程序，而你的&#8217;server.js&#8217;则会继续运行。如果想继续监控其运行状态，你可以重新登陆服务器，输入：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #660033;">-r</span></div></div>
<p>这会重新让你回到那个一直在背后运行你的程序的shell终端上。</p>
<p>当然，这个方法只被推荐用来进行试验性质的部署。如果你的node应用在某个时刻突然崩溃，screen是不会试图去重启它的，所以不要试图用这个方法在生产环境上部署应用。</p>
<h4>Joyent no.de</h4>
<p>还在编辑中，请稍后再来……（幸亏人家还在编辑中，不然偶要挂掉了……）</p>
[Footnotes]<ol class="footnotes"><li id="footnote_0_3551" class="footnote">这个我就不翻了吧。。。</li></ol>
	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2011/10/felix%e2%80%99s-node-js-style-guide-part-2/" title="Felix’s Node.js Style Guide (Part 2) (October 28, 2011)">Felix’s Node.js Style Guide (Part 2)</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/10/felixs-node-js-style-guide-part-1/" title="Felix&#8217;s Node.js Style Guide (Part 1) (October 27, 2011)">Felix&#8217;s Node.js Style Guide (Part 1)</a> (4)</li>
	<li><a href="http://blog.samsonis.me/2011/06/drink-coffee-then-write-coffeescript/" title="喝coffee写coffeescript (June 7, 2011)">喝coffee写coffeescript</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2011/05/live-fortune-node-js-that-do-not-crash/" title="Live Fortune (Node.js) that do not crash (May 31, 2011)">Live Fortune (Node.js) that do not crash</a> (18)</li>
	<li><a href="http://blog.samsonis.me/2011/06/deploying-live-fortune-node-js-with-nginx-and-monit/" title="Deploying Live Fortune (Node.js) with Nginx and Monit (June 1, 2011)">Deploying Live Fortune (Node.js) with Nginx and Monit</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/10/felixs-node-js-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL调优和SQL优化</title>
		<link>http://blog.samsonis.me/2011/10/sql-tuning-or-sql-optimization/</link>
		<comments>http://blog.samsonis.me/2011/10/sql-tuning-or-sql-optimization/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 07:11:31 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Share]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3523</guid>
		<description><![CDATA[本文为译文i，翻译自原文 http://beginner-sql-tutorial.com/sql-query-tuning.htm sql语句可以用来从数据库取回数据。不同的sql查询语句有时可以得到相同的返回结果。如果考虑执行效率的话，选择使用最佳的查询语句至关重要，所以我们需要根据需求来优化sql查询。下面是一些我们平时经常使用的查询语句，以及如何优化它们以达到更好的执行效率。 SQL调优/SQL优化技巧: 1) 如果在SELECT语句里面用具体的column名而不是&#8217;*'，sql查询会更快。 例如，查询语句写成： SELECT id, first_name, last_name, age, subject FROM student_details; 比写成下面这样快： SELECT * FROM student_details; 2) HAVING字句是用来进一步过滤SELECT查询结果的，它就像一个过滤器。别把HAVING用作其它任何用途。 例如，写成： SELECT subject, count&#40;subject&#41; FROM student_details WHERE subject != 'Science' AND subject != 'Maths' GROUP BY subject; 而不是: SELECT subject, count&#40;subject&#41; FROM student_details GROUP BY subject HAVING subject!= 'Vancouver' AND subject!= 'Toronto'; 3) ...]]></description>
			<content:encoded><![CDATA[<p>本文为译文<sup><a href="http://blog.samsonis.me/2011/10/sql-tuning-or-sql-optimization/#footnote_0_3523" id="identifier_0_3523" class="footnote-link footnote-identifier-link" title="翻译的主要目的在于强化记忆和加深理解">i</a></sup>，翻译自原文 <a href="http://beginner-sql-tutorial.com/sql-query-tuning.htm" title="SQL Tuning or SQL Optimization" target="_blank">http://beginner-sql-tutorial.com/sql-query-tuning.htm</a></p>
<p>sql语句可以用来从数据库取回数据。不同的sql查询语句有时可以得到相同的返回结果。如果考虑执行效率的话，选择使用最佳的查询语句至关重要，所以我们需要根据需求来优化sql查询。下面是一些我们平时经常使用的查询语句，以及如何优化它们以达到更好的执行效率。</p>
<p><strong>SQL调优/SQL优化技巧:</strong></p>
<p><strong>1) 如果在SELECT语句里面用具体的column名而不是&#8217;*'，sql查询会更快。</strong></p>
<p>例如，查询语句写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> last_name<span style="color: #66cc66;">,</span> age<span style="color: #66cc66;">,</span> subject <span style="color: #993333; font-weight: bold;">FROM</span> student_details;</div></div>
<p>比写成下面这样快：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> student_details;</div></div>
<p><span id="more-3523"></span><br />
<strong>2) HAVING字句是用来进一步过滤SELECT查询结果的，它就像一个过滤器。别把HAVING用作其它任何用途。</strong></p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> subject<span style="color: #66cc66;">,</span> count<span style="color: #66cc66;">&#40;</span>subject<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> subject !<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Science'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> subject !<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Maths'</span><br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> subject;</div></div>
<p>而不是:</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> subject<span style="color: #66cc66;">,</span> count<span style="color: #66cc66;">&#40;</span>subject<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> subject<br />
<span style="color: #993333; font-weight: bold;">HAVING</span> subject!<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Vancouver'</span> <span style="color: #993333; font-weight: bold;">AND</span> subject!<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Toronto'</span>;</div></div>
<p><strong>3) 有时候在主查询语句里面可能包含不止一个子查询语句。尽量减少子查询语句的数量有助于提高效率。</strong></p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>salary<span style="color: #66cc66;">,</span> age <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MAX <span style="color: #66cc66;">&#40;</span>salary<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> MAX <span style="color: #66cc66;">&#40;</span>age<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee_details<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> dept <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Electronics'</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> salary <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MAX<span style="color: #66cc66;">&#40;</span>salary<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> employee_details<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> age <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MAX<span style="color: #66cc66;">&#40;</span>age<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> employee_details<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> emp_dept <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Electronics'</span>;</div></div>
<p><strong>4) 恰当的使用EXISTS，IN和table joins。</strong></p>
<p>a) IN通常效率最低<br />
b) 当大多数过滤条件存在在子查询语句里面的时候，IN效率更高<br />
c) 当大多数过滤条件存在在主查询语句的时候，EXISTS效率更高</p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> product p<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> order_items o<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span>product_id <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>product_id<span style="color: #66cc66;">&#41;</span></div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> product p<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> product_id <span style="color: #993333; font-weight: bold;">IN</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> product_id <span style="color: #993333; font-weight: bold;">FROM</span> order_items</div></div>
<p><strong>5) 当join的两个表包含一对多关系时，优先使用EXISTS而不是DISTINCT。</strong></p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> d<span style="color: #66cc66;">.</span>dept_id<span style="color: #66cc66;">,</span> d<span style="color: #66cc66;">.</span>dept<br />
<span style="color: #993333; font-weight: bold;">FROM</span> dept d<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'X'</span> <span style="color: #993333; font-weight: bold;">FROM</span> employee e <span style="color: #993333; font-weight: bold;">WHERE</span> e<span style="color: #66cc66;">.</span>dept <span style="color: #66cc66;">=</span> d<span style="color: #66cc66;">.</span>dept<span style="color: #66cc66;">&#41;</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span> d<span style="color: #66cc66;">.</span>dept_id<span style="color: #66cc66;">,</span> d<span style="color: #66cc66;">.</span>dept<br />
<span style="color: #993333; font-weight: bold;">FROM</span> dept d<span style="color: #66cc66;">,</span>employee e<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> e<span style="color: #66cc66;">.</span>dept <span style="color: #66cc66;">=</span> e<span style="color: #66cc66;">.</span>dept;</div></div>
<p><strong>6) 尝试使用UNION ALL来替换UNION。</strong></p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details_class10<br />
<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> sports_team;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> subject<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details_class10<br />
<span style="color: #993333; font-weight: bold;">UNION</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> sports_team;</div></div>
<p><strong>7) 当在WHERE子句里面使用条件判断时，要小心。</strong></p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age <span style="color: #993333; font-weight: bold;">FROM</span> student_details <span style="color: #993333; font-weight: bold;">WHERE</span> age <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">10</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age <span style="color: #993333; font-weight: bold;">FROM</span> student_details <span style="color: #993333; font-weight: bold;">WHERE</span> age !<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>;</div></div>
<p>写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> first_name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'Chan%'</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> SUBSTR<span style="color: #66cc66;">&#40;</span>first_name<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Cha'</span>;</div></div>
<p>写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> first_name <span style="color: #993333; font-weight: bold;">LIKE</span> NVL <span style="color: #66cc66;">&#40;</span> :name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'%'</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> first_name <span style="color: #66cc66;">=</span> NVL <span style="color: #66cc66;">&#40;</span> :name<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">&#41;</span>;</div></div>
<p>写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> product_id<span style="color: #66cc66;">,</span> product_name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> product<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> unit_price <span style="color: #993333; font-weight: bold;">BETWEEN</span> MAX<span style="color: #66cc66;">&#40;</span>unit_price<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span> MIN<span style="color: #66cc66;">&#40;</span>unit_price<span style="color: #66cc66;">&#41;</span></div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> product_id<span style="color: #66cc66;">,</span> product_name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> product<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> unit_price <span style="color: #66cc66;">&gt;=</span> MAX<span style="color: #66cc66;">&#40;</span>unit_price<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> unit_price <span style="color: #66cc66;">&lt;=</span> MIN<span style="color: #66cc66;">&#40;</span>unit_price<span style="color: #66cc66;">&#41;</span></div></div>
<p>写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> salary<br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> dept <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Electronics'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> location <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Bangalore'</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> salary<br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> dept <span style="color: #66cc66;">||</span> location<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ElectronicsBangalore'</span>;</div></div>
<p>把非column表达式放在查询语句的一边，因为它们会被优先处理。</p>
<p>写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> salary<br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> salary <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">25000</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> salary<br />
<span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> salary <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">10000</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">35000</span>;</div></div>
<p>写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> age <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">10</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> first_name<span style="color: #66cc66;">,</span> age<br />
<span style="color: #993333; font-weight: bold;">FROM</span> student_details<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> age <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>;</div></div>
<p><strong>8) 使用DECODE来避免重复扫描同一条数据或重复join同一张表。DECODE也可以用来替换GROUP BY或ORDER BY子句。</strong></p>
<p>例如，写成：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'Ramesh%'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> location <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Bangalore'</span>;</div></div>
<p>而不是：</p>
<div class="codecolorer-container sql railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> DECODE<span style="color: #66cc66;">&#40;</span>location<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Bangalore'</span><span style="color: #66cc66;">,</span>id<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> id <span style="color: #993333; font-weight: bold;">FROM</span> employee<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'Ramesh%'</span>;</div></div>
<p><strong>9) 存储大二进制数据，首先将其存储在文件系统上，然后将其文件路径存储到数据库。</strong></p>
<p><strong>10) 遵守SQL标准规则。</strong></p>
<p>a) 对所有的SQL动词使用同一种大小写<br />
b) 所有的SQL动词放在新一行的开头<br />
c) 所有的单词用一个空格分隔<br />
d) 所有的SQL动词左对齐或右对齐首个SQL动词</p>
[Footnotes]<ol class="footnotes"><li id="footnote_0_3523" class="footnote">翻译的主要目的在于强化记忆和加深理解</li></ol>
	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/08/%e8%af%b4%e8%b0%8e/" title="说谎 (August 9, 2010)">说谎</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e8%8a%82%e9%80%89%e4%b8%a4%e6%ae%b5%e5%a5%a5%e5%b7%b4%e9%a9%ac2011%e5%9b%bd%e6%83%85%e5%92%a8%e6%96%87/" title="节选两段奥巴马2011国情咨文 (January 26, 2011)">节选两段奥巴马2011国情咨文</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2010/07/%e7%8b%ae%e5%ad%90%e5%ba%a7/" title="狮子座 (July 31, 2010)">狮子座</a> (16)</li>
	<li><a href="http://blog.samsonis.me/2010/02/%e6%9c%aa%e6%88%90%e5%b9%b4%e5%85%88%e5%88%ab%e5%84%aa/" title="未成年先别優 (February 23, 2010)">未成年先别優</a> (1)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e6%8e%a8%e8%8d%90%e4%b8%80%e4%b8%aareader-app%e5%92%8c%e4%b8%80%e4%b8%aagreasemonkey%e8%84%9a%e6%9c%ac/" title="推荐一个reader app和一个Greasemonkey脚本 (January 26, 2011)">推荐一个reader app和一个Greasemonkey脚本</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/10/sql-tuning-or-sql-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XXX != 3X</title>
		<link>http://blog.samsonis.me/2011/10/xxx-3x/</link>
		<comments>http://blog.samsonis.me/2011/10/xxx-3x/#comments</comments>
		<pubDate>Wed, 19 Oct 2011 13:11:00 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Minds]]></category>
		<category><![CDATA[Share]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3515</guid>
		<description><![CDATA[我记得去年还是前年什么时候广电总局开始禁止媒体使用XXX等外语缩略词，但似乎这几年还是频繁听到CPI，GDP等不得不逃避的敏感词，要不是美职篮停摆，估计NBA还是到处可见。 当然本文不是来讨论这个的…… 本文只是为了列举加收集本人没事断章取义望文生义出来的缩略词另解 i，并且会持续更新。 VPN = Very Poor Network SSD = Super Stupid Disk NBA = National BadAss SSO = Super Stupid Opinion TBD …… [Footnotes]皆原创 Related posts 说谎 (0) 节选两段奥巴马2011国情咨文 (0) 狮子座 (16) 未成年先别優 (1) 推荐一个reader app和一个Greasemonkey脚本 (2)]]></description>
			<content:encoded><![CDATA[<p>我记得去年还是前年什么时候广电总局开始禁止媒体使用XXX等外语缩略词，但似乎这几年还是频繁听到CPI，GDP等不得不逃避的敏感词，要不是美职篮停摆，估计NBA还是到处可见。</p>
<p>当然本文不是来讨论这个的……</p>
<p>本文只是为了列举加收集本人没事断章取义望文生义出来的缩略词另解 <sup><a href="http://blog.samsonis.me/2011/10/xxx-3x/#footnote_0_3515" id="identifier_0_3515" class="footnote-link footnote-identifier-link" title="皆原创">i</a></sup>，并且会持续更新。</p>
<p>VPN = <span style="color: #A0410D;">V</span>ery <span style="color: #A0410D;">P</span>oor <span style="color: #A0410D;">N</span>etwork<br />
SSD = <span style="color: #A0410D;">S</span>uper <span style="color: #A0410D;">S</span>tupid <span style="color: #A0410D;">D</span>isk<br />
NBA = <span style="color: #A0410D;">N</span>ational <span style="color: #A0410D;">B</span>ad<span style="color: #A0410D;">A</span>ss<br />
SSO = <span style="color: #A0410D;">S</span>uper <span style="color: #A0410D;">S</span>tupid <span style="color: #A0410D;">O</span>pinion</p>
<p>TBD ……</p>
[Footnotes]<ol class="footnotes"><li id="footnote_0_3515" class="footnote">皆原创</li></ol>
	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/08/%e8%af%b4%e8%b0%8e/" title="说谎 (August 9, 2010)">说谎</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e8%8a%82%e9%80%89%e4%b8%a4%e6%ae%b5%e5%a5%a5%e5%b7%b4%e9%a9%ac2011%e5%9b%bd%e6%83%85%e5%92%a8%e6%96%87/" title="节选两段奥巴马2011国情咨文 (January 26, 2011)">节选两段奥巴马2011国情咨文</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2010/07/%e7%8b%ae%e5%ad%90%e5%ba%a7/" title="狮子座 (July 31, 2010)">狮子座</a> (16)</li>
	<li><a href="http://blog.samsonis.me/2010/02/%e6%9c%aa%e6%88%90%e5%b9%b4%e5%85%88%e5%88%ab%e5%84%aa/" title="未成年先别優 (February 23, 2010)">未成年先别優</a> (1)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e6%8e%a8%e8%8d%90%e4%b8%80%e4%b8%aareader-app%e5%92%8c%e4%b8%80%e4%b8%aagreasemonkey%e8%84%9a%e6%9c%ac/" title="推荐一个reader app和一个Greasemonkey脚本 (January 26, 2011)">推荐一个reader app和一个Greasemonkey脚本</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/10/xxx-3x/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>天与弗取 反受其咎</title>
		<link>http://blog.samsonis.me/2011/09/%e5%a4%a9%e4%b8%8e%e5%bc%97%e5%8f%96-%e5%8f%8d%e5%8f%97%e5%85%b6%e5%92%8e/</link>
		<comments>http://blog.samsonis.me/2011/09/%e5%a4%a9%e4%b8%8e%e5%bc%97%e5%8f%96-%e5%8f%8d%e5%8f%97%e5%85%b6%e5%92%8e/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 07:14:32 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3448</guid>
		<description><![CDATA[天与弗取，反受其咎；时至不行，反受其殃 —— 《史记》 时不我待，终流于庸俗，鄙人也终干上了之前一直所为之不屑的放广告的勾当，这当然算不上什么出卖灵魂的事情，况且违心的事情我几乎每天都在干 i，就是一大俗人，没有必要也装不了清高；生活就像被强奸，反抗不得还不如闭眼好好享受吧； 广告永远是需要包装的，聪明的你也许已经发现，上篇文章 内容空泛，言不着意，却为了隐藏那一个 hyperlink 煞费苦心；标题曾特地包含的AD两字最终亦被要求remove，估计人家还想嘲笑我难道你连AD从来不说自己是AD这样简单的道理都不懂？ …… anyway，我已经开始享受了…… [Footnotes]汗一把…… Related posts 未成年先别優 (1) 不支持multiple playlists的player不是好player (0) Update 55: 核去核从 (2) Update 38: 人不可以貌相 (6) StarCraft II配不在高可玩就行 (8)]]></description>
			<content:encoded><![CDATA[<blockquote><p>
天与弗取，反受其咎；时至不行，反受其殃</p>
<p>—— 《史记》
</p></blockquote>
<p>时不我待，终流于庸俗，鄙人也终干上了之前一直所为之不屑的放广告的勾当，这当然算不上什么出卖灵魂的事情，况且违心的事情我几乎每天都在干 <sup><a href="http://blog.samsonis.me/2011/09/%e5%a4%a9%e4%b8%8e%e5%bc%97%e5%8f%96-%e5%8f%8d%e5%8f%97%e5%85%b6%e5%92%8e/#footnote_0_3448" id="identifier_0_3448" class="footnote-link footnote-identifier-link" title="汗一把&hellip;&hellip;">i</a></sup>，就是一大俗人，没有必要也装不了清高；生活就像被强奸，反抗不得还不如闭眼好好享受吧；</p>
<p>广告永远是需要包装的，聪明的你也许已经发现，<a href="http://blog.samsonis.me/2011/09/why-linux/" title="Why Linux? (AD)" target="_blank">上篇文章</a> 内容空泛，言不着意，却为了隐藏那一个 hyperlink 煞费苦心；标题曾特地包含的AD两字最终亦被要求remove，估计人家还想嘲笑我难道你连AD从来不说自己是AD这样简单的道理都不懂？</p>
<p>……</p>
<p>anyway，我已经开始享受了……</p>
[Footnotes]<ol class="footnotes"><li id="footnote_0_3448" class="footnote">汗一把……</li></ol>
	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/02/%e6%9c%aa%e6%88%90%e5%b9%b4%e5%85%88%e5%88%ab%e5%84%aa/" title="未成年先别優 (February 23, 2010)">未成年先别優</a> (1)</li>
	<li><a href="http://blog.samsonis.me/2011/07/%e6%b2%a1%e6%9c%89playlists%e7%9a%84player%e4%b8%8d%e6%98%af%e5%a5%bdplayer/" title="不支持multiple playlists的player不是好player (July 12, 2011)">不支持multiple playlists的player不是好player</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/04/update-55-%e6%a0%b8%e5%8e%bb%e6%a0%b8%e4%bb%8e/" title="Update 55: 核去核从 (April 13, 2011)">Update 55: 核去核从</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2010/07/update-38-%e4%ba%ba%e4%b8%8d%e5%8f%af%e4%bb%a5%e8%b2%8c%e7%9b%b8/" title="Update 38: 人不可以貌相 (July 30, 2010)">Update 38: 人不可以貌相</a> (6)</li>
	<li><a href="http://blog.samsonis.me/2011/08/starcraft-ii%e9%85%8d%e4%b8%8d%e5%9c%a8%e9%ab%98%e5%8f%af%e7%8e%a9%e5%b0%b1%e8%a1%8c/" title="StarCraft II配不在高可玩就行 (August 16, 2011)">StarCraft II配不在高可玩就行</a> (8)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/09/%e5%a4%a9%e4%b8%8e%e5%bc%97%e5%8f%96-%e5%8f%8d%e5%8f%97%e5%85%b6%e5%92%8e/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Why Linux?</title>
		<link>http://blog.samsonis.me/2011/09/why-linux/</link>
		<comments>http://blog.samsonis.me/2011/09/why-linux/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 16:15:19 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Share]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3443</guid>
		<description><![CDATA[Why Linux? A lot of people know nothing at all about Linux, and there is a significant portion of the computer-using community who don&#8217;t even know of its existence. Sadly, this may be due to the fact that companies who aren&#8217;t open-source can&#8217;t afford the same marketing budgets as Apple and Microsoft. It&#8217;s a shame, ...]]></description>
			<content:encoded><![CDATA[<p>Why Linux?</p>
<p>A lot of people know nothing at all about Linux, and there is a significant portion of the computer-using community who don&#8217;t even know of its existence. Sadly, this may be due to the fact that companies who aren&#8217;t open-source can&#8217;t afford the same marketing budgets as Apple and Microsoft. It&#8217;s a shame, because there&#8217;s a fair chance most people would switch, given the option.</p>
<p>Yes, you heard me correctly &#8211; it is an open-source operating system, and there are more versions of it than there are versions of Windows and Mac OS and OSX put together. Linux and Unbuntu are two of the most well known, and it is customiseable to the point where if you wanted an OS custom-built for you, it&#8217;s fully possible.</p>
<p>Open-source is also what it says on the tin &#8211; free code, which means that you&#8217;ll never pay a penny for the OS, or any of the software that works with it (unless you&#8217;re going out of the open-source bubble, which seems pointless when there are so many free alternatives that offer roughly the same features).</p>
<p>Installing it takes not long at all, as it&#8217;s quite a small OS in terms of memory, and it&#8217;s also nowhere near as CPU intensive as Windows or Mac OSX, so whether you&#8217;re running fifty programs at once or just a Twitter client and <a href="http://www.onlinepoker.com/">OnlinePoker.com</a>, everything will feel smoother and more responsive.</p>
<p>All I can say is that if you&#8217;re running a netbook or a low spec PC, or even a powerhouse, you owe it to yourself to install it, even on a partition, and test it out. Who knows? You just might become one of the thousands, if not millions of people contributing to the most generous movement on the web &#8211; the open-source community.</p>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/08/%e8%af%b4%e8%b0%8e/" title="说谎 (August 9, 2010)">说谎</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e8%8a%82%e9%80%89%e4%b8%a4%e6%ae%b5%e5%a5%a5%e5%b7%b4%e9%a9%ac2011%e5%9b%bd%e6%83%85%e5%92%a8%e6%96%87/" title="节选两段奥巴马2011国情咨文 (January 26, 2011)">节选两段奥巴马2011国情咨文</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2010/07/%e7%8b%ae%e5%ad%90%e5%ba%a7/" title="狮子座 (July 31, 2010)">狮子座</a> (16)</li>
	<li><a href="http://blog.samsonis.me/2010/02/%e6%9c%aa%e6%88%90%e5%b9%b4%e5%85%88%e5%88%ab%e5%84%aa/" title="未成年先别優 (February 23, 2010)">未成年先别優</a> (1)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e6%8e%a8%e8%8d%90%e4%b8%80%e4%b8%aareader-app%e5%92%8c%e4%b8%80%e4%b8%aagreasemonkey%e8%84%9a%e6%9c%ac/" title="推荐一个reader app和一个Greasemonkey脚本 (January 26, 2011)">推荐一个reader app和一个Greasemonkey脚本</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/09/why-linux/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>StarCraft II配不在高可玩就行</title>
		<link>http://blog.samsonis.me/2011/08/starcraft-ii%e9%85%8d%e4%b8%8d%e5%9c%a8%e9%ab%98%e5%8f%af%e7%8e%a9%e5%b0%b1%e8%a1%8c/</link>
		<comments>http://blog.samsonis.me/2011/08/starcraft-ii%e9%85%8d%e4%b8%8d%e5%9c%a8%e9%ab%98%e5%8f%af%e7%8e%a9%e5%b0%b1%e8%a1%8c/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 14:42:39 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[Entertainment]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[StarCraft 2]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3407</guid>
		<description><![CDATA[山不在高，有仙则名 水不在深，有龙则灵 —— 唐·刘禹锡《陋室铭》 鄙人以及鄙人的爱机（在他们看来则是陋机）已经被到处各种无数次的鄙视过了，但是…… but…… 阿Q就是这么诞生的： 参照这里优化，除了模型和影片，其它都比较无限趋向于杯具（一个极限函数）； 但是…… but…… 南阳诸葛庐，西蜀子云亭； 孔子云，何陋之有？ Related posts 未成年先别優 (1) 天与弗取 反受其咎 (3) 不支持multiple playlists的player不是好player (0) Update 55: 核去核从 (2) Update 38: 人不可以貌相 (6)]]></description>
			<content:encoded><![CDATA[<blockquote><p>
山不在高，有仙则名<br />
水不在深，有龙则灵</p>
<p>—— 唐·刘禹锡《陋室铭》
</p></blockquote>
<p>鄙人以及鄙人的爱机（在他们看来则是陋机）已经被到处各种无数次的鄙视过了，但是…… but…… 阿Q就是这么诞生的：</p>
<p><a href="http://blog.samsonis.me/wp-content/uploads/2011/08/starcraft2.png"><img src="http://blog.samsonis.me/wp-content/uploads/2011/08/starcraft2-300x219.png" alt="" title="starcraft2" width="300" height="219" class="alignnone size-medium wp-image-3417" /></a></p>
<p>参照<a href="http://www.sc2.com.cn/1009/148989961403.html" title="低配玩家的福音：星际2优化指导手册Mac版" target="_blank">这里</a>优化，除了模型和影片，其它都比较无限趋向于杯具（一个极限函数）；</p>
<p>但是…… but……</p>
<p>南阳诸葛庐，西蜀子云亭；<br />
孔子云，何陋之有？</p>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/02/%e6%9c%aa%e6%88%90%e5%b9%b4%e5%85%88%e5%88%ab%e5%84%aa/" title="未成年先别優 (February 23, 2010)">未成年先别優</a> (1)</li>
	<li><a href="http://blog.samsonis.me/2011/09/%e5%a4%a9%e4%b8%8e%e5%bc%97%e5%8f%96-%e5%8f%8d%e5%8f%97%e5%85%b6%e5%92%8e/" title="天与弗取 反受其咎 (September 26, 2011)">天与弗取 反受其咎</a> (3)</li>
	<li><a href="http://blog.samsonis.me/2011/07/%e6%b2%a1%e6%9c%89playlists%e7%9a%84player%e4%b8%8d%e6%98%af%e5%a5%bdplayer/" title="不支持multiple playlists的player不是好player (July 12, 2011)">不支持multiple playlists的player不是好player</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/04/update-55-%e6%a0%b8%e5%8e%bb%e6%a0%b8%e4%bb%8e/" title="Update 55: 核去核从 (April 13, 2011)">Update 55: 核去核从</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2010/07/update-38-%e4%ba%ba%e4%b8%8d%e5%8f%af%e4%bb%a5%e8%b2%8c%e7%9b%b8/" title="Update 38: 人不可以貌相 (July 30, 2010)">Update 38: 人不可以貌相</a> (6)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/08/starcraft-ii%e9%85%8d%e4%b8%8d%e5%9c%a8%e9%ab%98%e5%8f%af%e7%8e%a9%e5%b0%b1%e8%a1%8c/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Python NameError: name &#8216;readline&#8217; is not defined</title>
		<link>http://blog.samsonis.me/2011/08/python-nameerror-name-readline-is-not-defined/</link>
		<comments>http://blog.samsonis.me/2011/08/python-nameerror-name-readline-is-not-defined/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 08:08:14 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[MacPorts]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=3386</guid>
		<description><![CDATA[Python 2.6.×已经装了有一定时日了，已经完全不知道python的世界都更新到什么境界了；标题这个issue在我更新到Lion之前就已经存在了，所以这不能算是Lion里才introduce的issue，这个还是得有一句说一句； 之前我在Snow Leopard一直没有找到fix的办法，当然主要的原因还在于我也没有那么急切的想fix它，至少对于一个几个月都基本不碰python的家伙而言，它的急迫性显然没有解决starcraft2破解版一联机就自动更新不能玩这个issue来的高，xd:) BTW, 我只玩战役模式，Blizzard及其各路partner已经完全将starcraft2的对战模式变成网游，虽然我很想支持暴雪买正版，但是网游的各种消耗对我来说是无法容忍的. 貌似又有跑题的趋势…… anyway…… 这个issue就是下面这样的，注意倒数第二行： ~ ➔ python Python 2.6.6 &#40;r266:84292, Sep 13 2010, 20:53:06&#41; &#91;GCC 4.2.1 &#40;Apple Inc. build 5646&#41; &#40;dot 1&#41;&#93; on darwin Type &#34;help&#34;, &#34;copyright&#34;, &#34;credits&#34; or &#34;license&#34; for more information. Module readline not available. Traceback &#40;most recent call last&#41;: &#160; File &#34;/Users/samson/.pythonrc&#34;, line 22, in &#60;module&#62; &#160; ...]]></description>
			<content:encoded><![CDATA[<p>Python 2.6.×已经装了有一定时日了，已经完全不知道python的世界都更新到什么境界了；标题这个issue在我更新到Lion之前就已经存在了，所以这不能算是<a href="http://blog.samsonis.me/2011/08/lion-ready/" title="Lion Ready?" target="_blank">Lion里才introduce的issue</a>，这个还是得有一句说一句；</p>
<p>之前我在Snow Leopard一直没有找到fix的办法，当然主要的原因还在于我也没有那么急切的想fix它，至少对于一个几个月都基本不碰python的家伙而言，它的急迫性显然没有解决starcraft2破解版一联机就自动更新不能玩这个issue来的高，xd:)</p>
<p>BTW, 我只玩战役模式，Blizzard及其各路partner已经完全将starcraft2的对战模式变成网游，虽然我很想支持暴雪买正版，但是网游的各种消耗对我来说是无法容忍的.</p>
<p>貌似又有跑题的趋势…… anyway……</p>
<p>这个issue就是下面这样的，注意倒数第二行：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~ ➔ python<br />
Python 2.6.6 <span style="color: #7a0874; font-weight: bold;">&#40;</span>r266:<span style="color: #000000;">84292</span>, Sep <span style="color: #000000;">13</span> <span style="color: #000000;">2010</span>, <span style="color: #000000;">20</span>:<span style="color: #000000;">53</span>:06<span style="color: #7a0874; font-weight: bold;">&#41;</span> <br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span>GCC 4.2.1 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Apple Inc. build <span style="color: #000000;">5646</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>dot <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> on darwin<br />
Type <span style="color: #ff0000;">&quot;help&quot;</span>, <span style="color: #ff0000;">&quot;copyright&quot;</span>, <span style="color: #ff0000;">&quot;credits&quot;</span> or <span style="color: #ff0000;">&quot;license&quot;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">more</span> information.<br />
Module readline not available.<br />
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:<br />
&nbsp; File <span style="color: #ff0000;">&quot;/Users/samson/.pythonrc&quot;</span>, line <span style="color: #000000;">22</span>, <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">&lt;</span>module<span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; readline.read_history_file<span style="color: #7a0874; font-weight: bold;">&#40;</span>historyPath<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
NameError: name <span style="color: #ff0000;">'readline'</span> is not defined<br />
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span></div></div>
<p><span id="more-3386"></span><br />
主要的原因在于我起来的python cli会去读我home目录下.pythonrc <sup><a href="http://blog.samsonis.me/2011/08/python-nameerror-name-readline-is-not-defined/#footnote_0_3386" id="identifier_0_3386" class="footnote-link footnote-identifier-link" title="这个在环境变量$PYTHONSTARTUP里面设置">i</a></sup>，.pythonrc的前面几行则会试图去load readline或rlcompleter：</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">readline</span><br />
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Module readline not available.&quot;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">rlcompleter</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">readline</span>.<span style="color: black;">parse_and_bind</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tab: complete&quot;</span><span style="color: black;">&#41;</span></div></div>
<p>而诡异的是我明明装了readline，还装了两个（当然active的只有一个），这个python却始终很傻的说找不到：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~ ➔ port installed <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> readline<br />
&nbsp; readline <span style="color: #000000; font-weight: bold;">@</span>6.1.000_1+darwin<br />
&nbsp; readline <span style="color: #000000; font-weight: bold;">@</span>6.1.002_0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>active<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></div>
<p>我曾试图upgrade python，但是问题仍旧存在，当然真相永远只有一个……</p>
<p>不废话也不卖关子，最后的解决办法非常吐血，不是upgrade python也不是去装py-readline，而是再install一遍readline (当然最后turns out readline也升级了)：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~ ➔ <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> readline</div></div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~ ➔ port installed <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> readline<br />
&nbsp; readline <span style="color: #000000; font-weight: bold;">@</span>6.1.000_1+darwin<br />
&nbsp; readline <span style="color: #000000; font-weight: bold;">@</span>6.1.002_0<br />
&nbsp; readline <span style="color: #000000; font-weight: bold;">@</span>6.2.000_0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>active<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></div>
<p>problem solved:</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~ ➔ python<br />
Python 2.6.6 <span style="color: #7a0874; font-weight: bold;">&#40;</span>r266:<span style="color: #000000;">84292</span>, Sep <span style="color: #000000;">13</span> <span style="color: #000000;">2010</span>, <span style="color: #000000;">20</span>:<span style="color: #000000;">53</span>:06<span style="color: #7a0874; font-weight: bold;">&#41;</span> <br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span>GCC 4.2.1 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Apple Inc. build <span style="color: #000000;">5646</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>dot <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> on darwin<br />
Type <span style="color: #ff0000;">&quot;help&quot;</span>, <span style="color: #ff0000;">&quot;copyright&quot;</span>, <span style="color: #ff0000;">&quot;credits&quot;</span> or <span style="color: #ff0000;">&quot;license&quot;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">more</span> information.<br />
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span></div></div>
[Footnotes]<ol class="footnotes"><li id="footnote_0_3386" class="footnote">这个在环境变量$PYTHONSTARTUP里面设置</li></ol>
	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/08/terminal-app-use-option-as-meta-key/" title="Terminal.app use option as meta key (August 12, 2010)">Terminal.app use option as meta key</a> (14)</li>
	<li><a href="http://blog.samsonis.me/2011/03/terminal-app-echo-breaks-after-ctrl-c/" title="Terminal.app echo breaks after ctrl-c (March 18, 2011)">Terminal.app echo breaks after ctrl-c</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2010/09/sync-macports-ports-tree-using-subversion-over-http/" title="Sync MacPorts ports tree using Subversion (over HTTP) (September 13, 2010)">Sync MacPorts ports tree using Subversion (over HTTP)</a> (4)</li>
	<li><a href="http://blog.samsonis.me/2010/08/%e8%af%b4%e8%b0%8e/" title="说谎 (August 9, 2010)">说谎</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/01/%e8%8a%82%e9%80%89%e4%b8%a4%e6%ae%b5%e5%a5%a5%e5%b7%b4%e9%a9%ac2011%e5%9b%bd%e6%83%85%e5%92%a8%e6%96%87/" title="节选两段奥巴马2011国情咨文 (January 26, 2011)">节选两段奥巴马2011国情咨文</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2011/08/python-nameerror-name-readline-is-not-defined/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

