跳转至

软件架构与设计

6个重构方法可帮你提升80%的代码质量

原文地址:Top 6 Refactoring Patterns to Help You Score 80% in Code Quality

在过去做了不少代码审核,发现了一些代码质量上普遍存在的问题,以下是其中的前五名:

1. 臃肿的类

类之所以会臃肿,是因为开发者缺乏对最基本的编码原则,即“单一职责原则”(SRP)的理解。这些类往往会变得很臃肿,是由于不同的且在功能上缺少关联的方法都放在了相同的类里面。

解决方法:提取类/抽离方法 正如上面提到的,像“臃肿的类”(一个类提供了本该有几个类提供的功能)这种代码异味应该将原有类中的方法和属性移动到适当数目的新类中去。旧类中对应新类的方法和属性应该被移除。

另外,有时候一些类过于臃肿是因为它包含了被其他类使用本应该是其他类的成员方法的成员方法。这些方法也应该被迁移到合适的类中。

Conversation With API Builders

原文链接:http://conversation.bigbinary.com/

Conversation With API Builders

Bad APIs have one thing in common—they do not handle response status codes effectively. They all use custom solutions while the status code value can be attached to the HTTP resonse. This ebook covers when to use what status code and much more.

1. Return 200 when it is all good

API user : Hey, I have a problem.

API builder : What’s wrong.

API user : I sent a request to the API to create a user. But I do not see the newly created user.