WET Dilutes Performance Bottlenecks
来源:互联网 发布:简述什么是移动云计算 编辑:程序博客网 时间:2024/05/29 03:04
The 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 implementations. 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 bottleneck. Let's say featureX consumes 30% of the CPU. Now let's say that feature X has ten 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 feature is our bottleneck. However, let's say that we somehow recognized featureX as a bottleneck. We are now left with the problem of finding and fixing every single implementation. With WET we have ten different implementations that we need to find and fix. With DRY we'd clearly see the 30% CPU utilization and we'd 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, it forces users of our code to violate DRY by having each of them re-implement 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 calledCustomerList
. 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 asArrayList
, 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 indexing scheme withSortedList
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 were the code to be WET.
By Kirk Pepperdine
This work is licensed under a Creative Commons Attribution 3
- WET Dilutes Performance Bottlenecks
- WET Dilutes Performance Bottlenecks
- Common bottlenecks of software
- Performance
- Performance
- Performance
- Performance
- performance
- Hunting I/O Bottlenecks with iostat
- OpenStack Swift eventual consistency analysis & bottlenecks
- Detecting Memory Bottlenecks (检测内存瓶颈)
- Big List of 20 Common Bottlenecks
- Word效率工具(WET)
- 洗头 Wet Hair
- 1569: Wet Tiles
- csu1569: Wet Tiles
- csu1569: Wet Tiles
- Wet Shark and Bishops
- Ubuntu11.04安装JDK7
- eclipse中默认js编辑器非常慢,尤其在拷贝粘贴代码时的解决方法
- Use the Right Algorithm and Data Structure
- 谷歌不在关注WebKit了,正在研发新浏览器内核Blink
- Verbose Logging Will Disturb Your Sleep
- WET Dilutes Performance Bottlenecks
- A Model of Learning Objectives[一种学习目标模型]
- 微软推出新款日历服务 功能和界面均有完善
- When Programmers and Testers Collaborate
- Write Code as If You Had to Support It for the Rest of Your Life
- 苹果面临10大威胁:法律官司缠身 创新减少
- Write Small Functions Using Examples
- Spring与Mybatis三种常用整合方法
- Write Tests for People