Spot the defect 以中国研究院 DWORD status NO ERROR LPWSTR ptri Status f(..., &ptr )i if( Status isnot NO ERROR or ptr is NULL goto cleanup i
11 Spot the defect DWORD Status = NO_ERROR; LPWSTR ptr; Status = f( ..., &ptr ); if( Status isnot NO_ERROR or ptr is NULL ) goto cleanup; is??? isnot??? or??? ❖new C/C++ keywords? No. ❖overloaded operators? No. ❖just some confusing macros …
a better version 以中国研究院 DWORD status NO ERROR LPWSTR ptr NULLi Status f(..., &ptr )i if((Status NO ERROR)I(ptr NULL) goto cleanup i Make your intent explicit Use existing language constructs v Everybody understands them Avoid future problems Initialize parenthesize
12 A better version DWORD Status = NO_ERROR; LPWSTR ptr = NULL; Status = f( ..., &ptr ); if( (Status != NO_ERROR) || (ptr == NULL) ) goto cleanup; • Make your intent explicit ✓Use existing language constructs ✓ Everybody understands them • Avoid future problems: ✓Initialize ✓parenthesize
What's the maximum length of a function? Microsoft 软中国研究院 Look at this example a over 400m Watch out for functions> 200 lines Study in 1986 on IBMS OS/360: the most error-prone routines = longer than 500 lines Study in 1991 on 148,000-line of code functions <143 lines =>2.4 times less expensive to fix than longer functions
13 What’s the maximum length of a function? • Look at this example • Watch out for functions > 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 < 143 lines => 2.4 times less expensive to fix than longer functions. Over 1400 lines!!!
Look at these codes )救中国研究院 Let's look at these codes from our own projects Small functions nm量 Larger functions: a Then look at those from Windows 2000 source tree NT Kernel mode Code. a Eetc NT User Mode CPP File ne gp. NT USer Mode Header File: withs
14 Look at these codes • Let’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:
2取个好名字- Use Naming Conventions Microsoft 微软中国研究院 Hungarian notation PRefix -Base Tag-Name Standard prefix: p-A pointer. CHAR* psz rg An array. DWORD rgType[. C-A count of items. DWORD CBook h -A handle. HANDLE hFile Standard“ BaseTag void int Boo工 f UINT BYTE CHAR ch WCHAR wch ULONG LONG >1 DWORD dw HRESULT - hr fn function SZ NULL str USHORT, SHORT, WORD C++ member variables start with: m Global variables start with: g
15 2.取个好名字 - 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 hFile; • 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_