# 编写让别人能读懂的代码

1. 写让别人能读懂的代码
2. 写可扩展的代码
3. 写可测试的代码(代码应该具备可测试性，对没有可测试性的代码写测试，是浪费生命的表现)

1.拒绝注释，用代码来阐述注释

```/// <summary>
/// !@#\$%^&^&*((!@#\$%^&^&*((!@#\$%^&^&*((!@#\$%^&^&*((
/// </summary>
/// <returns></returns>
public decimal GetCash()
{
//!@#\$%^&^&*((!@#\$%^&^&*((
var a = new List<decimal>() { 2m, 3m, 10m };
var b = 2;
var c = 0m;
//!@#\$%^&^&*((!@#\$%^&^&*((!@#\$%^&^&*((
foreach (var p in a)
{
c += p*b;
}
return c;
}```

```public decimal CalculateTotalCash()
{
var prices=new List<decimal>(){2m,3m,10m};
var itemCount = 2;
return prices.Sum(p => p*itemCount);
}```

2. 为布尔变量赋值

```public bool IsAdult(int age)
{
if (age > 18)
{
}
else
{
}
}```

```public bool IsAdult(int age)
{
var isAdult = age > 18;
}```

3.双重否定的条件判断

```if (!isNotRemeberMe)
{
}```

```if (isRemeberMe)
{
}```

4.拒绝HardCode,拒绝挖坑

```if (carName == "Nissan")
{
}```

```if (car == Car.Nissan)
{
}```

5.拒绝魔数，拒绝挖坑

```if (age > 18)
{
}```

```const int adultAge = 18;
{
}```

6.复杂的条件判断

```if (job.JobState == JobState.New
|| job.JobState == JobState.Submitted
|| job.JobState == JobState.Expired
|| job.JobTitle.IsNullOrWhiteSpace())
{
//....
}```

```if (CanBeDeleted(job))
{
//....
}

private bool CanBeDeleted(Job job)
{
var invalidJobState = job.JobState == JobState.New
|| job.JobState == JobState.Submitted
|| job.JobState == JobState.Expired;
var invalidJob = string.IsNullOrEmpty(job.JobTitle);

return invalidJobState || invalidJob;
}```

7.嵌套判断

```var isValid = false;
{
{
if (!string.IsNullOrEmpty(user.Email))
{
isValid = true;
}
}
}
return isValid;```

```if (string.IsNullOrEmpty(user.UserName)) return false;
if (string.IsNullOrEmpty(user.Email)) return false;
return true;```

8.使用前置条件

```if (!string.IsNullOrEmpty(userName))
{
{
//register
}
else
{
throw new ArgumentException("user password can not be empty");
}
}
else
{
throw new ArgumentException("user name can not be empty");
}```

```if (string.IsNullOrEmpty(userName)) throw new ArgumentException("user name can not be empty");
//register```

9.参数过多，超过3个

```public void RegisterUser(string userName, string password, string email, string phone)
{
}```

```public void RegisterUser(User user)
{
}```

10.方法签名中含有布尔参数

```public void RegisterUser(User user, bool sendEmail)
{
}```

```public void RegisterUser(User user)
{
}
public void SendEmail(User user)
{
}```

11.写具有表达力的代码

```private string CombineTechnicalBookNameOfAuthor(List<Book> books, string author)
{
var filterBooks = new List<Book>();
foreach (var book in books)
{
if (book.Category == BookCategory.Technical && book.Author == author)
{
}
}
var name = "";
foreach (var book in filterBooks)
{
name += book.Name + "|";
}
return name;
}```

```private string CombineTechnicalBookNameOfAuthor(List<Book> books, string author)
{
var combinedName = books.Where(b => b.Category == BookCategory.Technical)
.Where(b => b.Author == author)
.Select(b => b.Name)
.Aggregate((a, b) => a + "|" + b);

return combinedName;
}```

http://www.cnblogs.com/richieyang/p/4840614.html

412 篇文章49 人订阅

0 条评论

## 相关文章

### 利用Actor实现管道过滤器模式

《基于Actor的响应式编程》计划分为三部分，第一部分剖析响应式编程的本质思想，为大家介绍何谓响应式编程（Reactive Programming）。第二部分则...

31340

31940

14440

4.7K30

20130

44860

6520

11840

38480

55070