当前位置: 移动技术网 > IT编程>开发语言>c# > C#的编码规范详细说明

C#的编码规范详细说明

2019年07月18日  | 移动技术网IT编程  | 我要评论
首先我们要明白一下几点,
1.代码写出来除了让他跑起来还有个非常非常重要的作用是维护,因为没有一成不变的代码,需求变化代码就不可避免的要变化。
2.你不是一个人在写代码,你身后有一个团队,这个团队中任何一个人都有可能会改你的代码,你如果写的不规范,后面的人会跟着写。举一个例子,最初的代码可能是这样的,
复制代码 代码如下:

if (orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "cz-sp")
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e = regetflight.getdirectflighthighweightentity(orderinfo.o_flightentity, "", e.flightagency);
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    regetflightinitdata.highcostandweightentity = e;
                    regetflightinitdata.tickettype = "0001";
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }
                else
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e.costrate1 = 1.0m;
                    regetflightinitdata.highcostandweightentity = e;
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }

但是过一时间又来个需求,对于“mu-ws”也需要走上面的逻辑,你会怎么写,如果你只是完成任务可能会像下面这样写
复制代码 代码如下:

if (orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "cz-sp" || orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-ws")
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e = regetflight.getdirectflighthighweightentity(orderinfo.o_flightentity, "", e.flightagency);
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    regetflightinitdata.highcostandweightentity = e;
                    regetflightinitdata.tickettype = "0001";
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }
                else
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e.costrate1 = 1.0m;
                    regetflightinitdata.highcostandweightentity = e;
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }

如果真的这么写你就是始作俑者,后面的人如果都和你一样就都会朝着葫芦画瓢,最后代码可能会是这样的
复制代码 代码如下:

if (orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "cz-sp" || orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-ws" || orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-ws" || orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-sp" || orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "cz-ws" || orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "xt-ws")
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e = regetflight.getdirectflighthighweightentity(orderinfo.o_flightentity, "", e.flightagency);
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    regetflightinitdata.highcostandweightentity = e;
                    regetflightinitdata.tickettype = "0001";
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }
                else
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e.costrate1 = 1.0m;
                    regetflightinitdata.highcostandweightentity = e;
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }

看到效果了么,你需要脱一下下面的那个长条才能看到全部代码,这就是你的“因”种下后得到的果实。所以我们需要改进,常见的改进是回车换行,保证所有的代码都在你一眼能够看到的范围之内。改进后代码如下:
复制代码 代码如下:

