Practical C++ Programming, 2nd Edition

Electronic Archaeology

by Steve Oualline, author of Practical C++ Programming, 2nd Edition

In college, programmers study the science of programming. They learn that you start with good requirements; this leads to a well-thought-out program design; and from this you create a coding plan, which you implement to produce a nearly perfect program.

The real world is nothing like this. In the real world, most programmers spend most of their time going through code that's a hundred years old and extremely messy. If it ever was designed, the design document was lost long ago. It has evolved over the years. Hundreds of people have worked on it. And it appears most of them knew very little about programming. As a result, most professional programmers have to deal with badly designed, badly implemented, uncommented, incomprehensible blobs.

The art of digging through ancient code is called electronic archaeology and this article discusses some of the tools you can use to make your job easier.

Ryan Hocket -- Big wrench.
Jim Hocket -- Medium wrench.
Chuck Cross -- Little wrench.
Steam engine courtesy of the Poway Midland Railroad.

There are a large number of tools out there designed to make the job of the electronic archeologist easier. These include:

Vim vi-like text editor with lots of extra features
grep Text searching program
find/grep File-finding program coupled with text search
glimpse High-speed text search tool
indent Indents programs
lxr Linux Kernel (or any large source system) cross reference
cpp C/C++ pre-processor
Source Navigator Source browsing tool


The Vim editor is a vi-like editor. But while vi is a workhorse of an editor, Vim is a space-age, heavy-duty work truck with every high-tech accessory you can imagine.

Related Reading

Practical C++ Programming
By Steve Oualline

Perhaps the newest command that Vim has is the :help command. By itself, it displays a window containing general help. If you give it the name of a command, for example :help j, it will display help for the given command.

Fast Word Searches

Sometimes the simplest things are the most powerful. In the case of Vim it's the quick-search capability. Suppose you are looking at code that uses the variable foo and you want to see where else in your program foo is used. Simply put the cursor on this word and press #. Vim will search for the next occurrence of the word. You can then use the normal vi commands n and N to repeat the search until you have scanned the entire file.

The command * works in a similar manner, only it searches backwards.

These commands are extremely useful in understanding how a variable or procedure is used in a particular file.

The :grep Command

But suppose you want to check out variables or procedures through multiple files. That's where :grep comes in.

The :grep command can be used to search through a set of files for a given string. For example, to search all the C source files for the string connection_status use the command:

 :grep connection_status *.c *.h

The editor will search all the files for the string and position the cursor on the first line that matches. To go to the next matching line, use the :cnext command. To go to the previous match, use :cprev. Finally, if you've moved around and want to return to the current match, use :cc.

The :copen command opens a new window that lists all the matches found. You can navigate to an interesting entry using the normal cursor movement commands, and then press <Enter;> to then edit that file (at the location matched).

Note: The :grep command is very similar to the :make command, which integrates program building and editing. For more information see The top 10 things a vi user should know about Vim.

Tag Navigation

In programming they are called procedures. For some reason in Vim they are called tags. The Vim editor has a number of commands that let you navigate through the procedures in your code.

The first step, before you start editing, is to create a tags file, which contains location information about the procedures in your code. This is created using the ctags command. (This command comes with Vim.)

Simply run this command on all your source files to generate the tags database:

	ctags *.c *.h

Now, when you are editing and you want to go to a particular procedure, all you have to do is execute a :tag procedure command. For example, to jump to the definition of the do_it procedure:

	:tag do_it

Let's suppose that do_it calls do_part_a. To find out where this procedure is defined, move the cursor onto the procedure call and press CTRL-]. The system will take us to the definition of do_part_a. Should this function call another subroutine, you can go to the definition of that function by putting the cursor on it and pressing CTRL-].

OK, now that you've descended the procedure call stack, you may want to return to where you started. The CTRL-T takes you back up through the call stack using CTRL-]. (So if you're in do_part_a the CTRL-T command will take you to the call in do_it.)

This system works well as long as there is only one definition of each procedure. Unfortunately, C++ introduced overloading, so there can be multiple definitions of a function. If you want to pick which definition you want, use the :tselect function. This command displays a list of all the functions that match and lets you choose which one you want. For example:

:tselect add_it
  # pri kind tag               file
  1 F   f    add_it            t.c
               char add_it(char i1, char i2)
  2 F   f    add_it            t.c
               fixed_pt add_it(fixed_pt i1, fixed_pt i2)
  3 F   f    add_it            t.c
               float add_it(float i1, float i2)
  4 F   f    add_it            t.c
               int add_it(int i1, int i2)
Enter nr of choice (<CR> to abort):

The :tselect function can also be used to search for a function using a regular expression. For example, to select from all functions with the word add in them, use the command:

	:tselect /add/

Finally, one of the innovations Vim introduced was multiple windows. To do a tag jump and display the result in a new window, use the command CTRL-W CTRL-]. (For a full discussion of how to use windows, execute the command :help windows.)

C Indentation

The Vim editor has a good understanding of C syntax and knows how to properly indent C programs. To turn on the C style indentation, use the command :set cindent. Now when you write your programs, indentation will be done automatically for you.

The number of spaces for each indent is determined by the shiftwidth option. So if you want to indent four spaces per level, use the command:

:set sw=4

But suppose you are dealing with legacy code and it's indented badly. The Vim command = will indent a section of code. The form of the command is =<motion>, where <motion> is a cursor's motion command.

Perhaps the most common use of this command is to indent a block of code that's been badly indented. You start by positioning the cursor on the first curly brace ({) of the block. Then execute the command =% (= -- indent to motion, % -- go to matching brace).

The Vim editor has hundreds of additional commands. Many of these are useful for programming and electronic archaeology. You can find out more information using the Vim help system (:help), the Official Vim Web Site, and my web site.

Pages: 1, 2, 3, 4

Next Pagearrow