毕业参加工作也快一年了,这份工作几乎都是接手前人做完或半成的项目,体会到不少阅读别人写的程序代码的痛苦,特别是混乱的代码。
最近,维护和开发公司旗下一网站的同事辞职了,主管又让我接手了这痛苦的差事。这网站已经几经人转手了,程序代码已经风格各异,惨不忍睹了。好了,回到的正题:把程序写好。
以下原始代码是网站程序代码里的一个函数,网站是用PHP+MYSQL
开发的。函数功能是:随机生成一个数据库里不存在的用户编号。
原始代码:
/**
*
随机生成用户编号,如果生成的编号已经存在,则重新生成。
*
* @param
int $type
* @return
int
*/
function
get_new_business_number(
$type
=
0
){
global
$db_link
;
//
引入全局的数据库连接
if
(
$type
){
$rand
= rand(
10000000
,
99999999
);
//
随机生成
1
个
8
位数
}
else
{
$rand
= rand(
1000
,
9999
);
//
随机生成
1
个
4
位数
}
while
(
$rand
==
0
){
//
如果生成的随机数为
0
,则递归的调用函数
get_new_business_number
重新生成
1
个随机数
$rand
=get_new_business_number(
$type
);
}
$sql
=
" SELECT
user_number FROM user_list WHERE user_number =
{
$rand
}
"
;
$result
= mysql_query(
$sql
,
$db_link
);
//
查询数据库
$rows
= mysql_num_rows(
$result
);
//
获取记录条数
if
(
$rows
>
0
) {
/*
如果记录已存在,则递归的调用函数
get_new_business_number
重新生成
1
个随机数,否则,随机数满足需求了,返回该值
*/
$rand
=get_new_business_number(
$type
);
}
else return
$rand
;
}
以上代码,原来没有注释,注释是我为解释程序加上的。我总觉得这段代码写得不太好:
首先,一个极其简单的功能,没有必要用递归的方式来实现,我们可以用更好更直接的方式来处理;
其次,代码后边
if
,
else
搭配方式来返回函数结果,现在一般的语法分析器都能检查到可能出现的问题(
C++ Primer 4th
edition
里也有相关的问题说明),也就是说函数可能会没有返回值,一般会有一个警告,本例为:
return-empty-val
: Both empty return and return with value used in function
get_new_business_number()
;
再次,代码中对生成的随机数是否为
0
进行判断,没有必要,参考
PHP
的手册对
rand
函数的说明,可以知道,代码中对
rand
函数的调用,不会返回
0
。
综上所述,我把代码改成了下面的形式,不足之处还请各路高手批评指正!
新代码
/**
*
随机生成用户编号,如果生成的编号已经存在,则重新生成。
*
* @param
int $type
* @return
int
*/
function
get_new_business_number(
$type
=
0
){
global
$db_link
;
//
引入全局的数据库连接
do
{
$rand = $type ?
rand(
10000000
,
99999999
) :
rand(
1000
,
9999
);
$sql
=
" SELECT user_number FROM user_list
WHERE user_number =
{
$rand
}
"
;
$result
= mysql_query(
$sql
,
$db_link
);
//
查询数据库
}
while
(mysql_num_rows(
$result
));
/*
如果记录已存在
,
重新循环生成
随机数,否则,随机数满足需求了,下面的语句返回该值
*/
return
$rand
;
}
上面的问题原始代码只是冰山一角,更有甚者,有些代码里的变量(包括变量名)随意定义,有时好几个看似毫不相关的变量,至少是变量名上如此,都代表同一对象,要看懂这类程序代码头都大。所以,我们有必要参照一定的规范来写代码,不管将来由谁来维护程序代码,都会感觉轻松。至于这规范在一些经典的计算机书籍里面都有说明,在此也不再赘述了。
写完程序,很容易;要把程序写好来,那就得下功夫了。完成一般的程序功能,可以有很多种方法,我们从中挑选一个简单、高效的就行(专业追求高效的,可要深思熟虑了)。虽然现在的计算机硬件已经发达了,但在问题规模较大的情况下,好的硬件配置远不如用好的算法写的程序。我就遇到过类似的情况,服务器配置了四核
CPU
、
4G
内存,上面的一个统计程序在日志文有上亿条记录时,跑
7
、
8
个小时都不出结果,我只把一段核心的代码稍作修改,
11
分钟不到就完成统计了,当然结果是正确的了。因此,在完成代码后,我们还有必要考虑代码能否进一步优化。
以上所述不成章体,难免有疏漏之处,还望各路高手不吝赐教!
附:
PHP
手册对
rand
函数的说明
rand
(PHP 4, PHP 5)
rand — Generate a random integer
int rand
( void )
int rand
( int
$min
, int
$max
)
If called without the optional
min
,
max
arguments rand()
returns a pseudo-random integer between 0 and
getrandmax()
. If you want a random number between 5 and 15 (inclusive), for example,
use rand(5, 15)
.
Note
: On some platforms (such as
Windows),
getrandmax()
is only 32768. If you require a range larger than 32768, specifying
min
and
max
will allow you to create a range larger than this, or
consider using
mt_rand()
instead.
Note
: As of PHP 4.2.0, there is no
need to seed the random number generator with
srand()
or
mt_srand()
as this is now done automatically.
min
The lowest value to return (default: 0)
max
The highest value to return (default:
getrandmax()
)
A pseudo random value between
min
(or 0) and
max
(or
getrandmax()
, inclusive).
Example #1 rand() example
<?php
echo
rand
() .
"/n"
;
echo
rand
() .
"/n"
;
echo
rand
(
5
,
15
);
?>
The above example will output something similar to:
7771
22264
11
- srand()
- Seed the random number generator - getrandmax()
- Show largest possible random value - mt_rand()
- Generate a better random value