写出优雅简明代码理论指导收集
2011-02-16 11:25
387 查看
1. 不要使用””, 使用string.Empty
string name = string.Empty;
2. 善于合并if
public bool Equals(CommentData obj) { if (!CommentId.Equals(obj.CommentId)) return false; if (!Comment.Equals(obj.Comment)) return false; if (!CommentorId.Equals(obj.CommentorId)) return false; return true; }--------->>>public bool Equals(CommentData obj) {return CommentId == obj.CommentId &&Comment.Equals(obj.Comment) &&CommentorId == obj.CommentorId;}
3. 不断重构你的代码
4. 用 Linq 简化代码foreach (CommentData data in Comments) { if (data.CommentId.HasValue) throw new ArgumentNullException("Create is only for saving new data. Call save for existing data.", "data"); }
--------------->>>
if (Comments.Any(data => data.CommentId.HasValue)) { throw new ArgumentNullException("Create is only for saving new data. Call save for existing data.", "data"); }5. 运用 ?:和??[/code]
string name = value; if (value == null) { name = string.Empty; }
------------>>>
string name = (value != null) ? value : string.Empty;
或: string name = value ?? string.Empty;
----------------------------------->>>6.运用AS
if (employee is SalariedEmployee) { var salEmp = (SalariedEmployee)employee; pay = salEmp.WeeklySalary; // ... }
var salEmployee = employee as SalariedEmployee; if (salEmployee != null) { pay = salEmployee.WeeklySalary; // ... }7. 运用 using, 去除dispose()[/code]
------------------------------->>>
再次简化:[/code][/code][/code]
相关文章推荐
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[2]
- 一起谈.NET技术,写出优雅简明代码的论题集 -- Csharp(C#)篇[2]
- 一起谈.NET技术,写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[2“.NET研究”]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[2]
- 写出优雅简明代码的论题集 -- Cshar“.NET研究”p(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[转]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[2]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇
- Julia: 一行代码可以写出什么优雅?
- 优雅的写出python代码
- 代码质量管理——如何写出优雅地代码
- 一些js代码优化方法,写出一手优雅的代码
- 用 RxSwift + Moya 写出优雅的网络请求代码