写好代码的10大秘诀

上传人:wt****50 文档编号:43332815 上传时间:2018-06-05 格式:PDF 页数:80 大小:215.45KB
返回 下载 相关 举报
写好代码的10大秘诀_第1页
第1页 / 共80页
写好代码的10大秘诀_第2页
第2页 / 共80页
写好代码的10大秘诀_第3页
第3页 / 共80页
写好代码的10大秘诀_第4页
第4页 / 共80页
写好代码的10大秘诀_第5页
第5页 / 共80页
点击查看更多>>
资源描述

《写好代码的10大秘诀》由会员分享,可在线阅读,更多相关《写好代码的10大秘诀(80页珍藏版)》请在金锄头文库上搜索。

1、110 Things You Can Do To Write Better Code ? Development Manager Microsoft Research, ChinaClick to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-2? - Solid and Robust Code ? - Maintainable and Simple Code ? - Fast Code ? - Small Code ? - Re-usable Code ? - Testable Code ? - Portab

2、le Code? ? ?Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-3Why is this code bad?void MyGirlFriendFunc(CORP_DATA InputRec, int CrntQtr, EMP_DATA EmpRec, float EstimRevenue, float YTDRevenue, int ScreenX, int ScreenY, COLOR_TYPE newColor, COLOR_TYPE PrevColor, STATUS_TYPE s

3、tatus, int ExpenseType) int i; for (i=1; i 200 lines! Study in 1986 on IBM s OS/360: the most error-prone routines = longer than 500 lines. Study in 1991 on 148,000-line of code: functions 2.4 times less expensive to fix than longer functions.Omsg_cxx.tlkOver 1400 lines!Click to buy NOW!PDF-XCHANGEw

4、ww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-14Look at these codesLet s look at these codes from our own projects: Small functions: Larger functions: Then look at those from Windows 2000 source tree: NT Kernel Mode Code: NT User Mode CPP File: NT User Mode Header File:align_cpp.tlkvariable_cpp.tlkEv

5、ent_c.tlkwrite_cpp.tlkWrite_h.tlkClick to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-152.? - Use Naming Conventions Hungarian Notation Prefix-BaseTag-Name Standard Prefix: p A pointer. CHAR* psz; rg An array. DWORD rgType; c A count of items. DWORD cBook; h A handle. HANDLE hFi

6、le; Standard “ BaseTag” void - v int - i BOOL - f UINT - ui BYTE - b CHAR - ch WCHAR - wch ULONG - ul LONG - l DWORD - dw HRESULT - hr fn - function sz - NULL str USHORT, SHORT, WORD - w C+ member variables start with: m_ Global variables start with: g_Click to buy NOW!PDF-XCHANGEwww.docu-Click to b

7、uy NOW!PDF-XCHANGEwww.docu-16Let s try these A flag indicates whether a C+ object has been initialized: BOOL m_fInitialized; A Session ID: DWORD dwSessionID; A ref count in a C+ object: LONG m_cRef; / skip the l A Pointer to BYTE buffer: BYTE* pbBuffer; A global logfile filename buffer: CHAR g_szLog

8、FileMAX_PATH; A pointer to a global logfile: CHAR* g_pszLogFile;Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-173.?, ? - Evil goto s? Maybe NotWhy goto is bad? Creates unreadable code Causes compiler to generate non-optimal code Why goto is good? Cleaner code Single entry

9、, single exit Less duplicate codeClick to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-18Spot the gotosSTART_LOOP: If (fStatusOk) if (fDataAvaiable) i = 10; goto MID_LOOP; else goto END_LOOP; else for (I = 0; I = 0) return TRUE; Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy N

10、OW!PDF-XCHANGEwww.docu-34Spot the detectBOOL SomeProblem(USHORT x) while (-x = 0) return TRUE; Infinite loop! Unsigned never negative!Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-35Spot the detectwcscpy(wcServerIpAdd, WinsAnsiToUnicode(cAddr, NULL);Note: WinsAnsiToUnicod

11、e is a private API which allocates a new UNICODE string given an ANSI string.Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-36Spot the detectwcscpy(wcServerIpAdd, WinsAnsiToUnicode(cAddr, NULL);WinsAnsiToUnicode can return NULL which will cause a program crashWinsAnsiToUni

12、code can return allocated memory which will cause a leakClick to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-37A better versionLPWSTR tmp; tmp = WinsAnsiToUnicode(cAddr, NULL); if (tmp != NULL) wcscpy(wcServerIpAdd, tmp); WinsFreeMemory(PVOID)tmp); else return(ERROR_NOT_ENOUGH_M

13、EMORY); Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-385. ?, ? - Handle The Error Cases: They Will Occur!Common examples: Out of memory Exceptions being thrown Registry keys missing Networks going down This is the hardest code to test so it better be right! Don t fail in

14、 C+ object constructor, you can t detect it!Click to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu-39Error cases (continued)In an error case, the code should Recover gracefully Get to a consistent state Clean up any resources it has allocated Let its caller know Interface should define and document the behavior (return status, throw exception) Code should adhere to that definition What not to do Ignore the error Crash ExitClick to buy NOW!PDF-XCHANGEwww.docu-Click to buy NOW!PDF-XCHANGEwww.docu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 文化创意

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号