I really enjoy writing console applications that do some sort of processing. I did that again a few weeks ago. A client needed an application that read a text file, made various modifications to it, and then output the new file. Some of the modifications included:
- Removing lines that matched particular patterns.
- Merging lines that matched particular patterns.
- Identifying lines between certain other lines. Performing some processing only on those lines.
- Replacing text within certain parts of certain lines. Some of these replacements were non-trivial (e.g., using an algorithm to change a list of values into a range of values).
It’s very easy for bugs to creep into an application like this. So I made any bugs easier to find by doing the following:
- Runtime verification. The application looked for very specific text patterns and threw an error if the patterns didn’t match. So if the application got “confused”, it threw an error — it didn’t output incorrect information.
- Log file. Verifying the differences between the input and output files using a “diff” program was time-consuming because of the sheer number of modifications. So the application wrote a list of the unique modifications it made to a log file. That way, I (and my client) could review 30 lines in a log file rather than 1,000 lines in a “diff” program.
- Unit tests. The client is planning to use the application for a long time. Some changes to the algorithms are inevitable. So I wrote a bunch of unit tests. That way, I can easily make changes in the future and verify that I didn’t break anything else.