tailieunhanh - Extreme Programming in Perl Robert Nagler phần 7

Tham khảo tài liệu 'extreme programming in perl robert nagler phần 7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | We want asymmetric weights so that defects such as swapping today s price and yesterday s average will be detected. A length of 4 yields an alpha of 2 5 and makes the equation asymmetric today s average today s price x yesterday s average x With alpha fixed at we can pick prices that make today s average an integer. Specifically multiples of 5 work nicely. I like prices to go up so I chose 10 for today s price and 5 for yesterday s average. the initial price . This makes today s average equal to 7 and our test becomes ok my ema EMA- new 4 is ema- compute 5 5 is ema- compute 5 5 is ema- compute 10 7 Again I revised the base cases to keep the test short. Any value in the base cases will work so we might as well save testing time through reuse. Our test and implementation are essentially complete. All paths through the code are tested and EMA could be used in production if it is used properly. That is EMA is complete if all we care about is conformant behavior. The implementation currently ignores what happens when new is given an invalid value for length. Fail Fast Although EMA is a small part of the application it can have a great impact on quality. For example if new is passed a length of -1 Perl throws a divide-by-zero exception when alpha is computed. For other invalid values for length such as -2 new silently accepts the errant value and compute faithfully produces non-sensical values negative averages for positive prices . We can t simply ignore these cases. We need to make a decision about what to do when length is invalid. One approach would be to assume garbage-in garbage-out. If a caller supplies -2 for length it s the caller s problem. Yet this isn t what Perl s divide function does and it isn t what happens say when you try to dereference a scalar which is not a reference. The Perl interpreter calls die and I ve already mentioned in the Coding Style chapter that I prefer failing fast rather than waiting until the program can do some .