红酥手,黄滕酒,满城春色宫墙柳。东风恶,欢情薄。一怀愁绪,几年离索。错!错!错!
春如旧,人空瘦,泪痕红浥鲛绡透。桃花落,闲池阁。山盟虽在,锦书难托。莫!莫!莫!

Introduce Explaining Variable

Jul 5th, 2007 | Filed under PHP
Comments Off

Motivation
复杂的表达式是很难阅读的,不仅如此,很多时候由于表达式太长而不得不换行书写,也影响代码的美化。

比如 If 括号中的条件表达式,如果太长的话,我们可以把表达式拆分为几个小的表达式,并赋值给不同的临时变量,这样代码将会显得清晰美观,易于阅读。

Example Code

1
2
3
4
5
6
7
8
function price() {
    // price is base price - quantity discount + extra fee
    return $this->quantity * $this->item_price -
        max(0, $this->quantity - 200) * $this->item_price * 0.08 +
        min($this->quantity * $this->item_price * 0.1, 100.0);
}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

是不是又臭又长很难读啊?下面我们试着把表达式拆分:

1
2
3
4
5
6
7
8
function price() {
    $price    = $this->quantity * $this->item_price;
    $discount = max(0, $this->quantity-200) * $this->item_price * 0.08;
    $fee      = min($basePrice * 0.1, 100.0);
    return  $price - $discount + $fee;
}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

上面的重构的代码,已经达到了我们的目的,下面我们来尝试一下新的重构方式:Extract Methods,把表达式拆分为一个个小的函数(或方法)。

拆分成函数和拆分为变量可以达到相同的效果,但如果我们需要达到重用的效果,Extract Methods 更适合,再看下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function getPrice() {
    return  $this->price() - $this->discount() + $this->fee();
}
 
// Extract as methods
 
function price() {
    return $this->quantity * $this->item_price;
}
 
function discount() {
    return max(0, $this->quantity - 200) * $this->item_price * 0.08;
}
 
function fee() {
    return min($this->price() * 0.1, 100.0);
}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

在这个例子中,Extract Methods 比 Explaining Variables 更好用,但并不代表所有的情况,还是那句话:具体问题具体分析。

使用哪种重构方法,还要看具体的情况而定。

Tags: ,

拆分临时变量

Jul 5th, 2007 | Filed under PHP
Comments Off

当一个临时变量被赋值多次时,那么将其拆分成多个,除非它是一个循环计数器。

Motivation

临时变量有这多种不同的用途。比如它们可被用作循环中的计数器,在循环中保存结果集,亦或保存一个冗长的表达式的计算结果等等。

这些类型的变量(容器)应该只赋值一次。如果一个同名的临时变量被赋予多个职责,将会影响代码的可读性。这个时候我们应当引入一个新的临时变量以使代码更加清晰易懂。

可能有些注重性能的人会说,引入一个新的变量将会占用更多的内存。的确如此,但是注册一个新的变量不会吸干服务器内存的,这一点请放心,我们不是活在 386 时代,与其在这些无聊的细枝末节上面搞所谓的优化,不如去优化真正的系统性能瓶颈,比如数据库、网络连接等等,而且清晰易懂的代码更容易被重构,发现 Bug,或者解决性能问题等等。

Example Code

很多时候,我们使用同一个 $temp 变量来计算一个物体的不同属性,这种情况比较常见,比如下面这个例子:

1
2
3
4
5
6
7
8
9
function rectangle($width=1, $height=1) {
    $temp = 2 * ($width + $height);
    echo "Perimter: $temp <br />";
 
    $temp = $width * $height;
    echo "Area: $temp";
}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

正如你所看到的,$temp 被使用了两次分别用来计算长方形的周长以及面积。这个例子看起来非常直观清晰,但实际的项目代码可能远比这个例子复杂,如果我们把代码改成下面的样子,这样,不管代码如何复杂都不会有混淆感了。

1
2
3
4
5
6
7
8
9
function rectangle($width=1, $height=1) {
    $perimeter = 2 * ($width + $height);
    echo "Perimter: $perimeter <br />";   
 
    $area = $width * $height;
    echo "Area: $area";
}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

为不同的东西(如表达式)声明一个新的临时变量吧,大部分时候性能并不是什么问题,而可读性则非常重要。

