程序设计实践阅读摘要[一]

本文摘自《程序设计实践》,强调了编程中的关键要素:使用描述性的全局名称和简洁的局部名称,保持一致性,谨慎处理副作用,避免使用函数宏,用常量代替魔法数字,并提供了关于注释的实用建议。此外,推荐了《编程风格的元素》和《代码大全》作为补充阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1 Names 

    1.Use descriptive names for global, short names for local

        Clarity is often achieved through brievity

    2 Be Constant 

    3 Use active names for functions

        checkoctal Vs isoctal


1.2 Expressions and Statement

    1.Break up complex expressions

        *x += (0xp = (2*k < (n-m) ? c[k+1]: d[k--]));

         Vs

        if (2 * k < n-m)

            *xp = c[k+1];

        else

            *xp = d[k--];

        *x += *xp;

    2.Be carefull with side effects

        scanf("%d %d" &yr, &profit[yr]);


1.3 Consistency and idioms

 

1.4 Functions Macros

    1 Avoid function macros

        parameter appears more than once in the definition might be evaluated more than once

    2 Parenthesize the macro body and arguments 


1.5 Magic Number

    1 Define numbers as constants, not macros

        We can use enum type for C;

        We don't need to apply this rule if the magic number is clearly itself and is used for only once, arraySize for example.

    2 Use the language to calculate the size of an object

        C/C++:

        #define NELEMS(array) (sizeof(array))/sizeof(array[0])

        double buf[1024];

        for(int i = 0; i < NELEMS(buf); i++)


        Java:

        char buf[] = new char[1024];

        for(int i = 0; i < buf; i++)


1.6 Comments

    1 Comment functions and global datas

    2 Don't comment bad code, just rewrite it

    3 Don't contradict the code

        Change the comment accordingly when the code is changed


Supplementary Reading

1 The Elements of Programming Style: Brian Kernighan......

2.Code Complete 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值