文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>代码优化

代码优化

时间:2007-02-05  来源:hello386

Linux 是一个多用户系统,因此对用户的管理是系统管理的基本组成部分。安装 Linux 的用户很可能就是该系统的管理员,也就是权限最高的 root。通过对用户的管理,分清了用户之间的责、权、利,保证了系统安全。

对软件的评价:代码的稳定性、友好性、代码的易读性、统一的风格、技巧。

    1。尽量少的使用全局变量

    2。局部变量一定要初始化,特别是指针变量

    3。成员函数功能单一,不要过分追求技巧,函数体不要过长。

         {i = i + j;      {int k = i;

         j = i - j;       i=j;

         i = i = j;       j=k;

         }                }

    4。最好有头文件

    5。关于变量名的长短问题

    6。设计函数时考虑到通用性

    7。申请内存时,一定先要释放。注意 if 问题。

    8。对浮点数比较大小时注意不要使用 ==

    9。最好不要用 goto 语句

    10。所有成员函数要单出口单入口

    11。函数中,要先检验参数的合法性

    12。最好所有的函数都有返回值,表明错误的原因。

    13。注释问题

    14。类型转化一律用显示转换。

    15。定义宏说,参数使用括号,结果也应该括起来

        #define SUB(a,b) ((a)-(b))

        3*SUB(3,4-5);

    16。变量长度一定要用 sizeof 来求

    17。malloc 后千万别忘 free 及使指针等于 NULL。

    18。字符串拷贝时尽量少使用 sprintf,而使用 memcpy,最后别忘加上'\0'

    19。慎重选择编译时的优化选项。

    20。小组开发时,注意代码风格的统一。


GNU 编码标准
GNU 编码标准(Coding Standards)

原文在

        http://gnu.clinux.org/prep/standards.html
看过之后随手记录了一点儿

Linux 命令行参数处理,
getopt();
getopt_long();
getopt_long_only();

在调用过程中多使用高层的接口;
eg. readdir;

signal handling facilities 信号处理:
1. BSD: signal          the Best
        #include <signal.h>
2. POSIX: sigaction
3. USG: signal

使用临时文件,请检查环境变量TMPDIR
使用由它指定的目录

编码格式:
or, if you want to use ANSI C, format the definition like this:

static char *
concat (char *s1, char *s2)
{
  ...
}

In ANSI C, if the arguments don't fit nicely on one line, split it
like this:

int
lots_of_args (int an_integer, long a_long, short a_short,
              double a_double, float a_float)
...

Try to avoid having two operators of different precedence at the same
level of indentation. For example, don't write this:

mode = (inmode[j] == VOIDmode
        || GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j])
        ? outmode[j] : inmode[j]);

Instead, use extra parentheses so that the indentation shows the
nesting:

mode = ((inmode[j] == VOIDmode
         || (GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j])))
        ? outmode[j] : inmode[j]);

Insert extra parentheses so that Emacs will indent the code properly.
For example, the following indentation looks nice if you do it by hand,


v = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000
    + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000;

but Emacs would alter it. Adding a set of parentheses produces something
that looks equally nice, and which Emacs will preserve:

v = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000
     + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000);

Format do-while statements like this:

do
  {
    a = foo (a);
  }
while (a > 0);

清洗的使用C语言的构造:
1.不要省略int类型的声明;
2.-Wall
3.不要在函数内部进行extern声明;
4.在函数中使用另外的形参命名方式;
5.不要在局部变量和参数中映射全局变量;

变量和函数的命名方法:
1.在定义全局变量和函数的时候,不要使用过于简单的命名方法,要通过名字反映它
们的用途;
2.不要过分使用缩写;

3.使用下划线来分割名字中的单词;
4.使用枚举类型来定义constant int,而不要用#define

不同系统间的可移植性:
1.使用Autoconf来进行配置;
2.define the "feature test macro" _GNU_SOURCE when compiling your C
files.

调用系统函数:
1.不要使用sprintf的返回值;
2.vfprintf不是都提供的;
3.main要返回int;
4.不要明确的声明系统函数;
5.如果必须定义系统函数的话,不要指明参数类型;
6.对于处理string的函数需要特别对待;

i18n,国际化:
1.要在每一个程序中使用gettext库;
  eg. printf (gettext ("Processing file `%s'..."));

程序的文档化:

发布过程:


Makefile约定:


书籍
     "Beginning Linux Programming"

                        published by Wrox Press
                        author: Neil Matthew and Richard Stones

     "LINUX kernel Internals"

                        published by Addison Wesley

     "Advanced Programming Language in the UNIX Environment"

                        published by Addison Wesley
                        ISBN 0-201-56317-7

                        author: W. Richard Stevens

     "UNIX Network Programming"

                        published by Prentice Hall
                        ISBN 981-3026-53-7
                        author: W. Richard Stevens

     "The UNIX C Shell field guide"

                        published by Prentice-Hall
                        ISBN 0-13-937468-X 025

相关阅读 更多 +
排行榜 更多 +
摧毁大厦游戏

摧毁大厦游戏

飞行射击 下载
合并动物城手游版

合并动物城手游版

休闲益智 下载
哈士奇大冒险

哈士奇大冒险

休闲益智 下载