【代码审查】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")这种方式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值