programing c: styles

Good programming style first;

Ideally, a program serves two purposes:
    1. it presents the computer with a set of instructions;
    2. it provides the programmer with a clear, easy-to-read description of what the program does.

Example:
/********************************************************
 * hello -- program to print out "Hello World".         *
 * Not an especially earth-shattering program.          *
 *                                                      *
 * Author: Steve Oualline.                              *
 *                                                      *
 * Purpose: Demonstration of a simple program.          *
 *                                                      *
 * Usage:                                               *
 *      Runs the program and the message appears.       *
********************************************************/
#include <stdio.h>
int main()
{
    /* Tell the world hello */
    printf("Hello World\n");
    return (0);
}



One of the best ways to organize thoughts: write them down in a language that is clear and easy to understand.

Good programming styles comes from experience and practice.

Only one rule exists: make your program as clear, concise, and simple as possible.

At the beginning of the program is a comment block that contains information about the program:
    Heading:    contains the name of the program and a short description of what the program does;
    Author:        take credit for it, also for future modification help
    Purpose:    Why did you write this program? What does it do?
    Usage:        a short explanation of how to run the program.
    References:    reference the original author of any work you copied
    File formats:    list the files that your program reads or write and a short description of their formats
    Restrictions:    list any limits or restrictions that apply to the program
    Revision history: contains a list indicationg who modified the program, and when and what changes were made
    Error handling:    if the program detects an error, describe what the program does with it
    Notes:        include special comments or other information that has not already covered

Declare variable:
    1. Avoid abbreviations;
    2. add a comment;
    so that, in effect, a mini-dictionary was created. It's easy to look up the meaning of a name.

Do not be clever. Clever kills. Clever makes for unreadable and unmaintainable programs.

Programs, by their nature, are extremely complex. Anything that you can do to cut down on this complexity will make your programs better.

Identation and Code Format:
    adapt to the one you feel most comfortable with
Clarity:
Simplicity:
    some general rules of thumb:
        1. a single function should not be longer than two or three pages.
        2. avoid complex logic like multiply nested ifs.
        3. long statements should be avoided.
        4. make your program as simple and easy to understand as possible, even if break some rules.

A progrAm should be concise and easy to read. It must serve as a set of computer instructions, but also as a reference work describing the algorithms and data used inside it. Everything should be documented with comments. Comments serve two purposes. First, they tell the programmer to follow the code, and second, they help the programmer remember what he did.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值