微软程序开发制胜策略微软程序员条制胜法则_第1页
微软程序开发制胜策略微软程序员条制胜法则_第2页
微软程序开发制胜策略微软程序员条制胜法则_第3页
微软程序开发制胜策略微软程序员条制胜法则_第4页
微软程序开发制胜策略微软程序员条制胜法则_第5页
已阅读5页,还剩50页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、微软开发制胜策略微软程序员13条制胜法则黄雪斌微软全球技术中心2022-3-171主要内容 微软开发过程概述 微软程序员13条制胜法则编写优质代码四大法则测试代码,安身立命之本四大法则千锤百炼,不败金身五大法则 Q&A2022-3-172编写优质代码 统一代码风格 避免冗长代码 降低代码间耦合 减少冗余代码2022-3-173法则1:统一代码风格 确定统一的编码风格 添加注释 起个好名字 让一切井井有条2022-3-174代码风格一致(Code Style)=public class BugSystem/更多的代码更多的代码=for (int i=0; iarray.length;i+

2、)if ( arrayi 0 )arrayi = -arrayi;/end if/end for=public class BugSystem/更多的代码更多的代码=for (int i=0; iarray.length; i+)if ( arrayi BugAge.Old)currentBug.setPriority(BugPriority.URGENT);2022-3-177添加注释(续) 添加注释的目的 使代码易读、易写、易维护 如何添加注释 代码、数据、算法的解释 做标记(时间、所做的改动等) 标识代码的功能和目的 代码如何调用 避免 对显而易见的内容进行注释 添加大段注释 注释的拼写

