Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What do you do to understand some code that you didn't write? Or code that you wrote long time ago and don't remember what it does anymore.

Do you have some technique that you go about? Do you analyze the structures first, or the public methods, or do you draw flow charts, etc.? Or do you fire up the debugger and just step through it? Or do you just ad-hoc your way through until you understand it?

share|improve this question
add comment

14 Answers

up vote 15 down vote accepted
  1. Asking the author
  2. Going with the debugger through in different scenarios
  3. Saving discoveries in written form
  4. Learning by trying to add/change something and seeing where it leads
  5. Doing some pair programming with an experienced colleague or the author
share|improve this answer
4  
+1 for asking the author. –  gablin Dec 21 '10 at 9:15
2  
+1 for "saving discoveries in written form". I follow a similar process and having data flow diagrams etc makes it much easier to get inside the author's thought processes. –  Gary Rowe Dec 21 '10 at 9:44
1  
"Saving discoveries in written form": Since finding no documentation and no meaningful variable or function names becomes the norm in my job, i start with adding comments and making names easier to understand when i finally "get" a code fragment. –  LennyProgrammers Dec 21 '10 at 14:55
2  
I wish asking the author was a reasonable choice more often. –  Logan Capaldo Dec 22 '10 at 3:14
add comment

Usually, I analyse a simple part first, e.g. the module used to maintain a small table. This teaches me the style the other programmer is using. If I have problems understanding even that, it's either very badly written or my knowledge of the language, framework etc. is insufficient. Once I grasp the simple part, it's time to move to the more complex parts of the program.

share|improve this answer
add comment

refactoring

the code, thus making it clearer, and in standard that ah-hock established.

share|improve this answer
    
And when people need to view your code, they refactor it again? Neverending story (hum: hihihihihihihi).. –  Arcturus Dec 21 '10 at 10:08
2  
@Arcturus , yes, its a never ending story of code maintenance. ill ask you this: is there a way to get from 3 diffrent people the same code? –  Display Name Dec 21 '10 at 10:37
    
@bold: no, of course there is no way, but to refactor all the code you come across seems a bit overkill to me.. Just learn to adjust to other peoples code style, instead of refactoring (and perhaps breaking) working code. –  Arcturus Dec 21 '10 at 10:41
    
@Arcturus ether you do not understand re-factoring as Folwer presented, or you do not agree with Folwer methods. read the book about refactoring, it will change your understanding about reading code. –  Display Name Dec 21 '10 at 11:23
2  
If new people view your code and refactor it again, that is just fine in my book. –  Marcie Dec 21 '10 at 14:24
show 3 more comments

Try to fix a bug

Best possible way to get to know the code! :)

share|improve this answer
    
Chicken-egg problem. How do you fix a bug without knowing the code? –  Pacerier May 12 at 13:26
add comment

I use a mix of:

  • Writing tests for it
  • Changing it to see how it break
  • Refactoring

Not necessarily in this order :-) It's astonishing how much easier it becomes to understand what a piece of code does after some refactoring.

share|improve this answer
1  
+1 for mentioning writing tests. It usually helps because you go through the whole method and can compare your expectations about how it should work against how it actually works. And you actually created something useful for the future. –  Anne Schuessler Dec 21 '10 at 14:19
add comment

Generate/draw/read a call graph.

share|improve this answer
add comment
  1. Run the programm with a simple test case
  2. Step through the code with the same test case
  3. Use more difficult test cases
share|improve this answer
add comment

this is coming with the experience. when you're a newbie, or you jump into other programming language is a little bit difficult. when you have several years working with a language, then is easier to understand.

but, as a general rule, I'm firing up the debugger, and start to understand what's happening in it. also it is VERY IMPORTANT to comment your code, and to work on documented code.

share|improve this answer
add comment

Good question, I don't think I have ever sat down and documented the process.

I guess thinking about it now, I just read it:

Line by Line

Of course this doesn't always work, asking the author is usually the last resort.

share|improve this answer
    
reading line by line is not useful, you need to grasp the abstraction in the code, and not step throw it in a way that no one ever intended it to sequence. –  Display Name Dec 21 '10 at 10:07
    
Yes I agree that one needs to grasp the abstraction. I'm not saying its the only method, put that't what I've found myself doing. This may or may not work for others and probably not in every situation. –  Darknight Dec 21 '10 at 10:13
add comment

I go through the usecases. Each use-case starts at some point and finishes at another. Start looking at the begining and follow the flow. When you've examined three or four usecases you know the structure of the code.

Preferably you should be writing tests when following the code, since it will help you keep a more active role in examining the code than reading it line by line.

The debugger is a great tool to follow the flow, you could make some quick tests that doesn't realy assert anything but start the code at the point you want to debug from, to get a quick starting point for the debugger.

Though depending on how comfortable you are with tests, tests might be faster to check expected results.

share|improve this answer
add comment

Use a white board to write out and diagram interactions between classes or methods. This can help you to see the flow of the program. Once you have the 100ft view, then start digging, tracing and debugging to find the nuances of the system.

share|improve this answer
add comment

How do I go about understanding others code?

Well, most I don't go about it at all. I only try to understand it if it doesn't work, and I'm trying to figure out if I did something wrong or "the other" did. And the tools I use for that is reading the code and using the debugger.

If that also doesn't help, I mail the author.

share|improve this answer
add comment

I imagine how I'd write the code and look for similarities.

share|improve this answer
add comment

Different people have different learning styles, so you have to choose the method that works best for you.

The first thing I do (after building the project) is read the entire code base through at least once. That gives me a general idea of where everything is. Then I choose a section to examine in more detail. Data structures would be a good place to start. Once I have a general idea of what's going on, I do the same with another portion of the code that interacts with the first. After enough iterations, I have a good sense of how the code works.

share|improve this answer
    
What if there's a ton ton of code? –  Pacerier May 12 at 13:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.