LifeType 中文開發論壇

開發 => 核心補強 => 主題作者是: Lance Li 於 九月 15, 2005, 01:10:06 下午



主題: 冗余代码的来源?
作者: Lance Li九月 15, 2005, 01:10:06 下午
在 articles.class.php 里,看起来有些代码是完全无用的,比如下面这段里面的 date=date,请问这些代码是从哪里来的?历史代码吗?

另外,同样是这个函数,上面的说明好象和下面的函数也相差很多了,同样是历史问题?

程式碼:
        /**
         * Updates the number of times a post has been read. This method does not just increase the num_read
         * counter by one but it can set it to whatever we want... Usually the value we pass in '$numReads' will
         * be the old value + 1, but it could be whatever.
         *
         * @param articleId A valid article identifier.
         * @param numReads A value, meaning how many times the post has been read.
         * @return Returns true if successful or false otherwise.
         */
        function updateArticleNumReads( $articleId )
        {
            // we have to build up the query, which will be pretty long...
            $query = "UPDATE ".$this->getPrefix()."articles SET ".
                     " num_reads = num_reads+1 ".
                     ", date = date ".
                     " WHERE id = '".Db::qstr($articleId)."'";

            $result = $this->Execute( $query );

            return $result;
        }


主題: Re: 冗余代碼的來源?
作者: minstrel九月 15, 2005, 03:18:01 下午
在 articles.class.php 裡,看起來有些代碼是完全無用的,比如下面這段裡面的 date=date,請問這些代碼是從哪裡來的?歷史代碼嗎?

另外,同樣是這個函數,上面的說明好像和下面的函數也相差很多了,同樣是歷史問題?

程式碼:
        /**
         * Updates the number of times a post has been read. This method does not just increase the num_read
         * counter by one but it can set it to whatever we want... Usually the value we pass in '$numReads' will
         * be the old value + 1, but it could be whatever.
         *
         * @param articleId A valid article identifier.
         * @param numReads A value, meaning how many times the post has been read.
         * @return Returns true if successful or false otherwise.
         */
        function updateArticleNumReads( $articleId )
        {
            // we have to build up the query, which will be pretty long...
            $query = "UPDATE ".$this->getPrefix()."articles SET ".
                     " num_reads = num_reads+1 ".
                     ", date = date ".
                     " WHERE id = '".Db::qstr($articleId)."'";

            $result = $this->Execute( $query );

            return $result;
        }

這是 SQL 語法的一部份....
第一個 date 是要傳進 SQL 的名稱, 第二個 date 是 articles 中的變數...

BTW. 代碼看起來無用, 跟代碼是否真的無用不同, 請不要太早就下了定論.



主題: Re: 冗余代码的来源?
作者: Lance Li九月 16, 2005, 09:15:31 上午
你确定?那这条语句的实际效果呢?你测试过 date 字段会有变化吗?

引用
第一個 date 是要傳進 SQL 的名稱, 第二個 date 是 articles 中的變數...
拜托仔细看看语法,如果照你所说,至少会应该是类似  date=$date 这样的语法吧



主題: Re: 冗余代碼的來源?
作者: minstrel九月 16, 2005, 02:02:15 下午
剛重看了一次. 更正一下說法.

第一個 date 是要傳進 SQL 的名稱, 第二個 date 是 SQL 本身的函式, 用來取得目前日期與時間.

用 date = date 是比較不正統的寫法, 一般都會寫成 date = date()

我個人還是覺得, 就算代碼有問題, 也就只是個bug, 還到不了冗余代碼或無用代碼的地步.

冗余代碼與無用代碼可是個頗嚴重的指控...



主題: Re: 冗余代码的来源?
作者: Lance Li九月 18, 2005, 09:43:48 上午
那就涉及到 date 字段设计时是准备用来做什么功能了。从我看的代码来看,似乎这里存储的最后修改时间,如果是这样,那在现在这个函数里应该是不需要更新这个字段的,而实际效果也是这样,date=date 并不起任何作用,所以我说它是冗余代码。

至于你据说的 date(),在 mysql 里似乎不是你想象的作用,具体参看这里 http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
程式碼:
DATE(expr)

Extracts the date part of the date or datetime expression expr.

mysql> SELECT DATE('2003-12-31 01:02:03');
        -> '2003-12-31'

DATE() is available as of MySQL 4.1.1.

我猜想你想用的是 NOW() 或者 CURDATE() 吧,这就又回到最开始说的,date 字段的设计功能了。


主題: Re: 冗余代码的来源?
作者: markwu九月 18, 2005, 11:14:47 上午
Hi Lance:

你可以看一下資料結構, plog_articles.date 他的預設值(缺省)是 current time stamp ,意思就是當你在 update 這一筆資料時,如果你不指定給 date 任何值,那麼他就會以現在的時間來當作這個欄位的時間。

我們以一個實例來看:

1. 假設有一篇文章 (id =2, 時間是 2005-09-18 12:01:47),那麼當我點下這篇文章, pLog 幫他 update 閱讀次數,他的實際 sql 如下:

UPDATE plog_articles SET num_reads = num_reads+1, date = date WHERE id = 2

所以這一個 sql 執行後 date 還是  2005-09-18 12:01:47,而 num_reads 會加 1。

2. 好,那麼我們把 sql 改為:

UPDATE plog_articles SET num_reads = num_reads+1 WHERE id = 2

你會發現sql 執行後,不僅  num_reads 會加 1,而且 date 被改變了。

這就是為什麼 sql 中要加入 (new time stamp) date = (old time stamp) date 的理由了。 因為希望文章的建立日期不要在 update  num_reads 被改變。

下次遇到這樣的狀況,可以先把他變成是實際的程式來試試看。光用看的,有時會很難理解的。 :-)

Mark


主題: Re: 冗余代码的来源?
作者: Lance Li九月 18, 2005, 06:57:03 下午
ok,明白了

我只是试了这一句的效果
UPDATE plog_articles SET num_reads = num_reads+1, date = date WHERE id = 2
看到了它不产生任何作用

也看过db设计,但没有仔细想过其中的效果,主要是没想到 db 会这样设计。

话说回来,这样设计的目的是什么呢?因为这个字段需要更新的次数相当少,完全可以在需要更新jf 用 date=NOW() 来指定,为什么要以现在的形式呢?相反这个 UPDATE plog_articles SET num_reads = num_reads+1 语句被执行的次数将相当多,如果每次都要指定 date=date 效率就会受影响吧。