Solved

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

Posted on 2008-10-30
5
606 Views
Last Modified: 2013-11-13
Hi,

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!
0
Comment
Question by:Cognize
5 Comments
 
LVL 2

Accepted Solution

by:
devshb earned 150 total points
Comment Utility
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.
0
 
LVL 16

Assisted Solution

by:brad2575
brad2575 earned 150 total points
Comment Utility
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.
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 50 total points
Comment Utility
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 (http://www.doxygen.org/). 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.
0
 
LVL 31

Assisted Solution

by:moorhouselondon
moorhouselondon earned 150 total points
Comment Utility
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?


0
 
LVL 31

Expert Comment

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

var
  somevariable:string;

procedure someproc;
var
  somevariable:string;
begin
some code
end;

procedure anotherproc;
begin
some more code
end;

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.  


0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now