我在这里等你~

拆分临时变量

Jul 5th, 2007

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

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

Fanfou Logo

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

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

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

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

TinyURL 链接转换脚本

Jul 4th, 2007
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

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

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

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

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

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

Fanfou Logo

Fanfou Tools 更新到 v1.0b4.

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

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

Check Syntax or Execute PHP file in Vim

Jun 25th, 2007

以前,我介绍过了如何在 VIM 中检查 PHP 的语法错误。这方面的知识,在网上也有很多人介绍。平时我们在编写 PHP 程序的时候,检查语法错误非常的方便,这样我们在浏览器中查看 PHP 程序运行效果时,就不会看到令人恼火的 PHP 语法错误了。

但有些时候,我们如果是想运行一小段简单的 PHP 代码时,该怎么办呢?以前我是通过 EditPlus 的 User Tools 来实现的,EP 的 User Tools 可以设定一个外部程序(比如 PHP 解析器),来运行 PHP 文件,然后在 Output Window 中显示运行的结果,非常方便。这次呢,我会介绍一下如何在 Vim 中实现相同的功能。

首先看一下下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parse and execute current php file
"""""""""""""""""""""""""""""""""""""""""""""""""""""
 
function! PhpParseExecute()
    " Parse and execute current php file
    setlocal makeprg=C:\PHP5\php.exe\ -f
 
    " Set shellpipe
    setlocal shellpipe=>
 
    " Use error format for parsing PHP error output
    setlocal errorformat=%m\ in\ %f\ on\ line\ %l
    make %
    clist
endfunction
 
map <F6> :call PhpParseExecute()<CR>
imap <F6> <ESC>:call PhpParseExecute()<CR>

如果你能看的懂 Vim Script,上面的代码一定会让你豁然开朗,其实这也是 Vim 官方网站的中的一个 Script,并不是我的发现。在很早之前我就在不断的思考,如何在 Vim 中执行 PHP 文件。我当初的做法是使用 “!” 调用 php.exe 来运行 PHP 文件,这个方法的确可行,但糟糕的是,Vim 在 Windows 平台上调用外部程序会弹出一个 DOS 窗口,非常的 Ugly!于是我只好放弃了这种非常之不优雅的做法,呵呵!直到我前段时间看到这个 Script,于是一切问题迎刃而解:P。

上面的 Script 思路很清晰,首先设置 makeprg 指定 PHP 解析器的位置,并使用 :make 命令来运行 PHP 脚本,然后设定 shellpipe 将运行结果发送到 VIM 的 “error file”,这里的 “>” 是 DOS 系统上面的默认值。最后执行 clist,打开 QuickFix 窗口,显示运行结果,如果有错误则直接显示 PHP 错误信息,而配置的 errorformat 正则表达式,在返回 PHP 错误时,按下回车键后,直接关闭 QuickFix,并把光标定位到出错的一行。

同样,我们也可以根据上面的脚本模板来改造一下,PHP 语法检查的功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Check PHP Syntax using makeprg
"""""""""""""""""""""""""""""""""""""""""""""""""""""
 
function! PhpCheckSyntax()
    " Check php syntax
    setlocal makeprg=\"C:\php5\php.exe\"\ -l\ -n\ -d\ html_errors=off
 
    " Set shellpipe
    setlocal shellpipe=>
 
    " Use error format for parsing PHP error output
    setlocal errorformat=%m\ in\ %f\ on\ line\ %l
    make %
endfunction
 
" Perform :PhpCheckSyntax()
map <F5> :call PhpCheckSyntax()<CR>
imap <F5> <ESC>:call PhpCheckSyntax()<CR>

为什么要改造这个语法检查的功能呢?因为按照以前做法,两者会冲突的,呵呵 :)

Tags: , ,