写最好的代码
2008-01-26 15:54
113 查看
最好的代码是什么样的代码?有人说是最短的,有人说是效率最高的.你问我?还是用事实来说话好一点.
事实一:Bjarne Stroustrup(Creator and implementor of C++ Language)先生的语录:
Remember, a primary aim of writing code is to make its meaning clear to the next person reading it – and that
person just might be yourself a few years hence.
事实二:sgi stl代码:
下面是关于partition这个函数的实现:
template <class BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last, Predicate pred) ...{
while (true) ...{
while (true)
if (first == last)
return first;
else if (pred(*first))
++first;
else
break;
--last;
while (true)
if (first == last)
return first;
else if (!pred(*last))
--last;
else
break;
iter_swap(first, last);
++first;
}
}
以下是我写的一个版本:
int partition(int* arr, int len) ...{
int front, back;
front = 0;
back = len - 1;
int pivot = arr[(int)(double(rand()) / RAND_MAX)*5];
while ( front < back ) ...{
while ( arr[front] < pivot )
++ front;
while ( arr[back] > pivot )
-- back;
if ( arr[front] != arr[back] )
swap ( arr[front], arr[back] );
else ...{
++ front;
-- back;
}
}
return back;
}
你觉得那个好了?可能你会毫不思索的说“sgi stl is best!”,我不知道你是不是权威的压迫下说出这样的话,如果没有,那我的恭喜你的鉴赏力.
我在这里不断的警告我自己,能让人看懂的代码才是最好的代码.这才是我们写代码的目的.其他都是形式主义的玩弄者.
事实一:Bjarne Stroustrup(Creator and implementor of C++ Language)先生的语录:
Remember, a primary aim of writing code is to make its meaning clear to the next person reading it – and that
person just might be yourself a few years hence.
事实二:sgi stl代码:
下面是关于partition这个函数的实现:
template <class BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last, Predicate pred) ...{
while (true) ...{
while (true)
if (first == last)
return first;
else if (pred(*first))
++first;
else
break;
--last;
while (true)
if (first == last)
return first;
else if (!pred(*last))
--last;
else
break;
iter_swap(first, last);
++first;
}
}
以下是我写的一个版本:
int partition(int* arr, int len) ...{
int front, back;
front = 0;
back = len - 1;
int pivot = arr[(int)(double(rand()) / RAND_MAX)*5];
while ( front < back ) ...{
while ( arr[front] < pivot )
++ front;
while ( arr[back] > pivot )
-- back;
if ( arr[front] != arr[back] )
swap ( arr[front], arr[back] );
else ...{
++ front;
-- back;
}
}
return back;
}
你觉得那个好了?可能你会毫不思索的说“sgi stl is best!”,我不知道你是不是权威的压迫下说出这样的话,如果没有,那我的恭喜你的鉴赏力.
我在这里不断的警告我自己,能让人看懂的代码才是最好的代码.这才是我们写代码的目的.其他都是形式主义的玩弄者.
相关文章推荐
- 写java代码有感。。。构造方法最好带着,
- 开发人员眼中最好的代码编辑器是谁?
- 超小的PHP小马587字节【php网站的最好后门】绝对免杀!因为本来就是正常代码嘛!
- 转 代码之我见:“诚实”的代码最好
- 写代码的时候闪过你脑海的第一个最土的解决方案就是最好的解决方案
- 一种最好用的php生成excel的代码 适用于windows和linux
- 写代码文章最好这样
- 最好的js代码在线压缩,加密
- [转]目前找到的最好的Iframe自适应高度代码
- JavaScript 面向对象编程,严格过程的标准化编程法,目前为止最好的 JS 生成对象代码结构
- js取得参数的最好的代码,及其他代码搜集
- 最好是用C语言可以写出来的 代码不用给了 说出意思就行 日历(万年历)算法
- 目前最好的,以rtmp发送264到red5服务器的代码,darkdragonking亲测可用--《到H264视频通过RTMP直播》
- 写一个函数,检查字符是否是整数,如果是,返回其整数值(最好用四句代码写出)
- 可能是迄今为止最好的GitHub代码浏览插件--赞
- 究竟什么是开发人员眼中最好的代码编辑器?
- 18个最好的代码编辑器/IDE工具
- PHP uniqid()函数可用于生成不重复的唯一标识符,该函数基于微秒级当前时间戳。在高并发或者间隔时长极短(如循环代码)的情况下,会出现大量重复数据。即使使用了第二个参数,也会重复,最好的方案是结
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
- 文字换行的最好代码,和需要注意的问题