C#代码简洁规范

 var sth=new zfsdfsdfs()

sth.ssss();

直接变成

new zfsdfsdfs().ssss();

for循环和foreach循环和if else是代码混乱的大敌。

之前的代码

Expression<Func<Math_RoleInfo, bool>> exp1 = null;
if (where == null)
{
exp1 = item => true;
}
else
{
exp1 = where;
}
 1、 Expression<Func<Math_RoleInfo, bool>> exp1 = where == null ? item => true : where;

1、巧用select where orderby 等方式。

将复杂的for循环和foreach循环提炼在。

将foreach循环写成方法。

List<Math_Deptinfo> list = bLL_Deptinfo.Search(start, length, out total, where);
            List<UI_Math_Deptinfo> listUIRoleModel = new List<UI_Math_Deptinfo>();
            foreach (Math_Deptinfo item in list)
            {
                UI_Math_Deptinfo uIRoleModel = Mapper.Map<UI_Math_Deptinfo>(item);
                listUIRoleModel.Add(uIRoleModel);
            }

           
 list.Select(item => Mapper.Map<UI_Math_Deptinfo>(item)).ToList();

2、将一些常量代码,进行static优化。

 #region 操作上的称呼
    /// <summary>
    /// 操作上的称呼
    /// </summary>
    public class OpCommonString
    {
        public static string DeleteSuccess = "删除成功";
        public static string DeleteFail = "删除失败";
        public static string InsertSuccess = "录入成功";
        public static string InsertFail = "录入失败";
        public static string UpdateSuccess = "更新成功";
        public static string UpdateFail = "更新失败";
        public static string Executing = "程序正在处理......";
        public static string ExecutedSuccess = "处理成功";
        public static string ExecutedError = "处理失败";
    }
    #endregion

3、能用字典,不用对象。

{
key:"",
value:[1,2,3,4,5]
}

改为:

{{
"xxx-xxxx-xxxx-xxxxx":{
value:[1,2,3,4,5]
}
}
'}

4、能在maper里面配置,不在for循环里自己赋值。

5、接口数据尽量简洁,前端可以用computered进行加工。

6、所有的系统类都要尽可能进行封装,这样可以减少项目的风险。

7、使用泛型接口,强制。

8、在bus层和显示层之间要建立 dto层,从bus到dto,从dto到bus,这样可以建立数据消费的版本。

9、前端和后端要建立token机制。

10、复杂的sql语句要写在视图和存储过程中。

11、减少if的使用,能少就少:

if(a>=0)
{
printf("malaing")
}
else
{
}
a?print('maliang'):()=>{}
 fullUrl = process.env.SINGLE === 'true' ? `/${prefix}/${url}` : `/${process.env.APP_NAME}/${prefix}/${url}`;
 
 return this.hasRead ? 0 : this.count;
原文地址:https://www.cnblogs.com/sexintercourse/p/12194558.html