What does “DAMP not DRY” mean when talking about u

2019-01-01 01:18发布

I heard someone say that unit tests (e.g. nUnit, jUnit, xUnit) should be

DAMP not DRY

(E.g. unit tests should contain "damp code" not "dry code")

What are they talking about?

8条回答
余生无你
2楼-- · 2019-01-01 01:49

DAMP stands for “descriptive and meaningful phrases” and is the opposite of DRY, not in the sense that it says “everything should look like a trash heap and be impossible to read”, in that readability is more important than avoiding redundant code.

http://codeshelter.wordpress.com/2011/04/07/dry-and-damp-principles-when-developing-and-unit-testing/

查看更多
初与友歌
3楼-- · 2019-01-01 01:50

I agree with Chris Edwards in that you need to strike a balance between the two. Another thing to note is that if, in an attempt to remove duplication, you end up adding a lot of additional structure in your unit test code (i.e. when taking DRY to extremes), you run the risk of introducing bugs in there. In such a situation, you would either have to unit test your unit tests or leave bits of structure untested.

查看更多
不再属于我。
4楼-- · 2019-01-01 01:59

It's a balance, not a contradiction

DAMP and DRY are not contradictory, rather they balance two different aspects of a code's maintainability. Maintainable code (code that is easy to change) is the ultimate goal here.

DAMP (Descriptive And Meaningful Phrases) promotes the readability of the code.

To maintain code, you first need to understand the code. To understand it, you have to read it. Consider for a moment how much time you spend reading code. It's a lot. DAMP increases maintainability by reducing the time necessary to read and understand the code.

DRY (Don't repeat yourself) promotes the orthogonality of the code.

Removing duplication ensures that every concept in the system has a single authoritative representation in the code. A change to a single business concept results in a single change to the code. DRY increases maintainability by isolating change (risk) to only those parts of the system that must change.

So, why is duplication more acceptable in tests?

Tests often contain inherent duplication because they are testing the same thing over and over again, only with slightly different input values or setup code. However, unlike production code, this duplication is usually isolated only to the scenarios within a single test fixture/file. Because of this, the duplication is minimal and obvious, which means it poses less risk to the project than other types of duplication.

Furthermore, removing this kind of duplication reduces the readability of the tests. The details that were previously duplicated in each test are now hidden away in some new method or class. To get the full picture of the test, you now have to mentally put all these pieces back together.

Therefore, since test code duplication often carries less risk, and promotes readability, its easy to see how it is considered acceptable.

As a principle, favor DRY in production code, favor DAMP in test code. While both are equally important, with a little wisdom you can tip the balance in your favor.

查看更多
旧时光的记忆
5楼-- · 2019-01-01 02:02

DAMP - Descriptive And Meaningful Phrases.

"DAMP not DRY" values readability over code re-use. The idea of DAMP not DRY in test cases is that tests should be easy to understand, even if that means test cases sometimes have repeated code.

See also Is duplicated code more tolerable in unit tests? for some discussion on the merits of this viewpoint.

It may have been coined by Jay Fields, in relation to Domain Specific Languages.

查看更多
笑指拈花
6楼-- · 2019-01-01 02:12

I don't wish to duplicate the effort here, but you can have tests that are DAMP but have the benefit of DRY. On the flip side, DRY tests won't satisfy DAMP tests in some cases.

I've blogged about DRY vs DAMP which includes some examples.

Neither approach should be your only solution, sometimes DAMP is overkill, other times a very nice addition.

As a general rule you should apply the rule of three. If you spot duplication a third time, it may be worth looking into writing DAMP style tests, but even then not all duplication is bad. Context matters.

查看更多
人间绝色
7楼-- · 2019-01-01 02:15

Damp = 'Descriptive And Meaningful Phrases' - your unit tests should be able to be 'read':

Readability is more important than avoiding redundant code.

From the article:

DAMP stands for “descriptive and meaningful phrases” and is the opposite of DRY, not in the sense that it says “everything should look like a trash heap and be impossible to read”, in that readability is more important than avoiding redundant code.

What does this mean and where to use it?

DAMP mostly applies when writing test code. Test code should be very easy to understand to the point that some redundancy is acceptable.

查看更多
登录 后发表回答