dongyiqi
7/11/2019 - 12:25 PM

Triniti 后续开发项目的代码规范

Triniti 后续开发项目的代码规范

C# 代码规范

命名规则 -使用Pascal命名法

  • 类名 首字母大写 public class ClassName
  • 接口 I+首字母大写 public interface INode{}
  • 成员变量
    • public 成员变量 首字母大写 public int BulletAmount;
    • private protected 成员变量下划线开头首字母小写 private int _bulletAmount;
    • Property public 首字母大写 public int Count{get;set;}
    • Property protect private 首字母大写 private int _Count{get;set;}
  • 成员函数
    • public protected 首字母大写 public void SampleFunction(){}
    • private 下划线开头 首字母大写 private void _SampleFunction(){} [可选不强制]
  • 临时变量 首字母小写 float tempValue = 0;
  • const 全大写 _字符分割 public const string PATH_NAME = "C:\\a.txt";
  • define 全大写 _字符分割 #define ENABLE_LUA
  • 枚举 首字母大写 enum {

换行规则

  • 分隔符号放在行尾 eg

     _assetLoadOperation.Add(new AssetLoadOperation(
                        assetPathInfo.BundleName, assetPathInfo.AssetNameInBundle
                        , typeof(T), GetAssetBundle, 
                      (asset) => { onAssetLoadedCallback?.Invoke((T)asset); }
                      )
                  );
    

Tips

  • 格式 用Visual Studio默认格式,不确定的话使用快捷键Ctrl+K+F
  • 变量命名用名词 or 形容词+名词 or 名词+名词 eg . string[] dirtyWords; Data creation;
  • 函数名用动词 public void Create();

Sample

    public enum HereType
    {
        Infantry,
        Cavarly,
        Archer
        }
        public class HeroData
        {       
            private float _hp;
            protected int _type;
            public float Hp { get { return _hp; } }
            public float Attack { get; set; }
            public void Create() { }
            private float _Start(){}
        }
        public class Hero
        {
            public HeroData Data { get; set; }
            public void SampleFunc()
            {
                if(Data._hp < 0)
                {
                    FireEvent("die");
                }                
            }            
        }

反面教材