Tips on reading and understanding other peoples code - any recommended methods?


I'm wondering if anyone has any good advice on understanding code written by other people. I'm about a year into my programming carreer (Mostly in C#) and am fairly confident in my coding, but understanding code in software written by others is very time consuming and quite difficult (Often, there is little documentation around in my experience so far!).

Is this just "one of those things" that will always be tricky, or does anyone have a killer methodology for getting to grips with large code projects that you didn't write your self?

Very general question but any advice would be appreciated!
Who is Participating?
devshbConnect With a Mentor Commented:
I've been a programmer for about 20 years, and used loads of different languages, but there are still some situations where I look at the code and it takes ages to work out what it does and how it works.

Usually the problem is when the code is:
1) Not properly modularised
2) Not commented
3) Uses unfathomable/un-english variable/function names.
4) Has no documentation

Technically you don't usually need much documentation, even on complex/large projects, as long as the code is properly structure/modularised/written. Often technical documentation (ie specs) are counter productive because they end up out of date with the code. Specs are a nice idea, but in practice they often end up doing more harm than good. Having said that, a top-level spec which explains the general overall flow of key bits of the project are always a good idea, as are things like entity diagrams for tables.

Sometimes it's actually quicker to re-write messy code than it is to analyse/fix it. You'd take each situation one at a time to see what you think the best approach would be.

If the code relates to something which is used by other people (eg a shopping cart which is used by other developers/merchants), then see if you can find the customer/developer forum for that software and ask questions there (similar for general programming questions; always look at forums and ask questions).

Most forums are very friendly/helpful (and usually free), no questions are deemed too simple, because all decent developers know that nobody knows everything and that everyone has to start somewhere, so usually people will be very helpful.

Never be afraid to ask questions, even if it's something simple like "how do I comment my code?" or "how do I add 2 text string together?" etc

Anyway; forums; I think that's your best bet in the early stages of learning programming or looking at any kind of software. You can often get an answer in 5 minutes which otherwise could have meant you running round in circles for weeks.
brad2575Connect With a Mentor Commented:
This is not easy to answer.  The biggest thing is to understand how the developers code you are viewing was written and get a feel for how they wrote the code.  Then you can be better equipped to read and unsertand their code.

It also helps if the code you are reading is documented and commented well (which it sounds like it is not).

In my experience I have had different methods for understanding the code depending on who wrote it and how familar I am with their coding style already.

One thing (if you can do this) is to add comments to the code as you are going through it so you can better understand it as you are going through it.  If you figure out what something does (and it is not obvious) add comments to it.  This will help yourself right now, and down the road when you have to come back to the code later.  It will also help developters other then yourself later on if they have to read the code as well.  This will take some time but it will help out in the long run.
peprConnect With a Mentor Commented:
To add to what the other said. It is always better when you have any tool that helps you to discover the structure of the application faster than by simply staring at the source. Try, for example, the freely available Doxygen tool ( It is capable to generate the documentation even from non-commented sources. It generates the "caller" and "called by" graphs, class hierarchy, hypertexted sources (HTML) with jumps to definitions of the functions, etc.
moorhouselondonConnect With a Mentor Commented:
I have had to unravel many different applications over the years to understand them and to either update or rewrite them.  

One thing to establish is whether the program to be analysed relies on outside interaction (other than from a user at their keyboard).  If you are analysing a program which expects input on a COM port for example, the techniques for diagnosing that are going to be different to that where all information is contained within the code you are looking at e.g. a word processing app.

If the original programmer understood structure enough to use procedures and functions, I would start by trying to write a kind of heirarchy chart to see what procedure is called by which procedure.  This gives a rough map of the app.  This is good to do simply because you can find out whether procedures are called by one procedure, or by lots of procedures.  If you decide to alter a procedure, you can then be confident knowing what the dependancies are on that particular procedure.  In many cases a programmer might decide to do lots of things in a procedure, start to code them in, realize that its better to separate it out, but leave redundant code in.

Similar thing with loops.  Analyze all loops to see where the begin, where they end.  What goes into the loop, what comes out at the other end.  What does the loop do?

Variable scoping is something that is a nightmare to interpret.  I use Delphi, let me give you a Delphi example:-


procedure someproc;
some code

procedure anotherproc;
some more code

A common mistake is where coders forget that they are using a global variable, or a local variable, or vice versa.  You might see code in someproc which starts to use somevariable without assigning a value to it first.  Delphi helpfully warns you about this in the compiler messages, but you can gaily ignore this warning and end up with bugs.  A good way to smarten this up, particularly if you can recompile the program yourself as you go along, is to rename any public/global variables with say a glob prefix e.g., globsomevariable.  You will be amazed at the apps that are out there in the real world that are relying on the compiler to pre-initialise strings and integers.  

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.