微软程序开发制胜策略微软程序员13条制胜法则

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

微软开发制胜策略——微软程序员13条制胜法则黄雪斌微软全球技术中心2019/9/8微软高级开发管理峰会2主要内容•微软开发过程概述•微软程序员13条制胜法则–编写优质代码——四大法则–测试代码,安身立命之本——四大法则–千锤百炼,不败金身——五大法则•Q&A2019/9/8微软高级开发管理峰会3编写优质代码•统一代码风格•避免冗长代码•降低代码间耦合•减少冗余代码2019/9/8微软高级开发管理峰会4法则1:统一代码风格•确定统一的编码风格•添加注释•起个好名字•让一切井井有条2019/9/8微软高级开发管理峰会5代码风格一致(CodeStyle)========publicclassBugSystem{//更多的代码}========for(inti=0;iarray.length;i++){if(array[i]0){array[i]=-array[i];}//endif}//endfor========publicclassBugSystem{//更多的代码}========for(inti=0;iarray.length;i++)if(array[i]0)array[i]=-array[i];2019/9/8微软高级开发管理峰会6代码风格一致(续)•代码易读,易理解–每个开发人员可以读懂其他人的代码•产品中所有的代码需遵循统一的标准–指定统一编码风格文档–重要的在于让每个开发人员都遵守–将不符合规范的代码当作错误处理•参考:–=/library/en-us/cpgenref/html/cpconnetframeworkdesignguidelines.asp––微软高级开发管理峰会7添加注释//根据bugID获取Bug//在bugHashTable中,Bug作为Object存储,需要进行类型转换BugcurrentBug=(Bug)bugHashTable[bugID];//如果该Bug处于Active状态,并且Bug的激活时间达到Old的时//间,则将该Bug的Priority设置为URGENT。//参见FunctionSpec第34页第二段的规则5if(currentBug.isActive()&¤tBug.ageBugAge.Old){currentBug.setPriority(BugPriority.URGENT);}2019/9/8微软高级开发管理峰会8添加注释(续)•添加注释的目的–使代码易读、易写、易维护•如何添加注释–代码、数据、算法的解释–做标记(时间、所做的改动等)–标识代码的功能和目的–代码如何调用•避免–对显而易见的内容进行注释–添加大段注释–注释的拼写错误•例子–VS.NET中自带的Duwamish的例子2019/9/8微软高级开发管理峰会9起个好名字if(b1.grd()ba.gt()){bl_sys.rem(b1);}else{bl_sys.rad(b1);}2019/9/8微软高级开发管理峰会10起个好名字(续)if(bug1.getReactivateDate()BugAge.getToday()){bugAlertSystem.removeBug(bug1);}else{bugAlertSystem.resetActivateDate(bug1);}2019/9/8微软高级开发管理峰会11起个好名字(续)//根据FunctionSpec的规则,如果Bug被重新激活,需要发送一份alert//email.重置BugAlertSystem中今天重新激活的Bug的激活日期,以便//统一发送alertemail.if(bug1.getReactivateDate()BugAge.getToday()){//Bug不是今天重新激活的,将该Bug从AlertSystem中移走bugAlertSystem.removeBug(bug1);}else{//Bug今天重新被激活,重置bug1在AlertSystem中的激活日期bugAlertSystem.resetActivateDate(bug1);}2019/9/8微软高级开发管理峰会12起个好名字(续)•大小写问题–PascalCase:BackColor–CamelCase:backColor–UpperCase:System.Web.UI•避免混淆intmyNumber;intMyNumber;•使用缩写准则–GetWindowvs.GetWin–首字母缩写•避免使用不被广泛接受的首字母缩写•IOvs.InputOut;OLAPvs.On-lineAnalyticalProcessing–词语的选择2019/9/8微软高级开发管理峰会13起个好名字(续)•名字空间、类、接口、方法、参数的命名–名字空间:CompanyName.TechnologyName[.Feature][.Design]–类:使用名词、不要使用前缀、不要使用“_”FileStream、ApplicationException–接口:使用名词或形容词、加前缀“I”、不要使用“_”IServiceProvider、IFormatable–方法:使用动词RemoveAll()、GetCharArray()–参数:camelCase、“见文知义”TypeGetType(stringtypeName)–=/library/en-us/cpgenref/html/cpconnamingguidelines.asp2019/9/8微软高级开发管理峰会14让一切井井有条•类中各个方法的次序–根据public,protected,private分组–每组中再根据各方法直接关系细分•openConnection()•sendMessage()•closeConnection()–其他以首字母为序–VS.NET中使用#region/#endregion进行分组2019/9/8微软高级开发管理峰会15让一切井井有条(续)•提醒自己未完成的任务publicclassTransmitter{stringmyDestination;publicTransmitter(stringaDestination){myDestination=aDestination;}publicvoidtransmit(stringaMessage){openConnection();sendMessage(aMessage);closeConnection();}privatevoidopenConnection(){//ToDo实现openConnection()}privatevoidsendMessage(stringaMessage){//ToDo实现openConnection()}privatevoidcloseConnection(){//ToDo实现closeConnection()}}2019/9/8微软高级开发管理峰会16法则2:避免冗长代码•一个method多长合适?–曾经看过1400行的一个method–1986年对IBMOS/360的统计结果:绝大多数的错误出在大于500行的函数中–1991年对148,000行代码的统计结果,在大于143行的函数中修复一个Bug需要多花费2.4倍的代价•专家的建议–不要超过一屏•MethodCohension2019/9/8微软高级开发管理峰会17MethodCohesionpublicvoidprocess(string[]words){//Loopthroughthearrayofstringsfor(inti=0;iwords.Length;i++){stringargument=;//Reversethecharactersineachstringfor(intj=words[i].Length;j0;j--){argument+=words[i].Substring(j-1,1);}Console.WriteLine(argument);}//Testfortwoparticularstringsif(words.Length==2){if(words[0].ToLower().Equals(“coding)&&words[1].ToLower().Equals(“guru)){Console.WriteLine(“Admire…);}}}2019/9/8微软高级开发管理峰会18MethodCohesion(续)privatestringreverseCharacters(stringforward){stringreverse=;for(intj=forward.Length;j0;j--){reverse+=forward.Substring(j-1,1);}returnreverse;}privateboolisCodingGuru(string[]names){boolIsGuru=false;if(names.Length==2){if(names[0].ToLower().Equals(“coding)&&names[1].ToLower().Equals(“guru)){Console.WriteLine(“Admire…);}}}2019/9/8微软高级开发管理峰会19MethodCohesion(续)publicvoidprocess(string[]words){for(inti=0;iwords.Length;i++){Console.WriteLine(reverseCharacters(words[i]));}if(isCodingGuru(words)){Console.WriteLine(“Admire…);}}2019/9/8微软高级开发管理峰会20法则3:降低代码间耦合•耦合无处不在–整体系统的构成依靠耦合–松耦合和紧耦合•避免紧耦合–修改一处代码不会引起更多的变动–便于代码重用、维护和扩充•类之间的耦合–Identity耦合,Representational耦合2019/9/8微软高级开发管理峰会21法则3:降低代码间耦合publicclassBookShelf{//We'lluseanArrayListtostoreourBooksArrayListmyBookList=newArrayList();//MethodtoaddaBooktotheinternalArrayListpublicvoidaddBook(BookaBook){myBookList.Add(aBook);}//MethodtoreturnaniteratorofalltheBooksheldpublicIEnumeratorgetBooks(){returnmyBookList.GetEnumerator();}}2019/9/8微软高级开发管理峰会22法则3:降低代码间耦合(续)publicclassBook{//RepresentsthebehaviourBookisdesignedtodeliverpublicvoidBookyBehavior(){Console.WriteLine(JustbeingaBook...);}//Book,responsibleforitself,knowshowtostoreitselfpublicvoidstoreSelf(BookShelfwc){wc.addBook(this);}}2019/9/

1 / 55
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功