代码审查:C#编程常见错误提醒
小标 2018-08-13 来源 : 阅读 1838 评论 0

摘要:本文主要向大家介绍了代码审查:C#编程常见错误提醒,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了代码审查:C#编程常见错误提醒,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

Methods should not be empty
不要写空方法,除非这种情况:An abstract class may have empty methods, in order to providedefault implementations for child classes.
Source files should not have any duplicatedblocks
源文件中不要出现任何重复的代码段或行或字符串等。没理解。
"switch case" clauses should not havetoo many lines
"switch case" 每个case里面的代码不要太长,太长的话可以考虑写个方法代替,主要是为了增强代码可读性
Nested blocks of code should not be leftempty
嵌套代码块不要是空的,比如 if( a > 0 ) {doSomething() } else { },这时候应该把后面的else{}去掉。
Methods should not be too complex
方法不要太复杂,否则难以理解和维护。
Unused private fields should be removed
没有使用的private的成员变量应该移除掉。
Dead stores should be removed
没有用到的本地变量或其他死存储应该移除掉,也就是写方法的时候,定义的变量如果后来发现根本用不到,要记得删掉那行代码。
"switch" statements should endwith a "default" clause
switch语句应该以default结束,这是一种defensiveprogramming思想
Unused method parameters should be removed
没有用到的方法参数应该移除掉
Control flow statements "if","for", "while", "switch" and "try"should not be nested too deeply
if /for/while/try这样的嵌套不要太复杂
Useless parentheses around expressionsshould be removed to prevent any misunderstanding
没有意义的括号不要随便加,以免造成误解,比如"="两边对象类型是相同的,就不要强转。
"for" loop stop conditions shouldbe invariant
for循环的结果条件不能是变量,而应该是常量
"static" members should beaccessed statically
static成员是与类、静态方法相联系的。
Catches should be combined
我还没理解
Primitives should not be boxed just for"String" conversion
不要使用 4+" "这样的方式将int值转变为字符串,而是使用 Integer.toString(4)这样的方式。就像Integer.parseInt("我是字符串")这样,不要偷懒。
Classes should not be empty
不要写空类
Unused local variables should be removed
没有用到的本地变量要删掉
"entrySet()" should be iteratedwhen both the key and value are needed
直接看英文更直接:When only the keys from a map are needed in a loop, iterating thekeySet makes sense. But when both the key and the value are needed, it's moreefficient to iterate theentrySet, which will give access to both the key andvalue, instead.
也就是说,如果只需要Map的Key,那么直接iterate这个Map的keySet就可以了,但是如果Key和value都需要,就iterate这个Map。
Method parameters, caught exceptions andforeach variables should not be reassigned
方法参数/捕获的异常/foreach的变量不应该被重新赋值。
Collection.isEmpty() should be used to testfor emptiness
当判断集合是否为空的时候,不要使用if (myCollection.size() == 0) 这样的方式,而是使用if(myCollection.isEmpty()这样的方式,后者性能更高。
Standard outputs should not be useddirectly to log anything
标准输出不直接打印任何东西,也就是打log的时候,不要使用System.out.println("My Message")这样的方式,而是使用logger.log("MyMessage")这种方式。    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程