3、错误 例子 VS.NET中自带的Duwamish的例子2022-3-178起个好名字if (b1.grd() ba.gt()bl_sys.rem(b1);elsebl_sys.rad(b1);2022-3-179起个好名字(续)if (bug1.getReactivateDate() BugAge.getToday()bugAlertSystem.removeBug(bug1);else bugAlertSystem.resetActivateDate(bug1);2022-3-1710起个好名字(续)/根据Function Spec的规则,如果Bug被重新激活,需要发送一份alert/ema

4、il. 重置BugAlertSystem中今天重新激活的Bug的激活日期,以便/统一发送alert email.if (bug1.getReactivateDate() BugAge.getToday()/Bug不是今天重新激活的,将该不是今天重新激活的,将该Bug从从AlertSystem中移走中移走bugAlertSystem.removeBug(bug1);else/Bug今天重新被激活,重置今天重新被激活,重置bug1在在AlertSystem中的激活日期中的激活日期bugAlertSystem.resetActivateDate(bug1);2022-3-1711起个好名字(续) 大

5、小写问题 Pascal Case:BackColor Camel Case:backColor Upper Case:System.Web.UI 避免混淆int myNumber; int MyNumber; 使用缩写准则 GetWindow vs. GetWin 首字母缩写 避免使用不被广泛接受的首字母缩写 IO vs. InputOut; OLAP vs. On-line Analytical Processing 词语的选择2022-3-1712起个好名字(续) 名字空间、类、接口、方法、参数的命名 名字空间:CompanyName.TechnologyName.Feature.Desi

6、gn 类:使用名词、不要使用前缀、不要使用“_”FileStream、ApplicationException 接口:使用名词或形容词、加前缀“I”、不要使用“_”IServiceProvider、IFormatable 方法:使用动词RemoveAll()、GetCharArray() 参数:camel Case、“见文知义”Type GetType(string typeName) http:/ 类中各个方法的次序 根据public, protected, private分组 每组中再根据各方法直接关系细分 openConnection() sendMessage() closeConne

7、ction() 其他以首字母为序 VS.NET中使用#region / #endregion进行分组2022-3-1714让一切井井有条(续) 提醒自己未完成的任务public class Transmitterstring myDestination;public Transmitter(string aDestination)myDestination = aDestination;public void transmit(string aMessage)openConnection();sendMessage(aMessage);closeConnection();private voi

8、d openConnection()/ToDo 实现实现openConnection() private void sendMessage(string aMessage)/ToDo 实现实现openConnection() private void closeConnection()/ToDo 实现实现closeConnection() 2022-3-1715法则2: 避免冗长代码 一个method多长合适? 曾经看过1400行的一个method 1986年对IBM OS/360的统计结果:绝大多数的错误出在大于500行的函数中 1991年对148,000行代码的统计结果,在大于143行的函

9、数中修复一个Bug需要多花费2.4倍的代价 专家的建议 不要超过一屏 Method Cohension2022-3-1716Method Cohesionpublic void process(string words)/Loop through the array of stringsfor (int i=0; i0; j-)argument += wordsi.Substring(j-1,1);Console.WriteLine(argument);/Test for two particular stringsif (words.Length = 2)if (words0.ToLower

10、().Equals(“coding) & words1.ToLower().Equals(“guru)Console.WriteLine(“Admire);2022-3-1717Method Cohesion(续)private string reverseCharacters(string forward)string reverse = ;for (int j= forward.Length; j0; j-) reverse +=forward.Substring(j-1,1);return reverse;private bool isCodingGuru(string name

11、s)bool IsGuru = false;if (names.Length = 2)if (names0.ToLower().Equals(“coding) & names 1.ToLower().Equals(“guru)Console.WriteLine(“Admire);2022-3-1718Method Cohesion(续)public void process(string words)for (int i=0; iwords.Length; i+)Console.WriteLine(reverseCharacters(wordsi);if (isCodingGuru(w

12、ords)Console.WriteLine(“Admire);2022-3-1719法则3: 降低代码间耦合 耦合无处不在 整体系统的构成依靠耦合 松耦合和紧耦合 避免紧耦合 修改一处代码不会引起更多的变动 便于代码重用、维护和扩充 类之间的耦合 Identity耦合,Representational耦合2022-3-1720法则3: 降低代码间耦合public class BookShelf / Well use an ArrayList to store our BooksArrayList myBookList = new ArrayList();/ Method to add a B

13、ook to the internal ArrayListpublic void addBook(Book aBook) myBookList.Add(aBook); / Method to return an iterator of all the Books heldpublic IEnumerator getBooks() return myBookList.GetEnumerator();2022-3-1721法则3: 降低代码间耦合(续)public class Book / Represents the behaviour Book is designed to deliverpu

14、blic void BookyBehavior() Console.WriteLine(Just being a Book.);/ Book, responsible for itself, knows how to store itselfpublic void storeSelf(BookShelf wc) wc.addBook(this);2022-3-1722法则3: 降低代码间耦合(续)public class BookClientpublic static void main(String args) / Make the ShelfBookShelf myBookShelf =

15、new BookShelf();/ Loop 10 timesfor(int i=0; i10; i+) / Make a new Book, and have it store itself in the Shelfnew Book().storeSelf(myBookShelf);2022-3-1723法则3: 降低代码间耦合(续) 耦合关系表ClassCouple toType Book BookShelf Identity, Representational BookShelf BookIdentity BookClient BookIdentity, Representational

16、 BookClient BookShelf Identity2022-3-1724法则3: 降低代码间耦合(续)public class BookShelf / Well use an ArrayList to store our BooksArrayList myBookList = new ArrayList();/ Method to add a Book to the internal ArrayListpublic void addBook(Book aBook) myBookList.Add(aBook); / Method to return an iterator of all

17、 the Books heldpublic IEnumerator getBooks() return myBookList.GetEnumerator();2022-3-1725法则3: 降低代码间耦合(续)public class Book public void BookyBehavior() Console.WriteLine(Just being a Book.);public class BookClientpublic static void main(String args) BookShelf myBookShelf = new BookShelf();for(int i=0

18、; i 0, FindCustomer出错,customerID应该大于0); / More code.return true;2022-3-1733返回值的验证if ( SetCustomerValid(customerID)Debug.Assert(CustomerIsValid(customerID);UserCustomer(customerID);elseDebug.Assert( !CustomerIsValid(customerID);ReleaseCustomer(customerID);2022-3-1734数据有效性验证Conditional (DEBUG)private

19、void BugValidate(Bug currentBug)Debug.Assert (currentBug.BugID0,“测试测试Bug有效性有效性”, “BugID应该大于应该大于0);Debug.Assert (currentBug.Status != null,“测试测试Bug有效性有效性”, “Bug状态不状态不能为空能为空);Debug.Assert (currentBug.Owner != null,“测试测试Bug有效性有效性”, “Bug负责负责人不能为空人不能为空);/ More here.public bool BugOperation (Bug currentBu

20、g)/More codeBugValidate(currentBug);ResolveBug(currentBug);BugValidate(currentBug);/ More code.return true; 2022-3-1735算法验证 使用Debug代码来验证代码的正确性 方法 对初始状态、中间状态和最终结果使用断言 使用更简单的算法对程序结果进行确认 举例 排序算法中,排序结果的每一个值都大于等于前一个值 压缩算法中,压缩文件解压缩后和原文件匹配 加密算法中,密文解密的结果应该等于原文 Demo2022-3-1736法则7:异常处理 异常处理和Debug代码的关系 异常处理和返回

21、值的关系 编写稳定的应用程序的关键 增强了程序的可扩展性AB 扩展到AIB 错误的处理更加灵活AIJKB (A可以灵活的处理由B引起的错误) 异常不容易被忽视2022-3-1737异常处理(续) 使用返回值判断使用返回值判断public bool Method() Boolean result = false; if(!OpenFile() goto End; if(!ReadFile() goto End; if(!CloseFile() goto End; result = true; End: return result; 使用异常处理使用异常处理public void Method()

22、 try OpenFile(); ReadBytes(); CloseFile(); catch(FileIOException) / handle failure 2022-3-1738抛出异常 根据method的功能,在需要的地方抛出异常 所有的自定义异常类以Exception结尾 自定义异常继承System.ApplicationException,不要继承System.Exceptionpublic class FileNotFoundException:ApplicationException 提供详细的异常信息(Exception Message) 不要在异常信息中提供敏感的安全内

23、容 抛出最为合适的异常2022-3-1739抛出异常(续) 使用Exception Builder方法class Filestring fileName;public byte Read(int bytes)if (! ReadFile(handle, bytes)throw NewFileIOException(); FileException NewFileException()string description = /build localized stringreturn new FileException(description);2022-3-1740处理异常 捕获特定的异常 不

24、要把异常“吃掉”public void Method()tryFile.Open();catch(Exception e) /所有的异常都被所有的异常都被“吃掉吃掉”了了 使用finally释放资源public void UpdateSet()FileStream stream = null;trystream = new FileStream(“SomeFile.dat”, FileMode.Open);finallyif(stream != null) stream.Close();2022-3-1741处理异常(续) 恢复原来的状态public void DoSomething(File

25、Stream fs)Int64 pos = fs.Position;try/do some reading with fs catchfs.Position = pos; / unwind on the failurethrow;/ rethrow 添加更加详细的信息public Int32 GetInt (Int32 array, Int32 index)tryreturn arrayindex;catch(IndexOutOfRangeException e)throw new ArgumentOutOfRangeException(“Parameter index is out of r

26、ange.”);2022-3-1742法则8:编写Unit Test 提倡先写Unit Test后写代码 测试先行 保证程序员在开发前理解Spec 确保Unit Test不会因为进度压力而取消 先定功能,再定细节 不是强制准则 如何写Unit Test 微软内部工具:TTest 其他的.NET Unite Test工具:NUnit, dotNetUnit, HarnessIt,csUnit Demo2022-3-1743小结:测试代码,安家立命之本 Debug vs. Trace 活用断言(Assert) 异常处理 编写Unit Test2022-3-1744千锤百炼,不败金身 Verifier,一个都不放过 安全至上 性能与功能并进 代码审核(Code Review) 及时修复 Bug2022-3-1745法则9: 使用Verifier工具一个都不放过2022-3-1746法则10:安全至上 了解黑客如何攻击产品 安全性是微软产品的最大挑战 让你的产品防御攻击 将安全性看作需要实现的功能之一 使用安

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论