WET Dilutes Performance Bottlenecks
2015-07-14 09:52
459 查看
WET Dilutes Performance Bottlenecks
Kirk PepperdineTHE IMPORTANCE OF THE DRY PRINCIPLE (Don’t Repeat Yourself) is that it codifies the idea that every piece of knowledge in a system should have a singular representation. In other words, knowledge should be contained in a single implementation. The antithesis of DRY is WET (Write Every Time). Our code is WET when knowledge is codified in several different implemen- tations. The performance implications of DRY versus WET become very clear when you consider their numerous effects on a performance profile.
Let’s start by considering a feature of our system, say X, that is a CPU bottle- neck. Let’s say feature X consumes 30% of the CPU. Now let’s say that feature X has 10 different implementations. On average, each implementation will consume 3% of the CPU. As this level of CPU utilization isn’t worth worrying about if we are looking for a quick win, it is likely that we’d miss that this fea- ture is our bottleneck. However, let’s say that we somehow recognized feature X as a bottleneck. We are now left with the problem of finding and fixing every single implementation. With WET, we have 10 different implementations that we need to find and fix. With DRY, we would clearly see the 30% CPU utiliza- tion and would have a tenth of the code to fix. And did I mention that we don’t have to spend time hunting down each implementation?
There is one use case where we are often guilty of violating DRY: our use of collections. A common technique to implement a query would be to iterate over the collection and then apply the query in turn to each element:
public class UsageExample { private ArrayList<Customer> allCustomers = new ArrayList<Customer>(); // ... public ArrayList<Customer> findCustomersThatSpendAtLeast(Money amount) { ArrayList<Customer> customersOfInterest = new ArrayList<Customer>(); for (Customer customer: allCustomers) { if (customer.spendsAtLeast(amount))

customersOfInterest.add(customer); } return customersOfInterest; } }
By exposing this raw collection to clients, we have violated encapsulation. This not only limits our ability to refactor, but it also forces users of our code to vio- late DRY by having each of them reimplement potentially the same query. This situation can easily be avoided by removing the exposed raw collections from the API. In this example, we can introduce a new, domain-specific collective type called CustomerList. This new class is more semantically in line with our domain. It will act as a natural home for all our queries.
Having this new collection type will also allow us to easily see if these queries are a performance bottleneck. By incorporating the queries into the class, we eliminate the need to expose representation choices, such as ArrayList, to our clients. This gives us the freedom to alter these implementations without fear of violating client contracts:
public class CustomerList { private ArrayList<Customer> customers = new ArrayList<Customer>(); private SortedList<Customer> customersSortedBySpendingLevel = new SortedList<Customer)(); // ... public CustomerList findCustomersThatSpendAtLeast(Money amount) { return new CustomerList( customersSortedBySpendingLevel.elementsLargerThan(amount)); } } public class UsageExample { public static void main(String[] args) { CustomerList customers = new CustomerList(); // ... CustomerList customersOfInterest = // ... } }
customers.findCustomersThatSpendAtLeast(someMinimalAmount);
In this example, adherence to DRY allowed us to introduce an alternate index- ing scheme with SortedList keyed on our customers’ level of spending. More important than the specific details of this particular example, following DRY helped us to find and repair a performance bottleneck that would have been more difficult to find had the code been WET.
相关文章推荐
- 服务器.htaccess 详解以及 .htaccess 参数说明
- cef network-settings
- C# in depth (第一章 C#开发的进化史)
- LeetCode:Single NumberⅡ
- libevent安装及使用
- JS限制用户只能输入数字
- 四年了, 一个java程序员的总结
- Android-无法隐藏标题栏
- Cannot run program \"cd\": error=2, No such file or directory
- MFC的工具条和状态栏
- android on qt 之模拟jquery-mobile控件的box-shadow效果
- UICollectionView 集合视图 的使用
- 目前开发手机app的几种方式
- 判断电话号码是否正确
- Jni 简单实战 获取c++返回值
- HDU 1239 Calling Extraterrestrial Intelligence Again
- Composite模式
- jQuery 常用的代码片段
- 黑马程序员--多线程
- ANDROID AIDL(安桌接口定义语言)