代码之家  ›  专栏  ›  技术社区  ›  jason

私有方法命名约定[已关闭]

  •  33
  • jason  · 技术社区  · 17 年前

    _Add “这里?我不喜欢领先的下划线,但这是我的一位队友所建议的。

    public Vector Add(Vector vector) {
        // check vector for null, and compare Length to vector.Length
        return _Add(vector);
    }
    
    public static Vector Add(Vector vector1, Vector vector2) {
        // check parameters for null, and compare Lengths
        Vector returnVector = vector1.Clone()
        return returnVector._Add(vector2);
    }
    
    private Vector _Add(Vector vector) {
        for (int index = 0; index < Length; index++) {
            this[index] += vector[index];
        }
        return this;
    }
    
    14 回复  |  直到 13 年前
        1
  •  34
  •   Konrad Rudolph    8 年前

    我从未见过C#中有任何区分公共方法和私有方法的编码约定。我不建议这样做,因为我看不到它的好处。

    如果方法名与公共方法冲突,那么是时候变得更具描述性了;如果像您的情况一样,它包含实际的方法 实施 *Impl . 即。 AddImpl

        2
  •  30
  •   Andy May    17 年前

    private Vector add(Vector vector) {
        for (int index = 0; index < Length; index++) {
            this[index] += vector[index];
        }
        return this;
    }
    
    public Vector Add(Vector vector) {
        for (int index = 0; index < Length; index++) {
            this[index] += vector[index];
        }
        return this;
    }
    
        3
  •  30
  •   TheSoftwareJedi jac    17 年前

    我通常看到并使用“AddCore”或“InnerAdd”

        4
  •  15
  •   Dmitry Gonchar Lasse V. Karlsen    8 年前

    就个人而言,对于方法,无论可见性如何,我都有相同的命名约定。

    • 名称空间、类型、方法、属性:PascalCase
    • 方法的参数:camelCase
    • 私有字段:_PascalCase带下划线前缀,如果为属性提供支持字段,则仅与带下划线前缀的属性同名

    例如,如果我有7种不同的方式通过DatabaseCommand类执行SQL语句,比如QueryDataTable、QueryEnumerable、, QueryEnumerable<T> ,QueryDataReader等。然后所有这些都想调用相同的私有方法,我倾向于调用这个方法InternalQuery或PrivateQuery。

        5
  •  6
  •   EricSchaefer    17 年前

    由于public Add()执行一些检查,而private没有:

    private Vector AddUnchecked(Vector vector) {
        for (int index = 0; index < Length; index++) {
            this[index] += vector[index];
        }
        return this;
    }
    
        6
  •  5
  •   Bevan    17 年前

    我看到的两种常用变体是:

    private Vector DoAdd(Vector vector) { ... }
    

    private Vector AddImpl(Vector vector) { ... }
    

    这两个都不是特别令人满意,但它们是我所看到的。

    我从来没有见过一个惯例,即所有私有方法都应该有前缀——一想到它就让我不寒而栗!

    处理所有用“^”来装订每个成员的C++开发人员已经够糟糕了,我以前是说Delphi开发人员,以前用“F”来对每个成员进行前缀。我还在康复中!

        7
  •  3
  •   cgreeno    17 年前

        8
  •  3
  •   Y.Yanavichus    15 年前

    公共方法:

    public void Add()
    {
    }
    this.Add()
    

    私人方法:

    private void _Add()
    {
    }
    _Add();
    

    public int Id {get;set;}
    this.Id = 10;
    

    领域:

    private bool _isUsed;
    _isUsed = false;
    

    局部变量:

    bool isUsed;
    
        9
  •  2
  •   PEZ    17 年前

    我会按照队友的建议去做

    public Vector Add(Vector vector) {
        // check vector for null, and compare Length to vector.Length
        for (int index = 0; index < Length; index++) {
            this[index] += vector[index];
        }
        return this;
    }
    
    public static Vector Add(Vector vector1, Vector vector2) {
        // check parameters for null, and compare Lengths
        Vector returnVector = vector1.Clone()
        return returnVector.Add(vector2);
    }
    

    也许我不该这么晚才上电视。。。

        10
  •  1
  •   Greg Dean    17 年前

    我认为在大多数惯例中,私人事务有更多的自由。然而,我经常看到这一点:

    private Vector AddCore(Vector vector)
    

    private Vector DoAdd(Vector vector)
    

    public static Vector Add(Vector vector1, Vector vector2) 
    {
        // check if vector1 is null
        Vector returnVector = vector1.Clone()
        return returnVector.Add(vector2);
    }
    
    public Vector Add(Vector vector) 
    {
        // check parameters for null, and compare Lengths
        for (int index = 0; index < Length; index++) {
            this[index] += vector[index];
        }
        return this;
    }
    

    同时将这些花括号放在正确的位置:-)

        11
  •  0
  •   Paul    17 年前

    有些人在私有字段前面加上“m_”,我还没有看到私有方法有任何类似的样式。

        12
  •  0
  •   MusiGenesis    17 年前

    不幸的是,我经常遇到这种约定,并且有一种倾向,即在表单上命名控件时使用前导下划线(例如“_txtFirstname”而不仅仅是“txtFirstname”)。这似乎是一种奇怪的bleedover效应,因为MS不再推荐使用前导下划线命名私有变量。或者程序员只是喜欢使用下划线键,我想不出什么原因。

    不要使用这种惯例,当你的同事坚持这样做时,挑战他去寻找一些东西( 任何东西

        13
  •  0
  •   user3638471 user3638471    10 年前

    EPiServer使用“…内部”约定,如中所示 AddInternal() 在这种情况下。