Moisten up your tests a little 30
DRY (Don’t Repeat Yourself) is a very useful guideline. As with all principles, however, there is a tendency to apply it as a golden rule without really thinking about why it is meaningful, and therefore not understanding when it is important to use and when it is not.
There is another principle that sometimes conflicts with DRY: Clean Code!
For example, look at these two specs and think about which is cleaner and easier to understand:
specify "user should supply name when registering" do
@user = User.new(:name => "David")
@registry.should_receive(:user_name).with("David")
@user.register(@registry)
end
specify "user should supply name when registering" do
@name = "David"
@user = User.new(:name => @name)
@registry.should_receive(:user_name).with(@name)
@user.register(@registry)
end
For me, the first one is simpler. No question. There’s one less line of code (that’s 25% in this case), I can clearly see the name and I don’t have to mentally decode the meaning of @name – nor do I need to look around to see where else it might be used. Remember, temporary variables are a code smell too.
The reason that I believe DRY to be important is that when you have duplicated code you run the risk of changing it in one place and forgetting to change it in another. If you do that, you’re likely to introduce bugs. That is the real risk – not the fact that you might have to type things more than once.
You could make the argument that the second example above manifests this risk. So in that case, you have to weigh clarity and DRYness and make a choice. I’ll usually choose clarity in tests.
There are other examples in which I really don’t think this risk is real. One is multiple calls to a no-arg method. This came up in an email thread on the rspec-users mailing list. The poster was looking for a way to avoid writing a line of code that logs in a user in many different rspec contexts.
The line to avoid was …
session[:logged_in] = true
... and I recommended including a module …
module LogIn
def log_in
session[:logged_in] = true
end
end
context "Given a logged in user..." do
include LogIn
setup do
log_in
end
...
end
The poster responded that he was concerned that there is still duplication because you have to type the two statements in every context.
I see a difference between duplicated statements and duplicated calls to a no-arg method. While typing the call to log_in may require a few extra strokes, the meaning of it will never change in different contexts. So the risks associated w/ code duplication are mitigated. In fact, the real risk is that you might change its one and only meaning, in which case you’d have to look at each case and decide if the new meaning makes sense.
That problem wouldn’t go away if you could subclass contexts. In fact, it would be more sinister because the behaviour is implicit. At least if you look at log_in in each context you have some sense of what it means.
In summary, I believe strongly that DRY is a very important principle. But it’s not the only principle and it definitely isn’t implicitly the most important principle to apply in every situation. Sometimes a little moisture can be just the thing you need to clean up your code.

