I participate in Dojo TDD coding, where we try to practice pure TDD in simple problems. However, it seemed to me that the code that comes out of unit tests is not the most efficient. Now that’s fine, but what if the use of the code grows, so efficiency becomes a problem.
I like the way code comes from unit testing, but is it possible to get the performance property through additional tests?
Here is a trivial example in ruby: simple factorization. I followed the clean TDD approach so that the tests pass one by one, checking my initial acceptance test (commented below). What further steps could I take if I wanted to create one of the common simple factorization algorithms ? To reduce the problem area, say, I want to get a quadratic sieve . Now, in this exact case, I know the "optimal algorithm, but in most cases the client will simply add the requirement that the function work in less than" x "for the given environment.
require 'shoulda'
require 'lib/prime'
class MathTest < Test::Unit::TestCase
context "The math module" do
should "have a method to get primes" do
assert Math.respond_to? 'primes'
end
end
context "The primes method of Math" do
should "return [] for 0" do
assert_equal [], Math.primes(0)
end
should "return [1] for 1 " do
assert_equal [1], Math.primes(1)
end
should "return [1,2] for 2" do
assert_equal [1,2], Math.primes(2)
end
should "return [1,3] for 3" do
assert_equal [1,3], Math.primes(3)
end
should "return [1,2] for 4" do
assert_equal [1,2,2], Math.primes(4)
end
should "return [1,5] for 5" do
assert_equal [1,5], Math.primes(5)
end
should "return [1,2,3] for 6" do
assert_equal [1,2,3], Math.primes(6)
end
should "return [1,3] for 9" do
assert_equal [1,3,3], Math.primes(9)
end
should "return [1,2,5] for 10" do
assert_equal [1,2,5], Math.primes(10)
end
end
end
and naive algorithm I created by this approach
module Math
def self.primes(n)
if n==0
return []
else
primes=[1]
for i in 2..n do
if n%i==0
while(n%i==0)
primes<<i
n=n/i
end
end
end
primes
end
end
end
edit 1 , , : unit test, , .
edit 2 , , , . , emerge, : ?
, : , , .