Chúng tôi kêu gọi thiết kế thử nghiệm điều khiển (TDD) hoặc lập trình thử nghiệm đầu tiên, bởi vì thiết kế và thực hiện một API được hướng dẫn bởi các trường hợp thử nghiệm của mình. Lập trình viên viết các kiểm tra cách ông muốn API để làm việc, và ông thực hiện các API để thực hiện đầy đủ những kỳ vọng đặt ra bởi thử nghiệm. | Refactor the Unit Tests After we move the common code into the base clase we run all the existing tests to be sure we didn t break anything. However we aren t done. We have a new class which deserves its own unit test. and have four cases in common. That s unnecessary duplication and here s with the cases factored out use strict use Test More tests 5 use Test Exception BEGIN use_ok 39 MABase 39 dies_ok MABase- new -2 dies_ok MABase- new 0 lives_ok MABase- new 1 dies_ok MABase- new After running the new test we refactor and similarly as follows use strict use Test More tests 5 BEGIN use_ok 39 EMA 39 ok my ema EMA- new 4 is ema- compute 5 5 is ema- compute 5 5 is ema- compute 10 7 By removing the redundancy we make the classes and their tests cohesive. MABase and its test is concerned with validating length. EMA and SMA are responsible for computing moving averages. This conceptual clarity also known as cohesion is what we strive for. Copyright 2004 Robert Nagler 102 All rights reserved nagler@ Fixing a Defect The design is better but it s wrong. The customer noticed the difference between the Yahoo graph and the one produced by the algorithms above Incorrect moving average graph The lines on this graph start from the same point. On the Yahoo graph in the SMA Unit Test you see that the moving averages don t start at the same value as the price. The problem is that a 20 day moving average with one data point is not valid because the single data point is weighted incorrectly. The results are skewed towards the initial prices. The solution to the problem is to build up the moving average data before the initial display point. The build up period varies with the type of moving average. For an SMA the build up length is the same as the length of the average minus one that is the average is correctly weighted on the length price. For an EMA the build up length is usually twice the length because the influence of