if (orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "cz-sp" ||
                    orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-ws" ||
                    orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-ws" ||
                    orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "mu-sp" ||
                    orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "cz-ws" ||
                    orderinfo.o_ordersentity.directflightchannel.trim().toupper() == "xt-ws")
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e = regetflight.getdirectflighthighweightentity(orderinfo.o_flightentity, "", e.flightagency);
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    regetflightinitdata.highcostandweightentity = e;
                    regetflightinitdata.tickettype = "0001";
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;
                }
                else
                {
                    ordercostandweightentity e = new ordercostandweightentity();
                    e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                    e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                    e.costrate1 = 1.0m;
                    regetflightinitdata.highcostandweightentity = e;
                    //如果是海航,则不需要政策倒查验证
                    regetflightinitdata.researchnoflight = false;

是不是清爽一点,好看一点,如果你在第一次修改代码的时候这样写后面的人就会跟着这样写,他们可能会想,前面一个人这样写这样写应该不会有问题,虽然代码看起来有点别扭,这时候我们可以带着鉴赏的眼光看这段代码,如果再想一想还有更好的方法,为何不用switch呢?
复制代码 代码如下:

switch (orderinfo.o_ordersentity.directflightchannel.trim().toupper())
                {
                    case "cz-sp":
                    case "mu-sp":
                    case "xt-ws":
                        {
                            ordercostandweightentity e = new ordercostandweightentity();
                            e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                            e = regetflight.getdirectflighthighweightentity(orderinfo.o_flightentity, "", e.flightagency);
                            e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                            regetflightinitdata.highcostandweightentity = e;
                            regetflightinitdata.tickettype = "0001";
                            //如果是海航,则不需要政策倒查验证
                            regetflightinitdata.researchnoflight = false;
                            break;
                        }
                    default:
                        {
                            ordercostandweightentity e = new ordercostandweightentity();
                            e.sendticketcity = int.parse(configurationmanager.appsettings["sendticketcityofdirectflights_hu"].tostring());
                            e.flightagency = insurancecommon.getstrategyflightagencynew(orderinfo.o_ordersentity.directflightchannel);
                            e.costrate1 = 1.0m;
                            regetflightinitdata.highcostandweightentity = e;
                            //如果是海航,则不需要政策倒查验证
                            regetflightinitdata.researchnoflight = false;
                            break;
                        }
                }

是不是更加简单,如果你这样写,后面的人会毫不犹豫地在后面加一个case “”:因为这个是大家再熟悉不过的语法了,丝毫不用怀疑这种写法的正确性,于是我们可以带着欣赏的眼光来看这段代码了。
3.要承认我们的心智就这么多,脑袋就这么大,我们既不能一目十行也不能出口成章,所以不要写一言看不出什么意思的代码。
以上的想法都是建立在维护别人代码的痛苦,失落,沮丧,各种不爽,各种吐槽之上,所以代码规范是需要我们时刻关注的,在这里自己总结一些规范,告诫自己不要做始作俑者。
1.一个文件中只放一个类,类名同文件名,不要在一个文件中写好几个类,这样看的清楚。
2.不要在一个文件中写多于1000行的代码,除了那些比较大的实体类。其实我还想说超过500行看起来就有点累,但是在我们的系统中超过10000行的代码比比皆是。
3.一个方法的代码不要超过100行,其实我想说超过50行的方法看起来就有点累。但是在我们的系统中超过200行代码的方法比比皆是。
4.存储过程的代码也不要超过100行,不要在存储过程中写过多的业务逻辑,那是找死,但是在我们的系统中我还真他妈的见过10000多行的存储过程,好宏伟啊!
5.避免写超过5个参数的方法,如果有请使用一个类或者结构来传。
6.一个方法只有一个return result; ,不要多次return结果,最好给返回结果赋值,最后return result;
7.不要给很简单的代码加注释,会有噪音的,会让人误解的,因为你写的大多数情况下很片面。
8.记录日志的时候不要到处都记,有条件的情况下针对客户一次操作(比如下单)只记录一条日志。
另外再这里列举自己对代码做的一些改进,不足之处欢迎大家指出。
1.参数过长使用缩进
复制代码 代码如下:

a.
                    //获得保险策略信息
                    insurancestrategy = insurancecommon.getstrategyinsurance(appfltentity.productsource.tostring(), strategyflightagency.tostring(), "", appfltentity.price.tostring(), appfltentity.directflightchannel, appfltentity.airline.dibitcode, appfltentity.flight, appfltentity.departairport.code, appfltentity.arriveairport.code, appfltentity.subclass, appfltentity.departtime, appfltentity.departtime.date, null);
b.
                    //获得保险策略信息
                    insurancestrategy = insurancecommon.getstrategyinsurance(appfltentity.productsource.tostring(),
                                                                             strategyflightagency.tostring(),
                                                                             "",
                                                                             appfltentity.price.tostring(),
                                                                             appfltentity.directflightchannel,
                                                                             appfltentity.airline.dibitcode,
                                                                             appfltentity.flight,
                                                                             appfltentity.departairport.code,
                                                                             appfltentity.arriveairport.code,
                                                                             appfltentity.subclass,
                                                                             appfltentity.departtime,
                                                                             appfltentity.departtime.date,
                                                                             null);

2.if条件过长中使用缩进
复制代码 代码如下:

a.
                if (isdirectflight && (this.corppaytype == expensetype.own || this.accountinfo.directordersquotemode == "i"))
                {
                    strategyflightagency = insurancecommon.getstrategyflightagencynew(appfltentity.directflightchannel);
                    //根据票台获得票台对应的城市
                    strategycityid = insurancecommon.getflightagencycity(strategyflightagency);
                 }
b.
                if (isdirectflight &&
                    (this.corppaytype == expensetype.own ||
                    this.accountinfo.directordersquotemode == "i"))
                {
                    strategyflightagency = insurancecommon.getstrategyflightagencynew(appfltentity.directflightchannel);
                    //根据票台获得票台对应的城市
                    strategycityid = insurancecommon.getflightagencycity(strategyflightagency);
                 }

3.使用三元表达式
复制代码 代码如下:

a.
                        if (insuranceinfolist != null && insuranceinfolist.count > 0 && insuranceinfolist[0].generaldescription.contains("e路泰康"))
                            collections.set("iseldercanbuyinsurance", "t");
                        else
                            collections.set("iseldercanbuyinsurance", "f");
b.
collections.set("iseldercanbuyinsurance", insuranceinfolist != null && insuranceinfolist.count > 0 && insuranceinfolist[0].generaldescription.contains("e路泰康") ? "t" : "f");

4.使用蓝不大表达式
复制代码 代码如下:

a.
                        insuranceinfolist = insuranceinfolist.findall(delegate(bookinginsuranceinfo iinfo)
                        {
                            return iinfo.typeid == "c2c30";
                        });
b.
insuranceinfolist = insuranceinfolist.findall(a => a.typeid == "c2c30");

5.使用switch表达式,见上面。
6.使用数组包含
复制代码 代码如下:

a.
            if (wsflt.productsource == 4 && !string.isnullorempty(wsflt.directflightchannel) && (wsflt.directflightchannel.toupper() == "ho-ws" or wsflt.directflightchannel.toupper() == "zh-ws" or wsflt.directflightchannel.toupper() == "xt-ws")
                && directflightcorporationlist != null && directflightcorporationlist.count > 0 && !directflightcorporationlist.contains(corporationid))
            {
                return false;
            }
b.
            if (wsflt.productsource == 4 &&
                new list<string>() { "ho-ws", "cz-ws", "xt-ws" }.contains(wsflt.directflightchannel.trim().toupper()) &&
                directflightcorporationlist != null &&
                directflightcorporationlist.count > 0 &&
                !directflightcorporationlist.contains(corporationid))
            {
                return false;
            }

可以看到代码中也使用了缩进。
欢迎大家也列举自己遇到的代码简洁之道。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网