[Tutor] unwanted 'zero' ending

Peter Otten __peter__ at web.de
Thu Jun 27 10:30:51 CEST 2013


Jim Mooney wrote:

> On 27 June 2013 00:43, Alan Gauld <alan.gauld at btinternet.com> wrote:
>> Take the constant definitions out of the function....
> 
> Since the program has an error And needs simplification (no doubt
> interdependent), but it would be hard to do both at once, this brings
> up a good general question: Is it best, in such cases, to correct the
> error, then simplify, or simplify, then correct the error?

That depends. Ideally you'd have unittests to ensure that you aren't 
introducing new errors with the simplification process.

So if you don't have them already now is the time to formalize your ad-hoc 
tests.

Then, as you already tried and didn't find the error you should simplify 
your code and split it into multiple functions. For example

def triplet_to_text(triplet):
    ...

has only 100 possible args and 100 possible results. It's one of the rare 
birds for that you can write exhaustive tests. Once you have a correct 
implementation -- whether simple and clean, convoluted, or just a big dict 
doesn't matter -- you can use it as a building block for numstonames() and 
never look at it again. 

Now you can attack the next problem, splitting a number into triplets. You 
can't do exhaustive tests for that, but try to add tests for a wide range of 
sizes and to cover all corner cases.

With these building blocks the final implementation of

numstonames() -- I suggest number_to_name(n) as a sane name -- should become 
fairly simple, and errors should become easier to debug and fix. Because you 
know you can rely on the helper functions the amount of code you need to 
check has become smaller -- and ideally simpler, too.




More information about the Tutor mailing list