programing c: styles

来源:互联网 发布:软件介绍ppt模板 编辑:程序博客网 时间:2024/04/30 00:38
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