Re: Is code duplication allowed in this instance?



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
.



Relevant Pages

  • Re: Why NP Problem is Important and Practical Examples
    ... there is a poly-time algorithm to verify a particular solution. ... My student was working as a software developer and he was meeting ...
    (comp.theory)
  • Re: the "hat" container class [C++]
    ... > I've managed to create a Oalgorithm for this problem by ... > creating a seperate vector of the same size as the data set. ... it is possible to create a peek() function ... at the expense of storage and pre-computing ...
    (comp.programming)
  • Re: decidable
    ... exist a proof of A and we have an effective procedure (or algorithm) to ... we have an algorithm to verify this". ... decidability we need not just an algorithm to verify that a sentence ...
    (sci.logic)
  • Re: getting closest point to a given point
    ... Given any one of the points, I was wondering if there is a matlab function out there that can return the closest point in the data set to the given point in 3d space. ... Your question of the second paragraph has to do with returning the nearest dataset neighbour of a _particular_ point, which is a problem that is linear in the number of points in the dataset: compute the 3000 distances and find the minimum. ... Your proposed algorithm of the third paragraph has to do with comparing every point to every other point, involving N*/2 distances and which doesn't seem to take into account the original 3-space point. ... There is non-negligible overhead for setting up the octree representation so this kind of approach would not be feasible for single point probes, but would become more practical as the number of searches over the same dataset increase There may well be algorithms even better than searching within an octree, as it seems likely the problem has been studied before. ...
    (comp.soft-sys.matlab)
  • The proof of the Halting Problem -- I cant understand
    ... There is no computer algorithm that will accept any algorithm X and ... in a finite number of steps when X is run with data set D. ... "halts" if X terminates in a finite number of steps ... loops forever if CheckHaltprints "halts" ...
    (sci.math)