代码高手十大秘诀 以中国研究院 1.集百家之长,归我所用 Follow basic Coding Style 2.取个好名字- Use Naming Conventions 3.凌波微步,未必摔跤- Evil gotos? Maybe Not 4.先发制人,后发制于人- Practice Defensive Coding 5.见招拆招,滴水不漏- Handle the error Cases: They will occur!
6 代码高手十大秘诀 1. 集百家之长, 归我所用- Follow Basic Coding Style 2. 取个好名字 - Use Naming Conventions 3. 凌波微步, 未必摔跤 - Evil goto’s? Maybe Not… 4. 先发制人, 后发制于人- Practice Defensive Coding 5. 见招拆招, 滴水不漏 - Handle The Error Cases: They Will Occur!
代码高手十大秘诀(Cont) )救中国研究院 6.熟习剑法刀术,所向无敌- Learn win32 API Seriously 7.双手互搏,无坚不摧-Test, but dont stop there 8.活用段言-Use,don' t abuse, assertions 9.草木皆兵,不可大意- Avoid Assumptions 10.最高境界,无招胜有招- Stop writing So much code
7 代码高手十大秘诀 (Cont.) 6. 熟习剑法刀术, 所向无敌- Learn Win32 API Seriously 7. 双手互搏, 无坚不摧 - Test, but don’t stop there 8. 活用段言 - Use, don’t abuse, assertions 9. 草木皆兵, 不可大意 - Avoid Assumptions 10. 最高境界, 无招胜有招- Stop writing so much code
1.集百家之长,归我所用 Follow Basic Coding Style Microsoft 软中国研究院 Your code may outlive you or your memory Think about the maintainer Comment your code File header Function Header Inline Commenting Pick a good name for your functions and variables Set Tab/Indent to 4 characters Align your code Less arguments
8 • Your code may outlive you or your memory! • Think about the maintainer • Comment your code – File Header – Function Header – Inline Commenting • Pick a good name for your functions and variables • Set Tab/Indent to 4 characters • Align your code! • Less arguments 1. 集百家之长, 归我所用 - Follow Basic Coding Style
Coding Style(Cont) )救中国研究院 Use all uppercase with underscores for macro define MY MACRO NUMBER 100 Declare all your variables at the start of function Avoid hard coding constant. Use enum or macro define MY MAX LENGTH 1024 User braces for one line code 工E(mf工 nitia1ized) hr = S OK; Limit the length of a single function
9 Coding Style (Cont.) • Use all uppercase with underscores for macro. #define MY_MACRO_NUMBER 100 • Declare all your variables at the start of function. • Avoid hard coding constant. Use enum or macro. #define MY_MAX_LENGTH 1024 • User braces for one line code If (m_fInitialized) { hr = S_OK; } • Limit the length of a single function
Spot the defect 以中国研究院 DWORD status NO ERROR LPWSTR ptri Status f( &ptr if( Status isnot NO ERROR or ptr is NULL goto cleanup i
10 Spot the defect DWORD Status = NO_ERROR; LPWSTR ptr; Status = f( ..., &ptr ); if( Status isnot NO_ERROR or ptr is NULL ) goto cleanup;