Sign in to follow this  
3dmodeler

reading source codes

Recommended Posts

I am pretty comfortable with c++ I have been looking at source codes to better understand it. It is easy to read a few pages of code for a small program but waht about thousands od lines of code, it takes a long time to understand it. any tips on reading code faster or easier? 3dmodeler

Share this post


Link to post
Share on other sites
The trick is ... don't read the code. [smile]

When you need to work with a piece of code, all that is relevant (usually) is the function prototypes and type declarations you need. You rarely care about the code behind them (this assumes that this is a relatively well tested codebase). If the code has good documentation, you may not even need to open any of the files that make up the program.

As you get more experienced, you will find it easier to navigate large codebases quickly.

Also, this might be worth a read.

Share this post


Link to post
Share on other sites
Reading source code to learn how to program is one of the worst ways to learn programming. That's like watching spanish soap operas, trying to learn spanish purely from context. It might be possible, but it's going to be a lot harder than if you go out, buy a spanish book, and actually start SPEAKING spanish, no matter how poorly.

Writing programs -- no matter how simple -- is almost always a better way to learn programming than by looking at source code someone else wrote.

Share this post


Link to post
Share on other sites
I have been learning c ++ with books I just wanted to see code the way other people might use it (lots of differences in developing a project) Reading it i think can help to see how something is done.

I read that article rip-off thanks i alway heard it was a good thing to read sourcecode I guess i am wrong

3dmodeler

Share this post


Link to post
Share on other sites
Well i'm not as experienced as most of the people here that will give you advice on learning to program, but i don't see reading source code as a bad thing. Or course, it's always better to do some exercises, hands-on experience is best, but reading other peoples code can give you insight into their methods and styles. You just have to remember that their code may not the best and a book will instruct you of proper usage of the language. And it's only really worth it if the code is well commented. I don't think anyone would disagree that tutorials can be helpful, but just reading source code without any explanation on what it does can also be a good exercise.
At least, that's what i think. [smile]

Share this post


Link to post
Share on other sites
right reading it isn't totally bad but there are proper steps whne doing it. Take me for instance. One day i started reading some source from the quake 3 engine that was release under gpl. I didn't understand anything when i began even though i had written many programs. For one, the tool i used was totally lame. Visual studio regardless of what ever one says is the best tool for beginners. It has superb searching and debuging is the best i have seen. ok when reading from source you have understand some context to grasp the rest. its like when reading we often use context clues to learn new words. Its kinda the same except in coding code isn't straight forward to beginners

Share this post


Link to post
Share on other sites
Quote:
Original post by owiley
...except in coding code isn't straight forward to beginners

Yes, i agree that it complete n00bs shouldn't just sit down and start reading source code when they barely know how to program.

Share this post


Link to post
Share on other sites
don't read the code. Compile it, run it, put a breakpoint where you want to figure out how something works. Step through the code till ya understand it [smile];

-me

Share this post


Link to post
Share on other sites
I personally started learning how to program by staring at the Quake 3 source code for hours on end.

Looking back on that, I would have been better off with a good book and some exercises. A book is more structured and, if it's a good one, will explain each step of an example in terms somebody new to the concept can understand.

Blindly looking at code *can* teach you some basics but it involves investing way more time than you should have to to understand what's going on. Even then, you'll have to sit through a lot of program crashes until things make sense. Don't do it if you want to avoid headaches :)

Share this post


Link to post
Share on other sites
Heh, even reading books can cause headaches :P

What I'm gathering is that you are fairly comfortable in C++ but you can't understand what you are seeing when you read others codes. Well, I don't necessarily think anything is wrong with that. Unless it is fairly well documented/commented, it's GOING to be hard to understand in some cases, not many people use the same association when they are naming their classes, objects, etc, and when it is something different than what you are used to doing yourself (again, without proper commenting), you are going to be confused. Length shouldn't be an issue, I think you are just looking at some code that hasn't been explained fairly well.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad

What I'm gathering is that you are fairly comfortable in C++ but you can't understand what you are seeing when you read others codes. Well, I don't necessarily think anything is wrong with that. Unless it is fairly well documented/commented, it's GOING to be hard to understand in some cases, not many people use the same association when they are naming their classes, objects, etc, and when it is something different than what you are used to doing yourself (again, without proper commenting), you are going to be confused. Length shouldn't be an issue, I think you are just looking at some code that hasn't been explained fairly well.


Comments don't help a reader that isn't familiar with domain idioms.

Someone can be told that library uses prototype objects created by factory, and understand thousands of lines of code. Someone who isn't familiar with "prototype" and "factory" patterns can read tens of thousands of lines of documentation but not be able to make sense of it. Or perhaps that some large block is socket library.

This is why effective libraries follow the Principle of Least Surprise. They are designed in a way that the target audience expects them to be. This audience can be very small, and be expected to have very thorough understanding of much broader or seemingly unrelated concepts.

Physics library usually implies one understands floating point accuracy, numeric stability, O(n) complexity of various tests, topological implications of various algorithms and much more. Even though it will be used by only calling tick() function 50 times a second.

Quote:
any tips on reading code faster or easier?


Years of experience of active coding, and studying software design methodologies and idioms. There is no shortcut.

Code is merely implementation of higher level concepts. But implementation itself can be terse or verbose, convoluted or elegant - it still represents same concept.

This is akin to trying to read a book by evaluating a letter at a time. To be effective, the minimum one needs is ability to understand sentences. But to truly understand the message of prose, one needs to be familiar with context, perhaps author's premise, cultural, political or other backgrounds.


This is one reason why different "levels" of languages exist. One line of code in Python can equal 20 lines of code in C++ or 2000 lines in assembly. They carry same message, but one of those is easier to understand than other two.

C++ is also worst of all since due to its flexible and verbose nature, as well as manual resource management concepts, embeds surprising amounts of knowledge into simplest of operations. While assignment operator (=) means one thing in most languages, PhD thesis can be written on full extent and implications of its use in C++.

Quote:
but waht about thousands od lines of code
or tens, hundreds thousands or even millions of lines of code.

Any project that large will have some high-level functional structure, which will be briefly described in accompanying documentation, or be implied from the context. User is then expected to be familiar with the domain to understand it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
don't read the code. Compile it, run it, put a breakpoint where you want to figure out how something works. Step through the code till ya understand it [smile];

Yes, that's even better. Seeing how the code actually works is surely better than a book telling you how it *should* work. I'm not saying that beginners shouldn't read books but you need hands-on experience and sometimes it's hard to just sit down and start typing yourself without seeing how other people to it.
Anyway, that's just another of my 2 cents (making it 6 in total [smile])

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this