天天看點

幹淨的代碼是改出來的

對于程式員來說,最終的也是最基本的目标就是能寫出一手好的代碼。随着代碼量的增長,自身對什麼是好的代碼的認識也漸漸有了不斷的調整。

1 注釋真的那麼重要麼?

最好的注釋就是代碼。這句話确實是沒有錯誤的。如果一個函數占用了一屏的版面,原因是由于各種各樣的注釋和解釋性的 // ** 等說明文檔,确實是比較惱人的。與其花過多的時間花精力在注釋和說明的編寫上面,不如花時間在變量名的編寫上面。

不能說沒有注釋的代碼一定是天書。在程式員界來說,其實有許多是大家預設的約定,以php為例子

如果說function getmsgbyssn(msgid,msgid,ssn)

function getmsgs($msgids);

這樣的語句其實不用注釋完全是可以的

這說明好的變量名和函數名是最好的注釋!

在做一個完整的項目的時候,看代碼的過程中其實就是接受作者潛意識規約的過程。

如果一個大的項目,所有的資料結構都使用一緻的變量名,msg,msg,chg, 那麼這些變量名就已經賦予了完整的定義了。

比如在一個項目中,在所有表示“消息”這個概念的地方,不管是參數還是傳回值,完全都隻使用$msg這麼一個array()

那麼,雖然我沒有在每個引用的地方加大篇幅說明$msg中的key和value是什麼,隻要讀者追着看到這樣的函數:

1

2

3

4

5

6

7

8

9

10

11

12

<code>function</code> <code>getmsg()</code>

<code>{</code>

<code>    </code><code>$msgid</code> <code>= self::getmsgid()</code>

<code>    </code><code>return</code> <code>array</code><code>(</code>

<code>    </code><code>‘msgid’ =&gt;</code><code>$msgid</code><code>,</code>

<code>    </code><code>'ssn'</code>      <code>=&gt; self::getssn(</code><code>$msgid</code><code>),</code>

<code>    </code><code>'title'</code>      <code>=&gt; self::gettitle(</code><code>$msgid</code><code>),</code>

<code>   </code><code>);</code>

<code>}</code>

是不是/** msg包含 msgid,ssn,title **/這樣的注釋更好呢?

當然,好代碼在變量都一定會遵循的規則是:一個項目一個意思的東西,一定隻有一個規定的變量名

好的代碼會由于一個或兩個變量名起的不對而不惜一次一次的svn commit,最後出現的代碼一定不會讓你失望的

2 代碼的簡潔性

你總是能感歎到為什麼有的人寫的代碼是這麼讓人舒服。

讓代碼簡單并不是一件容易的事情。這需要相當的代碼能力才能有這樣的能力。

比如這麼一個函數,明明可以更簡單的:

<code>function</code> <code>example()</code>

<code>    </code><code>$imsgid</code> <code>=</code><code>$this</code><code>-&gt;getmsgid();</code>

<code>    </code><code>$stitle</code> <code>=</code><code>$this</code><code>-&gt;gentitle(</code><code>$imsgid</code><code>);</code>

<code>    </code><code>$scontent</code> <code>=</code><code>$this</code><code>-&gt;gencontent(</code><code>$imsgid</code><code>);</code>

<code>    </code><code>$result</code> <code>=</code><code>array</code><code>(</code>

<code>        </code><code>'msgid'</code> <code>=&gt;</code><code>$imsgid</code><code>,</code>

<code>        </code><code>'title'</code>     <code>=&gt;</code><code>$stitle</code><code>,</code>

<code>        </code><code>'content'</code> <code>=&gt;</code><code>$scontent</code><code>,</code>

<code>    </code><code>);</code>

<code>    </code><code>return</code> <code>$result</code><code>;</code>

我甯可選擇寫成這樣:

<code>    </code><code>$msgid</code> <code>=</code><code>$this</code><code>-&gt;getmsgid();</code>

<code>    </code><code>$title</code> <code>=</code><code>$this</code><code>-&gt;gentitle(</code><code>$msgid</code><code>);</code>

<code>    </code><code>$content</code> <code>=</code><code>$this</code><code>-&gt;gencontent(</code><code>$msgid</code><code>);</code>

<code>    </code><code>return</code> <code>compact(</code><code>'msgid'</code><code>,</code><code>'title'</code><code>,</code><code>'content'</code><code>);</code>

不妨能不能用更少的代碼行數寫出一樣功能性的代碼。

代碼的量一旦減少,給的資訊就是:犯錯的機率也更少了。

最近在新項目組有幾個感想:

以前經常覺得有很多函數必須要很詳細的參數說明什麼的,其實大都都是可以使用oo的方法來使代碼更優美

比如function(msgid,msgid,title, content,content,ssn)

為什麼不是使用function($msg)呢?

開始我認為,$msg這樣傳入并不知道裡面包含的key和value是什麼,對代碼的閱讀性造成障礙

但是後來想想,其實這是因為我在閱讀到這個函數的時候并沒有$msg是一個對象的概念,也就是前面的代碼并沒有在人的潛意識裡面栽種下這個對象的概念。那麼前面的代碼應該改了…………

2 好的代碼不是一次性寫出來的,一定是一次一次svn commit堆積出來的,你會看到某大牛為了一個空格,一個檔案名是使用cron還是shell, 一個變量名(比如getmsg(msgid)=&gt;getmsg(msgid)=&gt;getmsg(msgid))而進行一次又一次的改動

最後得出的代碼真的是“幹淨”的!

----------------------

作者:yjf512(軒脈刃)

出處:http://www.cnblogs.com/yjf512/

本文版權歸yjf512和cnblog共有,歡迎轉載,但未經作者同意必須保留此段聲明