Contents

4.5 The Search Tool

Let's talk about the Search program some more. The command line contains a file name and a text string. The Search program searches that file for every occurrence of the string, and prints a list of all the line numbers containing that string. It's a handy tool for authors who are building the index for a book. Quality assurance folks can use it to search through code for gotos and abort statements. Students can use it to search through source code to find examples of particular Ada constructs. After Search gives you a list of line numbers, you can use Line to look at those lines in context.

I can see I have created an insatiable desire to have this marvelous tool. You can't wait to turn to the back of the book to find the source listing. Don't bother. It isn't there.

4.5.1 I've Seen That Before

I took the Search program out of the text because I thought most readers would say, "I've seen that before!" You must have noticed the striking similarity of More, Write, and Line. They are really three variations of one basic program (Show). They all take data from a command line, open a file, and display the contents in a slightly different way. Search is just a fourth variation on the same theme. The only difference is that it contains a boolean function that tells if a string is contained in a line. If you can write that function, then you can edit Line into Search without much difficulty. Why don't you try it and see for yourself?

4.5.2 Keep Selling the Same Software

It isn't unusual to find yourself in a job where you keep doing essentially the same thing over and over again. If you ever write one missile simulation it is a good bet you will write more in the next year or two. If you write a compiler program, there is a good chance your boss will tell you to write another one (for another language or the same language on a different computer) as soon as you get finished. Rarely does one work on a compiler one day and a missile simulation the next. Companies and individuals keep selling the same basic product.

When I wrote the More, Write, and Line programs, I was able to do them quickly because I already developed all the building blocks I needed by the time I finished the Show program. This is what modularity and reusability is all about. I almost always start a program by making a copy of an existing program, deleting parts I don't need, and adding some new code. If you find yourself starting from scratch every time you write a new program, you are doing something wrong. Whenever you start a new program you should ask yourself, "What program have I previously written that I can use for a starting point?" You ought to be able to think of several possibilities, unless you are doing something totally new. (You can't turn a compiler design into a missile simulation, but you should be able to edit a compiler into pretty printer much faster than you could write a pretty printer from scratch.) If the answer to your question is, "Well, I could use program X, but it would take much too long to modify it.", then you deserve to be beaten severely for the bad job you did on program X. It shows that program X is not modular or maintainable.

Of course the ultimate in programming excellence is seen when everyone in your group writes such good code that you can use each other's code. Some people say that will never happen, but I don't think it is unrealistic to expect people to be able to write code good enough to share with a friend. I get by with a little help from my friends. (Dent, Leif, and Lucas-- in alphabetical order.)

The way to make big money and impress your boss with your tremendous productivity is to build on what has already been done. People will say, "Isn't that amazing! Jones wrote the More program, and the next day he wrote the Write program, and the day after that he wrote the Line program, and on the fourth day he wrote the Search program. Four different programs in four days! What a genius!" The truth is I wrote one program and sold it four times. As long as I keep my mouth shut, everybody is happy and I'm rich.


Contents | Next ...