我犯了的错误 by 第一次 CodeReview(Updated
最近我们项目开始了CODE REVIEW. 结果让我写代码的时候,战战兢兢。也许是借口吧,希望大家不要跟我犯一样的错误,特别有些是很幼稚的错误,所以打算写一系列的文章《我犯了的错误》来总结每次CODE REVIEW我烦的错误。也写写“为什么”和解决的办法。如果有不同意的西方,希望可以一起讨论。开始啦!哦,忘了说了,我是写C#的.
1. 比较时,把常量写在变量的前面。
Don`t :
if( a > 3)
Do:
if( 3 < a)
Reason:
因为这样容易犯一种错误,就是当把a==3 打成了 a=3时,在C++,Java编译器一样会通过。因为a=3在IF里面,就会变成1.而C#只支持 true和False, 所以当if(a=3)时,编译器就会报错。但是为了养成良好的习惯,避免错误,所以在常量放在变量前面也是一个比较好的实验。
2. 先判断错误的情况,在运行正确的情况
Don`t:
if(正确情况)
{
throw .....
}
......(正确情况)
else(错误情况)
{
}
Do:
if(错误情况)
{
throw .....
}
......(错误情况)
else(正确情况)
{
}
Reason:
可以有清晰简单的编辑表达。
3. 函数长度
Don`t:超过一屏
Do:一屏之内
Reason:比较易于维护
4. if else使用时,哪怕是一句,也尽量用括号括住或者递进到同一行
Don`t:
if(..)
.......
else
.......
Do:
if(..) .......
else ......
or
if(..)
{....
}
else
{.....
}
Reason:
避免犯错误。项目经验说明,很多要会不小心有这样的代码:
if(3==a)
step = abc;
abc++
5. string比较时要用.Equals(...)
5. string比较时应该用==也可以,只在C#里面,因为C#重写了==的函数
Don`t:
string a = "abc";
string b = "abc";
if(a==b)
Do:
.....
if(a.Equals(b))
{....
}
Reason:
经过反编译,.NET中“==”是这样实现的:
public static bool operator ==(string a, string b)

而strings.Equals(string, string)的实现如下:
public static bool Equals(string a, string b)
{
if (a == b)
{
return true;
}
if ((a != null) && (b != null))
{
return a.Equals(b);
}
return false;
}
[MethodImpl(MethodImplOptions.InternalCall)]public extern bool Equals(string value); 由此可见, ==先比较了两个对象的引用,如果引用相同直接返回true(这个不是我们期待的一个逻辑,所以用Equals直接比较内容。
(Updated)
From MSDN
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.
对于引用类型,除了String, ==,如果两边是的引用到异样的对象,就返回的是TRUE。对于字符串类型而言,==是比较字符串的值。
(Updated)
static void Main(string[] args)
{
//General speaking
//1. c= "foo" and c = new(new char[]{'f','o','o'}) is different.Sample1:c,d have the same reference.Sample2: a,b have the different reference.
//2. "" and string.Empty, have the different reference.although they are the same as "" as sample 4
//3. If we confirm that, it`s string compare with string, you can use == , as sample 3
// If not, use Equals() as sample5
//Sample 1
object c = "foo";
string d = "foo";
bool i = ReferenceEquals(c, d);//The same reference, return true
// IL_0015: ldloc.0
// IL_0016: ldloc.1
// IL_0017: ceq(Compare)
bool result1 = (c == d);//reture true


//Sample 2
char[] test = new char[] { 'f', 'o', 'o' };
object a = new string(test);
string b = new string(test);
bool j = ReferenceEquals(a, b);//different reference, return false
// IL_004a: ldloc.s a
// IL_004c: ldloc.s b
// IL_004e: ceq
bool result2 = (a == b);//Ojbect compare string or Object compare Object, so it compare the reference of the object, return false


//Sample 3
//IL_0052: ldloc.s a
//IL_0054: castclass [mscorlib]System.String
//IL_0059: ldloc.s b
//IL_005b: call bool [mscorlib]System.String::op_Equality(string,string)
bool result3 = ((string)a == b);//Compare with two string,as the msdn said, it will compare the value of the string, return true


//Sample 4
//IL_0062: ldstr ""
//IL_0067: ldsfld string [mscorlib]System.String::Empty
//IL_006c: ceq
bool result4 = (((object)"") == String.Empty);//The compare the reference, return false. It means they reference it`s different. so return false.The same as sample 2.


//Sample 5
// IL_007a: callvirt instance bool [mscorlib]System.Object::Equals(object)
bool result5 = ((object)"").Equals(String.Empty);//compare value, return true
}
look through this, you can found out , the references of a, b is not the same, but the value is the same. But a==b, return false.
String 最佳实践
http://blog.joycode.com/microhelper/articles/19665.aspx
6. 全部的public 的函数和变量,应该包含了检查有效性的代码,当然最常规的就是检查是不是NULL了
7. 尽量用并行的代码,少用嵌套的代码
Don`t:
/// <summary>
/// Verify the install filename whether it`s exist and it`s a standard setup file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool VerifyInstallFile(string fileName)
{
if (fileName.Equals(null))
{
if (File.Exists(fileName))
{
FileInfo fileInfo = new FileInfo(fileName);
if
{
(fileInfo.Extension.ToLower().Equals(".sis")
if(fileInfo.Extension.ToLower().Equals(".sisx")
{
if(fileInfo.Extension.ToLower().Equals(".jar"))
{
return true;
}
}
}
}
}
else
{
return false;
}
}Do: (按照下面的意见更新了: ))
/// <summary>
/// Verify the install filename whether it`s exist and it`s a standard setup file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool VerifyInstallFile(string fileName)
{
if (fileName == null)
{
return false;
}
if (File.Exists(fileName))
{
return false;
}
FileInfo fileInfo = new FileInfo(fileName);
if (fileInfo.Extension.ToLower().Equals(".sis")
|| fileInfo.Extension.ToLower().Equals(".sisx")
|| fileInfo.Extension.ToLower().Equals(".jar"))
{
return true;
}
}

浙公网安备 33010602011771号