Tags: ,

WordPress Plugin – Fanfou Tools 1.0 Beta 6

Jul 5th, 2007 | Filed under WordPress
Comments Off

Fanfou Logo

Fanfou Tools 更新到 v1.0b6,修改的地方不多,只增加一个特性。

激活该选项后,Notify Post 时,该插件将会把冗长的 Permalink URL 转换为长度较短的 Tiny URL。

由于需要连接到 tinyurl.com 来转换 URL,所以对发贴的速度将会有所影响。

详细信息、下载,请访问:
WordPress Plugin – Fanfou Tools

TinyURL 链接转换脚本

Jul 4th, 2007 | Filed under PHP
Comments Off
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * transform
 *
 * @param mixed $url
 * @access public
 * @return void
 */
function transform($url) {
    if (empty($url) or !preg_match('|^(?:http://)?([^/]+)|i', $url)) {
        return;
    }
 
    $url     = "http://tinyurl.com/create.php?url=$url";
    $content = file_get_contents($url);
    $pattern = '|<blockquote><b>(http://tinyurl\.com/([^<]+))</b><br><small>|i';
    preg_match($pattern, $content, $matches);
    return $matches[1];
}
 
// }}}
 
 
// {{{ revert($tinyurl)
 
/**
 * revert
 *
 * @param mixed $tinyurl
 * @access public
 * @return void
 */
function revert($tinyurl) {
    $url     = explode('.com/', $tinyurl);
    $url     = 'http://preview.tinyurl.com/' . $url[1];
    $preview = file_get_contents($url);
    $pattern = '/redirecturl" href="(.*)">/i';
    preg_match($pattern, $preview, $matches);
    return $matches[1];
}
 
// }}}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

无法使用中文URL,官方转换的 URL 如果其中还有中文似乎也有问题。 :grin:

Tags: ,

My Build Vi IMproved Executables

Jul 2nd, 2007 | Filed under Vim

Build from:
Cygwin with GCC

Patches:
Included patches: 1-32

Interfaces supported:

  1. ActivePerl 5.8
  2. ActiveTcl 8.4
  3. MzScheme 370
  4. Python 2.5
  5. Ruby 1.8.6

Download:
http://www.phpvim.net/files/vim/vim71-latest.7z

Tags: ,

PHP 的速记语法

Jun 29th, 2007 | Filed under PHP

Handy shorthand syntax for php.
From: http://www.tech-recipes.com/php_programming_tips288.html

当程序中充满很多if/else结构的时候,程序会显得冗长,如果if/else结构比较简单,我们可以使用?:来简化这种结构:

1
echo "var is ".($var < 0 ? "negative" : "positive");

等同于:

1
2
3
4
5
echo "var is "; 
if ($var < 0) 
    echo "negative"; 
else 
    echo "positive";

第一种方式只用一行代码就完成了if/else五行代码的工作,显得非常简洁,但是从效率上来讲,if/else要高于使用?:,如何取舍,就看个人喜好了。
:smile:

Tags:

Java 与 PHP5 的构造函数重载

Jun 29th, 2007 | Filed under PHP
Comments Off

Java Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Hoge {
 
    public Hoge() {
        System.out.println("constructor 0");
    }
 
    public Hoge(int a) {
        System.out.println("constructor 1:" + a);
    }
 
    public Hoge(int a, String[] hoge) {
        System.out.println("constructor 2:" + a + ":" + hoge);
    }
 
    public Hoge(A a, B b, C c) {
        System.out.println("constructor 3:" + a + ":" + b + ":" + c);
    }
}

PHP5 Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Hoge {
 
    public function __construct() {
        $num = func_num_args();
        $args = func_get_args();
        switch($num) {
        case 0:
            $this->__call('__construct0', null);
            break;
        case 1:
            $this->__call('__construct1', $args);
            break;
        case 2:
            $this->__call('__construct2', $args);
            break;
        case 3:
            $this->__call('__construct3', $args);
            break;
        default:
            throw new Exception();
        }
    }
 
    public function __construct0() {
        echo "constructor 0" . PHP_EOL;
    }
 
    public function __construct1($a) {
        echo "constructor 1: " . $a . PHP_EOL;
    }
 
    public function __construct2($a, array $hoge) {
        echo "constructor 2: " . $a . PHP_EOL;
        var_dump($hoge);
    }
 
    public function __construct3(A $a, A $b, C $c) {
        echo "constructor 3: " . PHP_EOL;
        var_dump($a, $b, $c);
    }
 
    private function __call($name, $arg) {
        return call_user_func_array(array($this, $name), $arg);
    }
}
Tags: ,

