views:

596

answers:

11

Stacker Nobody asked about the most shocking thing new programmers find as they enter the field.

Very high on the list, is the impact of inheriting a codebase with which one must rapidly become acquainted. It can be quite a shock to suddenly find yourself charged with maintaining N lines of code that has been clobbered together for who knows how long, and to have a short time in which to start contributing to it.

How do you efficiently absorb all this new data? What eases this transition? Is the only real solution to have already contributed to enough open-source projects that the shock wears off?

This also applies to veteran programmers. What techniques do you use to ease the transition into a new codebase?

I added the Community-Building tag to this because I'd also like to hear some war-stories about these transitions. Feel free to share how you handled a particularly stressful learning curve.

+5  A: 

Write unit tests. You'll find the warts quicker, and you'll be more confident when the time comes to change the code.

Chris Dolan
+1  A: 

I don't know about this being "the best way", but something I did at a recent job was to write a code spider/parser (in Ruby) that went through and built a call tree (and a reverse call tree) which I could later query. This was slightly non-trivial because we had PHP which called Perl which called SQL functions/procedures. Any other code-crawling tools would help in a similar fashion (i.e. javadoc, rdoc, perldoc, Doxygen etc.).

Reading any unit tests or specs can be quite enlightening.

Documenting things helps (either for yourself, or for other teammates, current and future). Read any existing documentation.

Of course, don't underestimate the power of simply asking a fellow teammate (or your boss!) questions. Early on, I asked as often as necessary "do we have a function/script/foo that does X?"

Pistos
+7  A: 

One of the best ways to familiarize yourself with inherited code is to get your hands dirty. Start with fixing a few simple bugs and work your way into more complex ones. That will warm you up to the code better than trying to systematically review the code.

If there's a requirements or functional specification document (which is hopefully up-to-date), you must read it.

If there's a high-level or detailed design document (which is hopefully up-to-date), you probably should read it.

Another good way is to arrange a "transfer of information" session with the people who are familiar with the code, where they provide a presentation of the high level design and also do a walk-through of important/tricky parts of the code.

Ates Goral
+1  A: 

Go over the core libraries and read the function declarations. If it's C/C++, this means only the headers. Document whatever you don't understand.

The last time I did this, one of the comments I inserted was "This class is never used".

Lev
I once inherited a code base where the first time I tried to debug the app, the debugger stepped into a function commented "not used anymore" :-)
Graham Lee
+7  A: 

Pencil & Notebook ( don't get distracted trying to create a unrequested solution)

  • Make notes as you go and take an hour every monday to read thru and arrange the notes from previous weeks

  • with large codebases first impressions can be deceiving and issues tend to rearrange themselves rapidly while you are familiarizing yourself.

  • Remember the issues from your last work environment aren't necessarily valid or germane in your new environment. Beware of preconceived notions.

  • The notes/observations you make will help you learn quickly what questions to ask and of whom. Hopefully you've been gathering the names of all the official (and unofficial) stakeholders.

kloucks
+1  A: 

Do try to understand the code by fixing bugs in it. Do correct or maintain documentation. Don't modify comments in the code itself, that risks introducing new bugs.

In our line of work, generally speaking we do no changes to production code without good reason. This includes cosmetic changes; even these can introduce bugs.

No matter how disgusting a section of code seems, don't be tempted to rewrite it unless you have a bugfix or other change to do. If you spot a bug (or possible bug) when reading the code trying to learn it, record the bug for later triage, but don't attempt to fix it.

MarkR
+1  A: 

One thing vi and emacs users can do is use tags. Tags are contained in a file ( usually called TAGS ). You generate one or more tags files by a command ( etags for emacs vtags for vi ). Then we you edit source code and you see a confusing function or variable you load the tags file and it will take you to where the function is declared ( not perfect by good enough ). I've actually written some macros that let you navigate source using Alt-cursor, sort of like popd and pushd in many flavors of UNIX.

BubbaT

BubbaT
+1  A: 

My steps would be:

1.) Setup a source insight( or any good source code browser you use) workspace/project with all the source, header files, in the code base. Browsly at a higher level from the top most function(main) to lowermost function. During this code browsing, keep making notes on a paper/or a word document tracing the flow of the function calls. Do not get into function implementation nitti-gritties in this step, keep that for a later iterations. In this step keep track of what arguments are passed on to functions, return values, how the arguments that are passed to functions are initialized how the value of those arguments set modified, how the return values are used ?

2.) After one iteration of step 1.) after which you have some level of code and data structures used in the code base, setup a MSVC (or any other relevant compiler project according to the programming language of the code base), compile the code, execute with a valid test case, and single step through the code again from main till the last level of function. In between the function calls keep moting the values of variables passed, returned, various code paths taken, various code paths avoided, etc.

3.) Keep repeating 1.) and 2.) in iteratively till you are comfortable up to a point that you can change some code/add some code/find a bug in exisitng code/fix the bug!

-AD

goldenmean
A: 

The first thing I do before going down into code is to use the application (as several different users, if necessary) to understand all the functionalities and see how they connect (how information flows inside the application).

After that I examine the framework in which the application was built, so that I can make a direct relationship between all the interfaces I have just seen with some View or UI code.

Then I look at the database and any database commands handling layer (if applicable), to understand how that information (which users manipulate) is stored and how it goes to and comes from the application

Finally, after learning where data comes from and how it is displayed I look at the business logic layer to see how data gets transformed.

I believe every application architecture can de divided like this and knowning the overall function (a who is who in your application) might be beneficial before really debugging it or adding new stuff - that is, if you have enough time to do so.

And yes, it also helps a lot to talk with someone who developed the current version of the software. However, if he/she is going to leave the company soon, keep a note on his/her wish list (what they wanted to do for the project but were unable to because of budget contraints).

rshimoda
+1  A: 

Try to understand the business logic behind the code. Once you know why the code was written in the first place and what it is supposed to do, you can start reading through it, or as someone said, prolly fixing a few bugs here and there

Midhat
A: 

create documentation for each thing you figured out from the codebase. find out how it works by exprimentation - changing a few lines here and there and see what happens. use geany as it speeds up the searching of commonly used variables and functions in the program and adds it to autocomplete. find out if you can contact the orignal developers of the code base, through facebook or through googling for them. find out the original purpose of the code and see if the code still fits that purpose or should be rewritten from scratch, in fulfillment of the intended purpose.

find out what frameworks did the code use, what editors did they use to produce the code.

the easiest way to deduce how a code works is by actually replicating how a certain part would have been done by you and rechecking the code if there is such a part.

it's reverse engineering - figuring out something by just trying to reengineer the solution.

most computer programmers have experience in coding, and there are certain patterns that you could look up if that's present in the code.

there are two types of code, object oriented and structurally oriented.

if you know how to do both, you're good to go, but if you aren't familiar with one or the other, you'd have to relearn how to program in that fashion to understand why it was coded that way.

in objected oriented code, you can easily create diagrams documenting the behaviors and methods of each object class.

if it's structurally oriented, meaning by function, create a functions list documenting what each function does and where it appears in the code..

i haven't done either of the above myself, as i'm a web developer it is relatively easy to figure out starting from index.php to the rest of the other pages how something works.

goodluck.