热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#开发者的代码审查清单

概述这是为C#开发者准备的通用性代码审查清单,可以当做开发过程中的参考。这是为了确保在编码过程中,大部分通用编码指导原则都能注意到。对于新手和缺乏经验&
概述

这是为C# 开发者准备的通用性代码审查清单,可以当做开发过程中的参考。这是为了确保在编码过程中,大部分通用编码指导原则都能注意到。对于新手和缺乏经验(0 到 3 年工作经验)的开发者,参考这份清单编码会很帮助。

清单
  1. 确保没有任何项目警告(VS project warnings)。
  2. 如果先执行Code Analysis(启用所有Microsoft Rules)再消除所有警告就更好了。
  3. 去掉所有没有用到的 using。编码过程中去掉多余代码是个好习惯。

    参考: http://msdn.microsoft.com/en-us/magazine/ee335722.aspx.

  4. 在合理的地方检查对象是否为‘null’,避免运行的时候出现 Null Reference Exception。
  5. 始终遵循命名规范。一般而言变量参数使用Camel 命名法,方法名和类名使用Pascal命名法。

    参考: http://msdn.microsoft.com/en-us/library/ms229043.aspx.

  6. 请确保你意识到 SOLID 原则。

    维基百科定义: 在程序设计领域,SOLID (单一职责、开闭原则、里氏替换、接口分离以及依赖反转) 是由罗伯特·C·马丁在 21 世纪早期引入的记忆术首字母缩略字,指代了面向对象编程和面向对象设计的五个基本原则。当这些原则被一起应用时,它们使得一个程序员开发一个容易进行软件维护和扩展的系统变得更加可能。SOLID 所包含的原则是通过引发编程者进行软件源代码的代码重构进行软件的代码异味清扫,从而使得软件清晰可读以及可扩展时可以应用的指南。SOLID 被典型的应用在测试驱动开发上,并且是敏捷开发以及自适应软件开发的基本原则的重要组成部分。

    参考: http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

  7. 代码可重用性。如果一块代码已经被使用超过一次,或者你希望将来使用它,请提取成一个方法。将重复的工作做成通用的方法放在相关的类中,这样一旦你完成别人就可以使用了。将常用功能开发成用户控件(并非指ASP.NET中的用户控件),这样可以跨项目重用它们。

    参考:

    • http://msdn.microsoft.com/en-us/library/office/aa140806(v=office.10).aspx
    • http://blogs.msdn.com/b/frice/archive/2004/06/11/153709.aspx
  8. 代码一致性。比方说,Int32 写成 int,String 写成 string,应该在代码里保持统一形式。不能一会二写成 int 一会儿写成 Int32。
  9. 代码可读性。代码应该是可维护的,便于其他开发者理解。

    参考: http://msdn.microsoft.com/en-IN/library/aa291591(v=vs.100).aspx

  10. 释放非托管资源,比如文件I/O,网络资源等。一旦使用结束就应该释放它们。如果你想一旦超出使用范围就自动释放对象,可以使用 using 将非托管代码括起来。

    参考: http://msdn.microsoft.com/en-us/library/498928w2.aspx

  11. 合理实现异常处理(try/catch 和 finally 块)和记录异常。

    参考: http://msdn.microsoft.com/en-us/library/vstudio/ms229005(v=vs.100).aspx

  12. 确保代码中方法的行数不要过多,不超过 30 到 40 行。
  13. 及时用代码管理工具 check-in/check-out 代码。(比如 TFS)  

    参考: http://www.codeproject.com/Tips/593014/Steps-Check-in-Check-Out-Mechanism-for-TFS-To-avoi

  14. 相互审查代码。和你的同事交换代码,实现内部审查。
  15. 单元测试。编写开发测试用例完成单元测试,确保代码被送到 QA 以前,基本测试完成。

    参考: http://msdn.microsoft.com/en-us/magazine/cc163665.aspx

  16. 尽量避免 for/foreach 循环嵌套和 if 条件嵌套。Avoid nested for/foreach loops and nested if conditions as much as possible.
  17. 如果代码只会使用一次,请使用匿名类型。Use anonymous types if code is going to be used only once.

    参考: http://msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx

  18. 尽量使用 LINQ 查询和 Lambda 表达式,增加可读性。Try using LINQ queries and Lambda expressions to improve Readability.

    参考: http://msdn.microsoft.com/en-us/library/bb308959.aspx

  19. 合理使用 var、object 和 dynamic 关键字。由于很多开发者会感到困惑或者知道的很少,会觉得它们有些相似,故而交换使用,这是要避免的。Proper usage of var, object, and dynamic keywords. They have some similarities due to which most of the developers are confused or don’t know much about them and hence they use them interchangeably, which shouldn't be the case.

    参考: http://blogs.msdn.com/b/csharpfaq/archive/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords.aspx

  20. 使用访问限定符(private, public, protected, internal, protected internal)限定每个方法、类或变量的需要范围。比方说如果一个类只会在程序集内使用,那么定义成 internal 就足够了。Use access specifiers (private, public, protected, internal, protected internal) as per the scope need of a method, a class, or a variable. Let's say if a class is meant to be used only within the assembly, then it is enough to mark the class as internal only.

    参考: http://msdn.microsoft.com/en-us/library/kktasw36.aspx

  21. 在需要保持解耦的地方使用接口,有些设计模式的出现也是由于接口的使用。Use interfaces wherever needed to maintain decoupling. Some design patterns came into existence due to the usage of interfaces.

    参考: http://msdn.microsoft.com/en-IN/library/3b5b8ezk(v=vs.100).aspx

  22. 按照用法和需要将类定义为 sealed、static 或 abstract。Mark a class as sealed or static or abstract as per its usage and your need.

    参考: http://msdn.microsoft.com/en-us/library/ms173150(v=vs.100).aspx

  23. 如果需要多次串联,请使用 Stringbuilder 代替 string,这可以节省堆内存。Use a Stringbuilder instead of string if multiple concatenations are required, to save heap memory.
  24. 检查是否有不可能执行的代码,如果有,请修改。Check whether any unreachable code exists and modify the code if it exists.
  25. 在每个方法前注释,说明它的用法、输入类型和返回值类型信息。Write comments on top of all methods to describe their usage and expected input types and return type information.
  26. 使用类似 Silverlight Spy 的工具,检查和操控 Silverlight 应用在运行时对 XMAL 的渲染,以此来改善效率。这可以在设计执行 XAML 时,节省大量退回和来回修改的时间。Use a tool like Silverlight Spy to check and manipulate rendered XAML in Runtime of a Silverlight application to improve productivity. This saves lot of back & forth time between Design & Run views of the XAML.
  27. 使用 filddler 工具通过检查 HTTP/网络流量和带宽,来跟踪 web 应用和服务的性能。Use fiddler tool to check the HTTP/network traffic and bandwidth information to trace the performance of web application and services.
  28. 如果你想确认 Visual Studio 以外的方法,请使用 WCFTestClient.exe 工具,或者装载它的进程到 Visual Studio 来进行调试。Use WCFTestClient.exe tool if you want to verify the service methods out of the Visual Studio or by attaching its process to Visual Studio for debugging purposes.
  29. 在任何合理的地方使用 constants 和 readonly。Use constants and readonly wherever applicable.

    参考:

    • http://msdn.microsoft.com/en-us/library/acdd6hb7(v=vs.100).aspx
    • http://msdn.microsoft.com/en-us/library/e6w8fe1b(v=vs.100).aspx
  30. 尽量避免强制转换和类型转换,因为会造成性能损失。Avoid type casting and type conversions as much as possible; because it is a performance penalty.

    参考: http://msdn.microsoft.com/en-us/library/ms173105.aspx

  31. 对于你想提供自定义信息的类,请重载 ToString (来自 Object 类)。Override ToString (from Object class) method for the types which you want to provide with custom information.

    参考: http://msdn.microsoft.com/en-us/library/ms173154(v=vs.100).aspx

  32. 避免直接从其他代码中 ctrl+c/ctrl+v。一直建议还是自己用手敲,即使你已经找到相关代码。这样可以锻炼自己写代码能力,还能正确理解那段代码的用法。最终你永远都不会忘记那段代码。Avoid straightaway copy/pasting of code from other sources. It is always recommended to hand write the code even though if you are referring to the code from some sources. By this, you will get good practice of writing the code yourself and also you will understand the proper usage of that code; finally you will never forget it.
  33. 保持阅读书籍和文章的良好习惯,遵循大神们的实践指导。(比如微软专家和一些著名的专家,Martin Fowler, Kent Beck, Jeffrey Ritcher, Ward Cunningham, Scott Hanselman, Scott Guthrie, Donald E Knuth.)Always make it a practice to read books/articles, upgrade and follow the Best Practices and Guidelines by industry experts like Microsoft experts and well-known authors like Martin Fowler, Kent Beck, Jeffrey Ritcher, Ward Cunningham, Scott Hanselman, Scott Guthrie, Donald E Knuth.
  34. 确认代码是否有内存泄漏。如果有,请确保已修正。Verify whether your code have any memory leakages. If yes, make sure that they have been fixed.

    Refer: http://blogs.msdn.com/b/davidklinems/archive/2005/11/16/493580.aspx

  35. 尽可能参加专家们组织的技术研讨会,可以接触到最新的软件趋势、技术和最佳实践。Try attending technical seminars by experts to be in touch with the latest software trends and technologies and best practices.
  36. 要透彻理解 OOP 概念,并尽可能在代码里实现。Understand thoroughly the OOPs concepts and try implementing it in your code.
  37. 知道项目设计架构,可以从整体上理解程序的执行流程。Get to know about your project design and architecture to better understand the flow of your application as a whole.
  38. 采取必要措施阻止避免任何交叉脚本攻击、SQL 注入和其他安全漏洞。Take necessary steps to block and avoid any cross scripting attacks, SQL injection, and other security holes.
  39. 永远记得将保密和敏感信息加密(通过使用好的加密算法),比如保存到数据库的密码和保存在 web.config 文件中的连接字符,要避免被非认证的用户操纵。Always encrypt (by using good encryption algorithms) secret/sensitive information like passwords while saving to database and connection strings stored in web.config file(s) to avoid manipulation by unauthorized users.
  40. 避免对已知类型(原始类型)使用默认关键字,比如 int, decimal, bool 等。多数情况下,如果不确定是值类型还是引用类型,就使用泛型类型(T)。Avoid using default keyword for the known types (primitive types) like int, decimal, bool, etc. Most of the times, it should be used in case of Generic types (T) as we may not be sure whether the type is a value type or reference type.

    参考: http://msdn.microsoft.com/en-us/library/xwth0h0d(v=vs.100).aspx

  41. 微软(在代码分析条例和指导中)并不推荐使用’out’和’ref’,这些关键字是通过引用传参,请注意,’ref’参数在传入被调用方法之前,应当在调用方法中先初始化,但’out’参数就不是这样。Usage of 'out' and 'ref' keywords be avoided as recommended by Microsoft (in the Code analysis Rules and guidelines). These keywords are used to pass parameters by reference. Note that 'ref' parameter should be initialized in the calling method before passing to the called method but for 'out' parameter this is not mandatory.

    参考: http://msdn.microsoft.com/en-us/library/ms182131(v=vs.100).aspx

引用: http://www.codeproject.com/Articles/593751/Code-Review-Checklist-and-Guidelines-for-Csharp-De

转:https://www.cnblogs.com/jes_shaw/archive/2013/06/13/3134171.html



推荐阅读
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 如何去除Win7快捷方式的箭头
    本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
author-avatar
手机用户2502923697
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有