Re: Is code duplication allowed in this instance?
- From: Francesco Bochicchio <bieffe62@xxxxxxxxx>
- Date: Fri, 3 Jul 2009 05:34:27 -0700 (PDT)
On Jul 3, 12:46 pm, Klone <hkm...@xxxxxxxxx> wrote:
Hi all. I believe in programming there is a common consensus to avoid
code duplication, I suppose such terms like 'DRY' are meant to back
this idea. Anyways, I'm working on a little project and I'm using TDD
(still trying to get a hang of the process) and am trying to test the
functionality within a method. Whoever it so happens to verify the
output from the method I have to employ the same algorithm within the
method to do the verification since there is no way I can determine
the output before hand.
So in this scenario is it OK to duplicate the algorithm to be tested
within the test codes or refactor the method such that it can be used
within test codes to verify itself(??).
If the purpose of the test is to verify the algorithm, you obviously
should not use the algorithm
to verify itself ... you should use a set of pairs (input data,
exoected output data) data that you know is
well representative of the data your algorithm will process. Possibly
to prepare the test data set
you might need a different - and already proven - implementation of
the algorithm.
Another thing I sometime do when testing mathematics function is use
counter-proof: for instance, if my function
computes the roots of a quadratic equation, the test verifies that the
roots applied to the equation
actually give (almost) zero as result. This kind of test might not be
as rigorous as preparing the data set with the known
answers, but it is easier to setup and could give you a first idea if
your code is "correct enough" to stand
more formal proof.
Ciao
----
FB
.
- Follow-Ups:
- Re: Is code duplication allowed in this instance?
- From: Bearophile
- Re: Is code duplication allowed in this instance?
- References:
- Is code duplication allowed in this instance?
- From: Klone
- Is code duplication allowed in this instance?
- Prev by Date: Re: Sequence splitting
- Next by Date: Re: Python debugger
- Previous by thread: Re: Is code duplication allowed in this instance?
- Next by thread: Re: Is code duplication allowed in this instance?
- Index(es):
Relevant Pages
|