WordPress Plugin – Fanfou Tools 1.0 Beta 5

Jun 28th, 2007 | Filed under WordPress
Comments Off

Fanfou Logo

Fanfou Tools 更新到 v1.0b5.

  • 新特性:Fanfou Tools 支持 WordPress Widgets 了
  • 重写 fanfou_list_posts(),增加 $args 参数
  • 在设置界面可以设置时间的格式

$args 参数的风格与 sidebar 类似的函数相似,需要注意的是参数 $args 中的值将会覆盖 Fanfou Options 中设置的参数,
比如 sidebar 中显示消息数目 limit 和时间格式 date_format

示例:

// 使用默认参数
fanfou_list_posts();
 
// 不显示时间
fanfou_list_posts("show_date=0");
 
// 自定义时间格式,并显示 5 条饭否消息
fanfou_list_posts("date_format=Y-m-d H:i&limit=5");

可用的参数包括(左侧为参数名,右侧为默认值):

show_date     => 1
date_format   => Y-m-d H:i
title_li      => Fanfou
echo          => 1
sort_column   => fanfou_created_at
sort_order    => DESC
class         => fanfou
limit         => 10

详细信息、下载,请访问:
WordPress Plugin – Fanfou Tools

Vim Script – Fanfou

Jun 26th, 2007 | Filed under Vim

Fanfou Logo

这是个很简单的 Vim + Python 脚本,可是我却写了一整夜,毕竟对于 Python,我还是在入门边缘徘徊的菜鸟。

这个脚本做三件事情:

  1. 显示饭否随便看看的消息
  2. 显示饭否中我以及我的好友的消息
  3. 发布新的饭否消息

其实,按照 API 来说,还是可以做很多,比如好友管理什么的,不过天快亮了,就不做了,基本上可以发布消息,可以看消息基本就 OK 了。

由于这个 Script 包含 Python 文件,所以你的 Vim 必须支持 Python 并安装 simplejson 模块才行(使用 easy_install simplejson 安装,具体步骤,请自行查询,谢谢:P ),否则无法使用,Vim 启动时会报错,很难看哦!

Installation:
把 fanfou.py、fanfou.vim 拷贝到 vimfiles/plugin 目录下面,打开 fanfou.vim,编辑:

if !exists("g:fanfou_username")
    let g:fanfou_username = "fanfou-id-or-email"
endif
 
if !exists("g:fanfou_password")
    let g:fanfou_password = "fanfou-password"
endif
</php>
 
填入你的帐号密码,即可。
 
另外你也可以修改下面的键盘映射:
 
<pre lang="bash">
nmap <Leader>tw <Esc>:execute 'py FanfouVim.UpdateStatus("' . inputdialog('Enter a fanfou status message:') . '")'<CR>
nmap <Leader>tp <Esc>:py FanfouVim.GetPublicTimeline()<CR>
nmap <Leader>tf <Esc>:py FanfouVim.GetFriendsTimeline()<CR>

Update: Jun 28, 2007 17:50

  1. 修正source问题,使用该插件发送饭否消息时,将显示为:“通过 Vim”

Update: Jul 11, 2007 05:38

  1. 没有安装 Python 以及没有安装 simplejson 的情况,显示友好的报错信息。

Download:
Vim Script – Fanfou 1.0b2

Tags: ,

WordPress Plugin – Fanfou Tools 1.0 Beta 4

Jun 26th, 2007 | Filed under WordPress
Comments Off

Fanfou Logo

Fanfou Tools 更新到 v1.0b4.

  • 解决未导入Prototype.js,导致Test Login报错的问题 (感谢 star)
  • 增加同步功能,同步饭否与WordPress的数据
  • 移除饭否文摘功能,估计没人愿意把饭否上面的东西打包贴到WordPress上面

详细信息、下载,请访问:
WordPress Plugin – Fanfou Tools