I'll admit it, when I'm writing code just for myself to learn stuff I tend not to unit test. When I'm in a team environment I insist on it as the person coding isn't going to be the person maintaining. Unit Testing has lots of benefits in terms of project success, code quality and longevity. If you think testing first then you tend to think about the solution to the problem a bit more and that is always a good thing. I tend to think about design by contract over unit testing, but unit testing tends to be the only way to build such things.
So lets take an example... file compression. The sort of thing that a single class can easily do. We've got two functions
char *compress(char *bytes)
char *uncompress(char *bytes)
Right now what are our core unit tests?
Well the first one of course is that if we compress something then uncompress it then it must be the same as the original. The second one is also pretty obvious namely that the compress method should result in something smaller than the input. Pretty much the basic method for such things.
Over at Microsoft Office however the 'compress' function built into MS Office, at least on the Mac, which is a one time thing to 'reduce file size' has a slightly different approach. When you do 'reduce file size' on a presentation it manages to sometimes increase the file size.
That sort of thing should really have been caught in Unit Test.
So lets take an example... file compression. The sort of thing that a single class can easily do. We've got two functions
char *compress(char *bytes)
char *uncompress(char *bytes)
Right now what are our core unit tests?
Well the first one of course is that if we compress something then uncompress it then it must be the same as the original. The second one is also pretty obvious namely that the compress method should result in something smaller than the input. Pretty much the basic method for such things.
Over at Microsoft Office however the 'compress' function built into MS Office, at least on the Mac, which is a one time thing to 'reduce file size' has a slightly different approach. When you do 'reduce file size' on a presentation it manages to sometimes increase the file size.
That sort of thing should really have been caught in Unit Test.
2 comments:
You didn't major in computer science did you?
If you did, you would know that running a compression algorithm on something that is already compress (all images except for bitmaps) will most likely increase their size, unless you run over several files.
Oddly I did, that is why I know that if you attempt to compress something and fail the right answer is to report that you are unable to compress. Microsoft's 'reduce' aims to also adjust the PPI which should give it more options to reduce the size, again if it fails the correct output would be to leave the file alone.
Basic Computer Science, if the starting state is nearer to the desired state than the final state then revert to start.
Post a Comment