.NET程序员要遵守的二十条编码习惯
2010-01-20 11:08
281 查看
本文将为大家介绍二十条.NET程序员不得不遵守的.NET编码习惯,希望通过这些.NET编码习惯能让大家的.NET开发工作有事半功倍的效果。
1、不要硬编string/ numeric,可以使用一些常量代替。 (提高可读性)
2、对于字符串比较-使用String. Empty ,而不是""。
3、不要声明成员变量为 public 或者proteted,尽量使用private 成员变量和public/protected 属性。 (修改)
4、当我们要在循环操作字符串,使用StringBuilder,而不是字符串,示例如下。
不好的习惯:
好点的习惯:
好点的习惯:
10、使用as做类型转换的时候,对转换后的值进行null值判断
11、创建wcf代理,可以使用using表达式。 (很多地方可以这样使用)
12、对于昂贵的资源(例如Connection, File 等等),遵照'Acquire late, release early’
(尽量晚的获取,尽量早的释放)准则。
例子:如果你想在数据操作时,使用的SqlConnection对象,请在方法级别,而不是在类级别创建实例。
代码
如果你想创建的类级别SqlConnection实例,确保您的类实现了IDisposable接口,并在Dispose()中清理SqlConnection实例。
代码
13、如果你不想别人扩展你的类功能,使用‘sealed’。
14、避免为每个类都声明‘destructor’ ,因为它会增加不需要常驻内存的类的生命周期。
15、相对manual threading,更倾向用Thread Pool 。
16、在循环内不要去调用其它方法。 (call function 有性能损耗)
例如:
不好的习惯:
17、不要在循环内处理异常,而是将循环处理的逻辑放在try/catch里面
不好的习惯:
好点的习惯:
18、不用通过异常处理应用程序的逻辑
例如:
不好的习惯:
好点的习惯:
19、相对for/while ,倾向使用foreach循环。[更正]
20、使用多层架构的系统,层与层之间的交互,比起DataSet/DataTables更倾向于使用对象传递数据。
原文链接:http://developer.51cto.com/art/201001/179370.htm
[/code]
1、不要硬编string/ numeric,可以使用一些常量代替。 (提高可读性)
int Count; Count = 100; private static const int ZERO = 0; if ( Count == ZERO ) { // 执行一些操作 }
2、对于字符串比较-使用String. Empty ,而不是""。
3、不要声明成员变量为 public 或者proteted,尽量使用private 成员变量和public/protected 属性。 (修改)
4、当我们要在循环操作字符串,使用StringBuilder,而不是字符串,示例如下。
不好的习惯:
String temp = String.Empty; for ( int i = 0 ; i<= 100; i++) { temp += i.ToString(); }
好点的习惯:
StringBuilder sb = new StringBuilder(); for ( int i = 0 ; i<= 100; i++) { sb.Append(i.ToString()); } 5、简单的操作,比起Collection更倾向使用Array。 (视情况,这里是建议) 6、比起ArrayList更倾向使用Generic Collection。 (视情况,这里是建议) 7、比起HashTable更倾向使用Generic Dictionary。 (视情况,这里是建议) 8、对于字符串的操作和存储,倾向于StringCollection和StringDictionary。 (视情况,这里是建议) 9、使用适合的数据类型。 例如:你想要判断状态,使用bool比int要好。 不好的习惯: [code]int Check = 0; if ( Check == 0 ) { // 执行一些操作 }
好点的习惯:
bool Check = false ; if (!Check) { // 执行一些操作 }
10、使用as做类型转换的时候,对转换后的值进行null值判断
class A { } class B : A { } B objB = new B(); A objA1 = (A) objB; A objA2 = objB as A; if ( objA2 != null ) { //执行所需的操作 }
11、创建wcf代理,可以使用using表达式。 (很多地方可以这样使用)
using (Cerate the proxy) { //执行所需的操作 }
12、对于昂贵的资源(例如Connection, File 等等),遵照'Acquire late, release early’
(尽量晚的获取,尽量早的释放)准则。
例子:如果你想在数据操作时,使用的SqlConnection对象,请在方法级别,而不是在类级别创建实例。
代码
class MyData { public MyData() { } public List<Customer> GetAllCustomer() { using (SqlConnection objConnection = new SqlConnection( "Connection string" )) { //执行一些操作得到需要的数据 } } }
如果你想创建的类级别SqlConnection实例,确保您的类实现了IDisposable接口,并在Dispose()中清理SqlConnection实例。
代码
class MyData : IDisposable { SqlConnection objConnection ; public MyData() { objConnection = new SqlConnection( "Connection string" ); } public List<Customer> GetAllCustomer() { //通过objConnection得到需要的数据 } public void Dispose() { //清理SqlConnection实例 if ( objConnection != null ) { if ( objConnection.State == ConnectionState.Open) { objConnection.Close(); } } } }
13、如果你不想别人扩展你的类功能,使用‘sealed’。
14、避免为每个类都声明‘destructor’ ,因为它会增加不需要常驻内存的类的生命周期。
15、相对manual threading,更倾向用Thread Pool 。
16、在循环内不要去调用其它方法。 (call function 有性能损耗)
例如:
不好的习惯:
for ( int i = 0; i<= 100; i++) { Calculate(i); } 好点的习惯: for ( int i = 0; i<= 100; i++) { //直接写Calculate逻辑。 }
17、不要在循环内处理异常,而是将循环处理的逻辑放在try/catch里面
不好的习惯:
for ( int i = 0 ; i<= 100; i++) { try { } catch (Exception ex) { throw ex; } }
好点的习惯:
try { for ( int i = 0 ; i<= 100; i++) { } } catch (Exception ex) { throw ex; }
18、不用通过异常处理应用程序的逻辑
例如:
不好的习惯:
try { int x,y,z; x = 0; y = 10; z = y/x; } catch (DevideByZeroException ex) { Throw ex; }
好点的习惯:
try { int x,y,z; x = 0; y = 10; if ( x != 0 ) { z = y/x; } } catch (Exception ex) { }
19、相对for/while ,倾向使用foreach循环。[更正]
20、使用多层架构的系统,层与层之间的交互,比起DataSet/DataTables更倾向于使用对象传递数据。
原文链接:http://developer.51cto.com/art/201001/179370.htm
[/code]
相关文章推荐
- 团队遵守Command和Query分离的编码习惯能让后来者快速融入开发
- 团队遵守Command和Query分离的编码习惯能让后来者快速融入开发
- JAVA良好的编码习惯
- 不好的编码习惯(待续)和好的编码习惯
- C#编码标准--编码习惯
- 编码的良好习惯
- 我的编码习惯 - 函数编写建议(附完整源码)
- 我的编码习惯
- iOS7和iOS6适配,和保留之前的坐标编码习惯,不用刻意加减
- Kotlin习惯用语和编码风格[转自Kotlin中文文档]
- mysql和myelipse乱码解决办法-----推荐统一编码格式保持良好习惯
- 编码习惯-函数编写建议
- 我的编码不良习惯
- C#编码标准--编码习惯
- 编码习惯之接口定义
- 编码习惯之接口定义
- c#好的程序员必须掌握的编码习惯
- C#编码习惯和命名规则
- 编码习惯 – 异常处理