|more useful options|
Re: Test Driven Development example + design questionby moritz (Cardinal)
|on Jul 01, 2012 at 10:28 UTC||Need Help??|
I haven't read the book that you read, so might advice might differ a bit from what the book said.
As another disclaimer, Perl is much more expressive than Java, so we can make some APIs simpler, and still have flexibility under the hood.
So, what do you know about the problem? It involves reading of some files, computations, and writing of files. What's the simplest possible API for reading files?
Maybe you'll object "but that's not object oriented!". So what? Your job is to get stuff done. You can still do some OO stuff behind the scence if you really want, but for now you should focus on making the APIs as simple as possible. Oh, and you probably return some kind of object from read_file, so there's your OO part.
Anyway, now that you have a simple API for a part of your program, you can start to write tests.
So, now you need to implement it far enough to get the tests passing.
Next you can do the same for processing the data, and for writing a file. The API of each of those can always be a single subroutine call.
If you need some more flexiblity later on, those subroutines can call some constructors on your classes and use polymorphism and whatever buzzword you want to be compatible with. But always remember that the user should seee as little complexity as possible. That way the tests also see very little complexity, and it becomes very easy to test each piece of functionality in isolation from the others.
Once you have all of this in place, you will probably want some higher level abstraction, for example something that reads several input files, passes them to a routine that does the processing, and then writes output files. Again the API for this can be a simple function, and thus it can be easy to call and test.
Again, write the tests for it and implement it. Then be happy.
Note that all this time I didn't talk about how to structure roles and classes. That's because it's not really central to the working of your program, and it is often obvious. Once you know that an API is my $data = read_file($path);, it is obvious that $data must contain an object that knows everything interesting about the data in file $path (or contains other objects that know it).