logo

Navigation:First_Community->Software->Windows Application Goto:New TopicSettingSearch
VC编程规范-程序员们都应该这样写代码
Posted by: carl Date: March 25, 2006 10:11AM
基本要求

MegaEntry - Social networking and discussion site!

1.1 程序结构清析,简单易懂,单个函数的程序行数不得超过100行。1.2 打算干什么,要简单,直接了当,代码精简,避免垃圾程序。1.3 尽量使用标准库函数和公共函数。1.4 不要随意定义全局变量,尽量使用局部变量。1.5 使用括号以避免二义性。

MegaEntry - Social networking and discussion site!

2.可读性要求2.1 可读性第一,效率第二。2.2 保持注释与代码完全一致。2.3 每个源程序文件,都有文件头说明,说明规格见规范。2.4 每个函数,都有函数头说明,说明规格见规范。

MegaEntry - Social networking and discussion site!

2.5 主要变量(结构、联合、类或对螅┒ㄒ寤蛞檬保⑹湍芊从称浜濉?br> 2.7 常量定义(DEFINE)有相应说明。2.8 处理过程的每个阶段都有相关注释说明。2.9 在典型算法前都有注释。2.10 利用缩进来显示程序的逻辑结构,缩进量一致并以Tab键为单位,定义Tab为 6个字节。2.11 循环、分支层次不要超过五层。

MegaEntry - Social networking and discussion site!

2.12 注释可以与语句在同一行,也可以在上行。2.13 空行和空白字符也是一种特殊注释。2.14 一目了然的语句不加注释。2.15 注释的作用范围可以为:定义、引用、条件分支以及一段代码。2.16 注释行数(不包括程序头和函数头说明部份)应占总行数的 1/5 到 1/3 。

MegaEntry - Social networking and discussion site!

3. 结构化要求3.1 禁止出现两条等价的支路。3.2 禁止GOTO语句。3.3 用 IF 语句来强调只执行两组语句中的一组。禁止 ELSE GOTO 和 ELSE RETURN。

CopyRight owned by the original author.--(www.MegaEntry.com)

3.4 用 CASE 实现多路分支。3.5 避免从循环引出多个出口。3.6 函数只有一个出口。3.7 不使用条件赋值语句。3.8 避免不必要的分支。3.9 不要轻易用条件分支去替换逻辑表达式。

MegaEntry - Social networking and discussion site!

4. 正确性与容错性要求4.1 程序首先是正确,其次是优美4.2 无法证明你的程序没有错误,因此在编写完一段程序后,应先回头检查。4.3 改一个错误时可能产生新的错误,因此在修改前首先考虑对其它程序的影响。

CopyRight owned by the original author.--(www.MegaEntry.com)

4.4 所有变量在调用前必须被初始化。4.5 对所有的用户输入,必须进行合法性检查。4.6 不要比较浮点数的相等,如: 10.0 * 0.1 == 1.0 , 不可靠4.7 程序与环境或状态发生关系时,必须主动去处理发生的意外事件,如文件能否逻辑锁定、打印机是否联机等。

MegaEntry - Social networking and discussion site!

4.8 单元测试也是编程的一部份,提交联调测试的程序必须通过单元测试。5. 可重用性要求5.1 重复使用的完成相对独立功能的算法或代码应抽象为公共控件或类。5.2 公共控件或类应考虑OO思想,减少外界联系,考虑独立性或封装性。

CopyRight owned by the original author.--(www.MegaEntry.com)

5.3 公共控件或类应建立使用模板。附:C++ 编程规范,delphi作相应的参考.1适用范围

CopyRight owned by the original author.--(www.MegaEntry.com)

本标准适用于利用Visul C++ ,Borland C++进行软件程序开发的人员.。.2变量命名

MegaEntry - Social networking and discussion site!

命名必须具有一定的实际意义,形式为xAbcFgh,x由变量类型确定,Abc、Fgh表示连续意义字符串,如果连续意义字符串仅两个,可都大写.如OK.具体例程:

MegaEntry - Social networking and discussion site!

BOOL类型 bEnable;ch * char chTextc * 类对象 cMain(对象实例)

MegaEntry - Social networking and discussion site!

h * Handle(句柄) hWndi * intn * 无符号整型p * 指针sz,str * 字符串w WORD

MegaEntry - Social networking and discussion site!

x,y 坐标Char或者TCHAR类型 与Windows API有直接联系的用szAppName[10]形式否则用FileName[10]形式,单个字符也可用小写字母表示;Int类型 nCmdShow;

CopyRight owned by the original author.--(www.MegaEntry.com)

LONG类型 lParam;UINT类型 uNotify; 

MegaEntry - Social networking and discussion site!

DWORD类型 dwStart;PSTR类型 pszTip;LPSTR类型 lpCmdLine

MegaEntry - Social networking and discussion site!

LPTSTR类型 lpszClassName;LPVOID类型 lpReservedWPARAM类型 wParam,

MegaEntry - Social networking and discussion site!

LPARAM类型 lParamHWND类型 hDlg;HDC类型 hDC;

CopyRight owned by the original author.--(www.MegaEntry.com)

HINSTANCE类型 hInstanceHANDLE类型 hInstance,HICON类型 hIcon;

CopyRight owned by the original author.--(www.MegaEntry.com)

int iTmpfloat fTmpDWORD dw*

MegaEntry - Social networking and discussion site!

String , AnsiString str *m_ 类成员变量 m_nVal, m_bFlagg_ 全局变量 g_nMsg, g_bFlag

MegaEntry - Social networking and discussion site!

局部变量中可采用如下几个通用变量:nTemp,nResult,I,J(一般用于循环变量)。其他资源句柄同上.3常量命名和宏定义

MegaEntry - Social networking and discussion site!

常量和宏定义必须具有一定的实际意义;常量和宏定义在#include和函数定义之间;常量和宏定义必须全部以大写字母来撰写,中间可根据意义的连续性用下划线连接,每一条定义的右侧必须有一简单的注释,说明其作用;

CopyRight owned by the original author.--(www.MegaEntry.com)

资源名字定义格式:菜单:IDM_XX或者CM_XX位图:IDB_XX

MegaEntry - Social networking and discussion site!

对话框:IDD_XX字符串:IDS_XXDLGINIT:DIALOG_XX

CopyRight owned by the original author.--(www.MegaEntry.com)

ICON:IDR_XX.4函数命名函数原型说明包括引用外来函数及内部函数,外部引用必须在右侧注明函数来源: 模

MegaEntry - Social networking and discussion site!

块名及文件名, 如是内部函数,只要注释其定义文件名;第一个字母必须使用大写字母,要求用大小写字母组合规范函数命名,必要时可用下划线间隔,示例如下:void UpdateDB_Tfgd (TRACK_NAME); //Module Name :r01/sdw.c

CopyRight owned by the original author.--(www.MegaEntry.com)

void PrintTrackData (TRACK_NAME); //Module Name :r04/tern.cvoid ImportantPoint (void); //Module Name :r01/sdw.cvoid ShowChar (int , int , chtype); //Local Module

MegaEntry - Social networking and discussion site!

void ScrollUp_V (int , int); //Local Module.5结构体命名结构体类型命名必须全部用大写字母,原则上前面以下划线开始;结构体变量命名必须用

MegaEntry - Social networking and discussion site!

大小写字母组合,第一个字母必须使用大写字母,必要时可用下划线间隔。对于私有数据区,必须注明其所属的进程。全局数据定义只需注意其用途。示例如下:typedef struct

MegaEntry - Social networking and discussion site!

{char szProductName[20];char szAuthor[20];

MegaEntry - Social networking and discussion site!

char szReleaseDate[16];char szVersion[10]; 

CopyRight owned by the original author.--(www.MegaEntry.com)

unsigned long MaxTables;unsigned long UsedTables;}DBS_DATABASE;

CopyRight owned by the original author.--(www.MegaEntry.com)

DBS_DATABASE GdataBase;6 控件的命名:

MegaEntry - Social networking and discussion site!

用小写前缀表示类别用小写前缀表示类别:fm 窗口cmd 按钮cob combo,下拉式列表框

MegaEntry - Social networking and discussion site!

txt 文本输入框lab labal,标签img image,图象pic picturegrd Grid,网格scr 滚动条

MegaEntry - Social networking and discussion site!

lst 列表框frm fram7注释

CopyRight owned by the original author.--(www.MegaEntry.com)

原则上注释要求使用中文;文件开始注释内容包括:公司名称、版权、作者名称、时间、模块用途、背景介绍等,复杂的算法需要加上流程说明;

MegaEntry - Social networking and discussion site!

函数注释包括:输入、输出、函数描述、流程处理、全局变量、调用样例等,复杂的函数需要加上变量用途说明;程序中注释包括:修改时间和作者、方便理解的注释等;

MegaEntry - Social networking and discussion site!

引用一: 文件开头的注释模板/******************************************************************

CopyRight owned by the original author.--(www.MegaEntry.com)

** 文件名:** Copyright (c) 1998-1999 *********公司技术开发部** 创建人:

CopyRight owned by the original author.--(www.MegaEntry.com)

** 日 期:** 修改人:** 日 期:

CopyRight owned by the original author.--(www.MegaEntry.com)

** 描 述:**** 版 本:

CopyRight owned by the original author.--(www.MegaEntry.com)

**-----------------------------------------------------------------------------

MegaEntry - Social networking and discussion site!

******************************************************************/

CopyRight owned by the original author.--(www.MegaEntry.com)

引用二: 函数开头的注释模板/******************************************************************* 函数名:

MegaEntry - Social networking and discussion site!

** 输 入: a,b,c** a---** b---

CopyRight owned by the original author.--(www.MegaEntry.com)

** c---** 输 出: x---** x 为 1, 表示...

MegaEntry - Social networking and discussion site!

** x 为 0, 表示...** 功能描述:** 全局变量:

CopyRight owned by the original author.--(www.MegaEntry.com)

** 调用模块:** 作 者:** 日 期:

MegaEntry - Social networking and discussion site!

** 修 改:** 日 期:** 版本

CopyRight owned by the original author.--(www.MegaEntry.com)

****************************************************************/引用三: 程序中的注释模板/*----------------------------------------------------------*/

MegaEntry - Social networking and discussion site!

/* 注释内容 *//*----------------------------------------------------------*/8 程序

MegaEntry - Social networking and discussion site!

a. 程序编码力求简洁,结构清晰,避免太多的分支结构及太过于技巧性的程序,尽量不采用递归模式。b. 编写程序时,亦必须想好测试的方法,换句话说,”单元测试” 的测试方案应在程序编写时一并拟好。

CopyRight owned by the original author.--(www.MegaEntry.com)

c. 注释一定要与程序一致。d. 版本封存以后的修改一定要将老语句用/* */ 封闭,不能自行删除或修改,并要在文件及函数的修改记录中加以记录。e. 程序中每个block 的开头 ”{" 及 "}” 必须对齐,嵌套的block 每进一套,

MegaEntry - Social networking and discussion site!

缩进一个tab,TAB 为4个空格,block类型包括if、for、while、do等关键字引出的。f. 对于比较大的函数,每个block 和特殊的函数调用,都必须注明其功能,举例如下:

MegaEntry - Social networking and discussion site!

count.divisor = 1193280 / freq; // compute the proper countOutByte((unsigned short)67, (unsigned char)182); // tell 8253 that acount is comingOutByte((unsigned short)66, count. c[0]); // send low-order byte

MegaEntry - Social networking and discussion site!

OutByte((unsigned short)66, count. c[1]); // send high-order byte×××××××××××××××××××××××××××××××××××××××

MegaEntry - Social networking and discussion site!

bcb,delphi中的变量命名:遵循匈牙利命名法,命名必须有意义,制定如下规定

MegaEntry - Social networking and discussion site!

窗体: 以大写的W开始,如About版权窗体, 命名为WAbout文件:以大写的F开始,如About版权窗体,文件命名为FAbout.cpp按钮(Button):如退出按钮,命名为btnExit

CopyRight owned by the original author.--(www.MegaEntry.com)

……基类: 加base标记,如报表基类,窗体命名为:WBaseRep, 文件命名为FBaseRep.cpp 


Reply To This Message
Subject: 

Copyright 2005-2006 megaentry,All Rights Reserved
IE 6.0 or above is perfect