views:

303

answers:

6

I have a large base of code to play with written in two programming languages [C++,OTCL], it is not well documented and I have to modify parts and add new modules. I found that reading the code is the best practice [the mailing list= many questions+very very few answers].

Do you have any tips for making my reading more effective ?

  • Habits

  • Editors

  • Plugins

  • Language specific ideas

  • Anything I don't know

BTW, I am using ubuntu/8.04.1

+4  A: 

Debug. Debugging is a very effective way to understand a code.

Ricardo Acras
+2  A: 

Along with debugging is vast quantities of logging. This is my favorite way to track program state while it is running.

EBGreen
+3  A: 

To dovetail off the debugging recommendation (which I am also a fan of) I'd also suggest trying to write unit tests for the code. If there aren't any I'd try and write some high level ones and work your way in or if there are some existing ones I'd start there prior to digging into the code.

Kevin McMahon
+1  A: 

To slightly expand on Ricardo Acras's answer, stepping through code with a debugger can be real help. Also, getting someone knows how the code works to step through it with you or doing a code review works well.

Fixing bugs and refactoring code are ideal tasks for learning a code base, especially if backed up by a comprehensive test suite to show you the error of your ways. It's a sort of learn by doing approach.

I wouldn't add comments to the code itself, unless you save off a personal copy. Notes written by newcomers often appear naive to people who have worked with a codebase for any amount of time. On the other hand, a reverse engineered design document will often be warmly welcomed.

If you are new to the language the code is written in, you'll have a doubly hard task. Poking around simpler examples, especially those that actually use language idioms, will give you some necessary background.

Jon Ericson
+1  A: 

One thing that I have found useful, in addition to debugging and logging as mentioned by others, when trying to make sense of spaghetti, is to have an old dot matrix printer with fanfold paper in the garage.

You can then pencil in the code paths, notes, abuse to the author as needed, very useful if ever you have to work on anything with gotos.

Thankfully have not needed for a few years, but good for an emergency.

seanb
I always called that desktop compiling and I agree that having it available is handy.
EBGreen
I like the desktop compiling term, I think one of my CS profs used to call it that, have not heard it for years though.
seanb
A: 

A pen and a piece of paper is my best tool ;)

I'll use it to diagram the application so that I understand flow and dependencies. This is especially useful for larger applications where you have to figure out the immediate and secondary impacts of changes.

Chris Lively