Re: Testing a class which utilizes randomness

Ron Jeffries <[email protected]> Fri, 4 Apr 2014 19:34:38 -0400
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <[email protected]>
Hi Nikola,

On Apr 4, 2014, at 6:53 PM, Nikola Novak <[email protected]> wrote:

> Thanks everyone for your replies. Like I said, I'm not testing the RNG (or RSG in this case), but rather the algorithm which deals with duplicates.
> I'll go with what David and John suggested, writing a class to generate unique random strings and injecting a random string generator.
> Interesting how a TDD approach resulted in code being organized properly. If I hadn't used it, I'd probably have written a method that used an embedded random string generator and would never have given the problem a second thought.


Sounds like a good start. I’ve not built this but my intuition is telling me that the design would like to have a PuttingAway object that holds strings, and a MakingStrings object that builds random strings, and some structure, perhaps a PuttingAwayUnique object but perhaps not.

If I’m right, there will be no need to create random strings for purposes of putting away uniquely: you can work with strings like “Hello”, “Goodbye”, and “Hello”. If I’m right, that will make the PuttingAway tests simpler. And that will be better.

So I recommend pushing in that direction, trying a few ways, and seeing what you come up with.

Regards,

Ron Jeffries
www.XProgramming.com
I know we always like to say it'll be easier to do it now than it
will be to do it later. Not likely. I plan to be smarter later than
I am now, so I think it'll be just as easy later, maybe even easier.
Why pay now when we can pay later?