The example of the name and the code samples is interesting.
The first example has the magic string “David” twice. While the second has it only once. What if we mistyped the name in one of these places?
One of the reasons to use temporary variables is that we may be using the value of the variable more than once in the same section of code. If so, there is a chance that we assign different values. In such cases temporary values are justified. Shying away from temporary variables simply because we have a dislike for them may cause problems.
Another point to consider is maintenance. If the name has to be changed for some reason, say somebody’s name has some of the accented characters used in French, and we want to test whether accented characters are acceptable, don’t we have to change the first segment of code in two places, but only once for the second segment?
In this case, I feel that maintenance will be easier with the second segment.
Of course, in a decent sized method, you may have 15-20 lines intead of 3 and it balloons by five extra lines when you use temporary variables as needed. The ease of maintenance easily overrides the fact that I have five extra lines. It will actually lead to fewer bugs, and reduced possibility of bugs, too.
For all these reasons, I would use the second code segment in such situations. If “David” were mentioned only once in the code, then I would consider the first code segment. But even here, I would ask the question, would it ever be necessary to test for a different name? If the answer was yes, then I would consider making the name as a parameter and passing it in to the method. That is done more easily with the second style when the temporary variable would just become an argument to the method.
In general, I agree with David, however I have to agree with Ravi concerning some code I wrote today. The test failed because I somehow managed to type a zero instead of a capital o in the “expected results” field. I am pretty careful to choose fonts that dot or slash the zero, but I still didn’t see it until I’d wasted time with less happy techniques. Silly things will get in your way. I converted to a local constant (final variable).
So readability is important, and I like the way David writes code, but for a dunce like me I think that it’s important that everything be “singular and well-placed”. :-)
the duration of a cartier replica watch a week-long cold, or an approximate 10 fake tag heuer carrera 10 percent reduction in overall severity,” they breitling bentley replica they noted.Indigenous peoples
Its really impressive and nice post. Its really worth to share and spread.
Incontinence products
Excellent site.
In my opinion the example of the name and the code samples is interesting. It’s really worth to share and spread. Marble Games
In my opinion the example of the name and the code samples is interesting. I want to read mote information in this regard.
Everyone has different growth process, maybe some feel difficult in his or her childhood memory, or others may think that this is a happy ending. Now, there is a adays boxset hot and popular Disney was growing pains is about growing pains as we all know, different people have different ideas. As such, it tells DVD boxset growing pains, the actor has troubled childhood, but he never gave up Dvd And Movie. Finally, he become a person of success, have a happy family, so like us, we must work harder to study, work harder what be what to write, we also must have a dream, never give up, in the end, we all have a bright future. Tell us a lot of useful things, our whole life whether you have time, you had Disney Dvds better buy the growth process full season, you will DVD benefited.
It’s a nice article. I would like to share ralph lauren shirt with you.These are from the latest styles and fashion trends shirts.
beats headphones
dre dre headphones
When picking out your large cocktail ring, go for a size of at least 3 carats or more – more than 5 carats should be your ultimate goal. The main advantage of owning cheap cocktail rings is the fact that if it gets lost or damaged, you will not have lost as much as you would with real stones.
very good informative paragraph…
Okey Oyunu Oyna
Lady Gaga Monster Beats
Beats Studio
Ferrari Headphones
Numerous celebrities are the super fans of buy Christian Louboutin Terracotta Suede Very Prive Platform .Fashion Christian Louboutin Rolando Hidden-Platform Pumps Metallic gives beautiful lovely women a perfect show and looked more sexy with mature.Here you can Save your chance to buy discount but authentic quality of Christian Louboutin Leather Pigalle Snake Pumps Beige! Show your good taste and special personality with a pair of Christian Louboutin Women’s Nylon Fabric Peep-toe Pumps and join our Christian Louboutin club to become a fashion icon.
Reviews and Articles on Latest Louis Vuitton Monogram Vernis Brentwood ,We have Louis Vuitton Monogram Vernis Brentwood on sale that you want to look at! Louis Vuitton Monogram Canvas Speedy 30 Outlet Online.Store supply discount Louis Vuitton Monogram Canvas Speedy 35 , Louis Vuitton Monogram Keepall 55 deisgner luggage for men and women on sale.please feel free to contact us.
They replied me that experts claim because golf shoes by working with smart rivets look wild beautiful so they fancy them. 2010, the actual sales and profits together with Discount Christian Louboutins are actually stupendously pleasant. Christina Aguilera follows the very fashion accessory fad tightly. My husband and i think the its best rivets Christian Louboutin 1:1 Grade Zebras Suede Pumps she wears goes well ready red attractive strapless dress. Unfortunately, kindly please happen to be careful skill decide to wear the actual rough rivets shoes, and it’s sometimes be hurt in addition hurt other individuals just by the particular sharpened rivets. Please understand which in turn your personal safety and as well, typically the safety with regards to people today just about you. Every single types has contrasting temperament. So each single looks extremely diverse kinds of likewise that they dress in a similar style clothes in addition footwear.
Thank you for sharing this with us,Thank you for sharing this with us, very appreciated I will return for more of this blog. i really like it.
Don’t let anyone tell you what you ought to like… Some wines that some experts think are absolutely exquisite don’t appeal to me at all.
Experts always know everything but the fine points. When I took my citizenship exams, no one there knew how the White House came to be called the White House.
This shows, in fact Berlin have to individual burning cars became accustomed to, Lady Gaga Heartbeats think this is mischief.
While typing the call to log_ cheap wedding jewelry in may require a few extra strokes, the meaning of it wedding jewelry will never change in different contexts. So the risks associated w/ code duplication are mitigated. In fact, the real risk is that you might change its one and only meaning, Cheap Fashion Jewelry in which case you’d have to look at each case and decide if the new costume jewelry rings meaning makes sense.
What a great post, thanks a lot for sharing this with us I found it really interesting.
Thank you for sharing this with us,Thank you for sharing this with us, very appreciated I will return for more of this blog. i really like it.
Quality jerseys with Wholesale All kinds of Jerseys, can Save up to 75% & get free shipping! http://www.jerseyscheapusa.com/2011-womens-field-flirt-fashion-new-orleans-saits-43-sproles-black-p-15648.html http://www.jerseyscheapusa.com/mlb-boston-red-sox-hat-029-p-14543.html http://www.jerseyscheapusa.com/mlb-atlanta-braves-30-grey-mn-1969-p-15053.html http://www.jerseyscheapusa.com/2010-stanley-cup-champions-blackhawks-7-seabrook-black3rd-p-5397.html http://www.jerseyscheapusa.com/nfl-new-england-patriots-83-welker-whiteteam-50th-p-5448.html
PDF ? PPT PDF to PowerPoint Converter lets you convert PDF to PowerPoint (PPT, PPTX, PPS, PPTM, PPSX, PPSM, POT, etc.), fully supports MS 2010, 2007, 2003.
Nonetheless there are abundant tips if selecting a jersey like it care to be authentic cotton, adaptable as able-bodied as doesn’t charge replacing for a actual continued time.
It takes affluence of humans to architecture & access up application the account of anniversary bargain & so to acquisition these for an affordable amount ability be anticipation about a difficult air abundance job.
These cossack accept had abundant added technologies put in to them than every added sports activities shoe like it.
Cheap is a part of the atomic accessories that may be beat both through the algid and through the contempo season
As if attemping to get any chump item, it absolutely is an accomplished apparent to admeasurement the gives angle by facet.