How to understand the code which wrote by someone??

Started by
23 comments, last by JasonBlochowiak 17 years, 7 months ago
I am studying basic C/C++ almost all topics... and try to testing myself by picking up the source code from web to see the codes line by line for making understanding the code. But, I am got a problem. Many times I can not understanding or clear the code to see how the code working even I think I have enough basic to figure out the code. I am not sure that what is the real problem which occurred on me. So, do you have any suggestion for me. - When I pickup the source code which wrote by someone. What is the best thing to start consideration how the code work? I know that we should looking for main( ) and typing the code and run. How to convert the code lines to visual image? I have found this porblem especialy when I see very many lines of code.... Thanks in advance from any suggestion.
Advertisement
1. one possibility why you cannot understand someones code could be that this code is bad structured ^^

2. if there are classes look at the header files, the member functions and classes should have names from which you can easily figure out what they do.
Then you could go through every function/member-function and read the code (always having in mind what the function is for)

hth

more doesn't come into my mind at the moment
Use a debugger and single step through the program.

Skizz
My idea is that you must first write your source code and understand it.
every body have spesial habits to write an source code.
Also you can give help from MSDN or something source like it.
--Mojtaba--
thank you for everyone.

More questions, is it important & necessary that we need to understand code mechnical each lines inside each function or.. just only know what the function does? Such as..how to call them? What is parameters which I send to it and what is the value return from function? (In case of I don't wanna modify them)
given enough amount of money under the right circumstances you'll understand the moon shouting out loud she's not the price for a woman's love.

in short, try harder.
[size="2"]I like the Walrus best.
Quote:Original post by eniCma
thank you for everyone.

More questions, is it important & necessary that we need to understand code mechnical each lines inside each function or.. just only know what the function does? Such as..how to call them? What is parameters which I send to it and what is the value return from function? (In case of I don't wanna modify them)


The first key point is to understand the purpose of the functions, in order to be able to use them. To do this, you may have to decompose the function in a list of algorithm and study the algorithms. This may require the reading of individual lines for complex algorithm. Debugging can help, because it essentially shows the result of each step of the algorithm.

Remember that reading code is kind of hard - you are not in the mind of the code creator, and you might miss something he considers as being obvious.

Regards,
I don't have much of an advice, just wanted to point out that understanding others code is not the same as writing the code yourself. In most cases, I think it's much easier to make an advanced application by myself instead of trying to understand a fairly basic application that someone else have made.

The difference is in other words that it's much harder to understand something you haven't made yourself, unless you're reading a tutorial where you put down each line step by step where you're following the whole process.
Good code should have comments that make it clear what's going on (so basically, unlike mine :P). If it doesn't, or it's something that really shouldn't be commented, just think about it step by step. Ask yourself "What's going on on this line?" and if you can't understand, take a step back, look at the header files, and classes, functions and data structures to try to understand better.
Originality is dead.
Well I find it incredibly hard to understand someone's code if I'm not familiar with what he's doing (what the code should be used for), especially if there are no or not clear enough comments.

So I think, basically you should be familiar with the matter befor analysing someone's code. For example, trying to understand some genetic algorithm code is really hard if you don't know what genetic algorithms are.

Understanding someone's code or even your own if a sufficient amount of time has passed since you wrote it is never easy. That's why good variable / class / method names should be used and comments should appear everywhere in your code. I know it's hard to force yourself to comment everything, but from my own experience is it that I have learned that those nice comments help me understand what I did all those months ago. Yet I often do right the opposite (no comments), shame on me.

Additionally I find it usefull to start with some pseudo code comments when I implement some function so I can use them as a guideline, e.g.

//get the object pointer
//move the object if it is movable
//draw it

would end in something like,

//get the object pointer
Object* obj = Pool.getObject();

//move the object if it is movable
if(obj->isMovable())
obj->move();

//draw it
obj->draw();


(OK, very very simple example ;P )
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement