zoukankan      html  css  js  c++  java
  • C# Rules

    Design Guidelines for Developing Class Libraries

    No.1

    Keep classes size small.

    Large class trying to do too many things.

    Figure out responsibly, use delegation and collaborate small classes

    No.2

    Keep methods short.

    Normally, 1-10 lines is ok, 10-20 lines is pushing it, may be need a quick check. 20-40 lines usually need to break.

    Solution: divide it into methods.

    No.3

    Try to avoid comments, unless its for API documents.

    If comments have any purpose, it will be relieve the tension of the code, try to explain the code function. In that case, I intend to refactor or abstract it to  method to self-explain.

    No.4

    Try to avoid multiple Exits

    If we have single entry point, we should have single exit point.

    use variable to hold return value, then return that variable at the end of the code.

    No.5

    Encapsulate complex expressions

    The express may have lots of compare operation, like and, or, >,<. use method to encapsulate them, give it a meaningful name, return the compare result.

    No.6

    Treat warnings as Errors.

    Use VS build-in function to force compile fails when there has a warning.

    No.7

    Avoid too man parameters.

    Use class to wrap those parameters, too many parameters meaning there may have some connections between them and use class could provide some validation function

    No.8

    Avoid Boolean Parameters.

    Why, because it may cause confusion in caller’s code. whenever someone invoke your API, there has a method with Boolean parameters, the reader doesn’t really know what’s that bool stands for, instead, rename the methods, provide multiple methods, will make your caller’s code much more cleaner.

    No.9

    Use Exceptions For Errors

    Stop use boolean or status code for return value, if caller pass in some invalid parameters. Use exception could explicit tell caller what’s wrong, the cause of error may come from caller’s validation code. like check empty value of parameter.

    No. 10

    Avoid Use Regions

    Generally, Regions are used to hide ugly code. Each of the region may could be a class, too many regions may meaning this class doing too many things.

    Happy Coding Everyday~ 快乐编码,享受生活~
  • 相关阅读:
    HDU 4814 Golden Radio Base 模拟
    Java提高篇(三二)-----List总结
    让你提前认识软件开发(28):数据库存储过程中的重要表信息的保存及相关建议
    BBSXP最新漏洞 简单注入检測 万能password
    CodeForce 356A Knight Tournament(set应用)
    POJ--3268--Silver Cow Party【SPFA+邻接表】
    strip 命令的使用方法
    MySQL创建用户权限结果Trigger失败
    Cocos2d-x3.0下一个 Lua与C++打电话给对方
    sharepoint 2013 userprofile 用户信息
  • 原文地址:https://www.cnblogs.com/tedzhang/p/2498602.html
Copyright © 2011-2022 走看看