C# 班级布局

C# 班级布局,c#,C#,C#中的课程是否有明确的推荐布局 下面是一个布局问题的示例 public class DinnerParty { //>>>>should the const fields that are initiallised with a value be at the top? private const int CostOfFoodPerPerson = 25; //>>>>should the construct

C#中的课程是否有明确的推荐布局

下面是一个布局问题的示例

 public class DinnerParty {

      //>>>>should the const fields that are initiallised with a value be at the top?
    private const int CostOfFoodPerPerson = 25;

      //>>>>should the constructor always be placed near the top of the class?
    public DinnerParty(int numberOfPeople, bool Health, bool fancyDecorations) {
        NumberOfPeople = numberOfPeople;
        this.fancyDecorations = fancyDecorations;
        Health = healthyOption;
        CalculateCostOfDecorations(fancyDecorations);
    }

      //>>>>backing private fields should always precede properties that use them?
    private int numberOfPeople;
    public int NumberOfPeople {
        get {
            return numberOfPeople;
        } 
        set {
            CalculateCostOfDecorations(fancyDecorations);
            numberOfPeople = value;
        }
    }

      //>>>>where do these fields go?
    private bool fancyDecorations;
    public decimal costOfBeveragePerPerson;

    private bool healthyOption;
    public bool HealthyOption{
        set {
            value =  healthyOption;
            if (healthyOption) {
                costOfBeveragePerPerson = 5.0M;
            } else {
                costOfBeveragePerPerson = 20.0M;
            }
        }
    }

     //>>>>should methods be placed after the constructor and all properties?
    public decimal costOfDecorations = 0;
    void CalculateCostOfDecorations(bool fancy) {
        if (fancy) {
            costOfDecorations = (numberOfPeople * 15.0M) + 50.0M;
        } else {
            costOfDecorations = (numberOfPeople * 7.50M) + 30.0M;
        }
    }

    public decimal CalculateCost(bool xfancy, bool xhealthyOption) {
        decimal totalCost = (numberOfPeople * (CostOfFoodPerPerson + costOfBeveragePerPerson)) + costOfDecorations;
        if (xhealthyOption) {
            return totalCost * 0.95M;
        } else {
            return totalCost;
        }
    }
 }

StyleCop有以下规则:

  • 在类、结构或接口中,元素必须按以下顺序定位:

    • 恒定场
    • 田地
    • 建设者
    • 终结器(析构函数)
    • 代表
    • 事件
    • 列举
    • 接口
    • 性质
    • 索引器
    • 方法
    • 结构
    • 班级
  • 相同类型的元素必须按访问级别按以下顺序放置:

    • 公开的
    • 内部的
    • 保护内部
    • 保护
    • 私人的
  • 所有静态元素必须放置在相同类型的所有实例元素之上

有关详细信息,请参阅。

我个人认为

  • 变量声明
  • 方法
  • 性质 …按顺序
其他人有自己的偏好。我能想到的唯一一条硬性规定就是要始终如一。不管你的偏好如何,如果你清楚且可预测,这会让下一个男人更容易接受。

我倾向于:

  • 事件
  • 字段(包括常量)
  • 属性(包括索引器和自动属性)
  • 建设者
  • 公共/受保护/内部/私有方法
  • 嵌套类型
也就是说,我尽量尊重文件的顺序,因为不必要地移动内容会导致合并痛苦

我不确定是否有一个明确的方式来编写课堂内容;即使有,我也会采取一小撮盐,在一天结束时,这也只是某人的意见。保持一致,尽量保持代码整洁(我个人讨厌代码文件中乱七八糟的空格,因为按Enter键太多次)


帮助合并保持良好效果的另一个重要因素是在代码格式上有一致的设置(行内括号与新行括号、单行if语句周围的括号等)。

看起来这可能会结束;我怀疑这个问题不够具体——有没有人得到过修改这个问题以使其更易于接受的提示?(我觉得这个问题的可能答案很有用)它属于半代码审查领域,而SO、代码审查或程序员并没有真正涉及到这一领域。我不认为这是一个好问题,但它可能会成为一个合格的社区维基。@Adamhuldsworth我如何将其切换到社区维基?实际上,请阅读:@Adamhuldsworth OK-我将其中一篇文章标记为答案,然后尝试将其标记为社区维基当然,这些规则是可选的;-)@亚当霍尔德斯沃思:是的,当然。但它们是.NET世界公认的标准。@Adamhuldsworth:使用ReSharpers Clean Code facility、StyleCop插件和匹配配置,您可以在几分钟内更改完整的代码库,以遵守规则。目的是向路人强调您的答案不是“明确的推荐方式”,这只是一种观点,因为它有工具支持而被广泛使用。当然,它让合并和历史有点痛苦。我只是觉得它没有任何价值。因为文件是按特定顺序编写的,所以我的工作效率没有提高。对我来说,这纯粹是美学。