Reading source code.

Pages: 12
Apr 19, 2012 at 9:01am
Have you successfully read a source code of a project? If yes, have you fully understood all of it?? I've looked at some opensource projects (vlc, filezilla, wxwidgets, ...) but it's just too huge to devour. I guess notepad++ is the only one that somehow make sense to me when reading its code.

[edit]
BTW, macros are one those things that confuse me when reading other people's code. I also notice books don't usually focus on C/C++ macros.
Last edited on Apr 19, 2012 at 9:12am
Apr 19, 2012 at 9:20am
Because they're considered bad practice most of the time, partly because they make it very hard to interpret someone else's code.
Apr 19, 2012 at 11:17am
That and macros aren't strongly typed like the rest of the language so you can introduce errors into your code a lot easier.
Apr 19, 2012 at 11:21am
There's no reason to read the entire source code of a project (unless it's really tiny), you just need to figure out which parts you need to make the desired modifications and how those parts are designed.
Apr 19, 2012 at 2:55pm
Macros exist to make code easier to read. While it is true that they can be abused, I don't think that they are as often as they could be.

For a simple example, we have the C/C++ macro "NULL", which exists to make it obvious that we are working with pointer values. You could just write "0", and the program will be correct, but the meaning is made explicit:

 
if (foo == NULL) foo = do_something();  // foo must be a pointer... 


Macros also allow you to parameterize on your build-environment, which is not something esoteric, but a common need. Another very simple example:

1
2
3
4
5
6
7
#if defined(_WIN32)
  #include <windows.h>
#elif defined(__unix__)
  #include <unistd.h>
#else
  #error Alas, there is no support for your platform.
#endif 


Macros are messy, and can be horribly abused, but they aren't evil. (They just make great tools for evil programmers.)
Apr 19, 2012 at 3:47pm
Like me. >:D

How many of those projects had "developer's documentation" explaining where you could find certain classes/functions and what they do? I usually find that such documentation helps a lot if it's rather comprehensive.

-Albatross
Last edited on Apr 19, 2012 at 3:48pm
Apr 19, 2012 at 3:48pm
My bad, was only thinking about macros in the style of:
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
Apr 19, 2012 at 3:52pm
Yeah, just the other day I did some playing around with defines.
1
2
3
#ifdef DEBUG
      cout << word << ' ';
#endif 

Had things like that in the file and did the command line
g++ file.cpp -o fileExe -DDEBUG


I don't use them that much though because of the fact that don't have types.
Apr 19, 2012 at 4:16pm
I use the pre-processor for unit testing. I put a main function at the end of the source file of every class wrapped in #if TEST_<class name> and #endif .
Apr 19, 2012 at 6:03pm
closed account (3hM2Nwbp)
I've looked at some opensource projects (vlc, filezilla, wxwidgets, ...) but it's just too huge to devour


It wouldn't be so bad if many of those projects' docs were up to par. It still takes quite a bit of time from the point where you first encounter an alien code base to the point where you can actually make viable contributions to it. I've personally found it quite painful to read through the source code of many open source projects because of the variety of different coding conventions used in it.
Apr 19, 2012 at 6:11pm
closed account (1yR4jE8b)
I looked at the VLC source code....once...never again. It's a monstrosity of bad coding practices.
Apr 19, 2012 at 6:34pm
Remember helios' thread about it? Someone was creating an array similar to the argv[] array that is passed to the main function, just to call a function within the program.
Apr 19, 2012 at 7:56pm
VLC is an example of horrors and what goes wrong when you have no clear architectural requirements and just keeP hacking at it. Alas, just because a program is OS it doesn't mean that it is well-written or well-designed.
Apr 19, 2012 at 8:14pm
There's no way of telling if it's the same story in closed-source software, though, since obviously we don't have access to the source code.
Apr 20, 2012 at 12:15am
closed account (3hM2Nwbp)
I've worked with several closed source projects before and 100% of those projects were "better" than open-source alternatives. The reason being that the projects' requirements had a blueprint enforced by an iron fist. (Either you meet the requirements, or you're fired. Simple as that.) There was absolutely no room for sloppy practices.

"better" =

a) easier to use
b) easier to understand
c) easier to maintain
d) easier to extend

I guess that the term "better" is subjective, but that's based upon what I have personally experienced.

I may seem opposed to open source development, but I'm not. Open source software does what it's designed to do very well. However from my experience, customers like to have guaranteed official customer support for a product, something that *most open source projects do not guarantee.
Last edited on Apr 20, 2012 at 12:16am
Apr 20, 2012 at 2:49am
I've seen a few close source projects that were in worse shape than your average open software, after being in production since the 1980s, but all those companies (except one, which is now gone) were/are busy switching to modern C++, setting up effective development and testing infrastructures, and otherwise regaining competitive advantage.
Apr 20, 2012 at 9:43am
Athar wrote:
There's no reason to read the entire source code of a project (unless it's really tiny), you just need to figure out which parts you need to make the desired modifications and how those parts are designed.

Hmm, I see. I'm not trying to modify it in anyway though. I use it for learning how other programmers write code. I try to put myself on the authors shoes and to wrap my head around the code and try to guess what the author is thinking while he/she was coding.


Luc Lieber wrote:
I've personally found it quite painful to read through the source code of many open source projects because of the variety of different coding conventions used in it.

I'm glad I'm not alone :) Maybe that's why I somewhat understand notepad++ which I believe is written by one person (I might be wrong here)


Macros are almost always in use specially in cross platform projects just like Duoas said. So may I ask if anyone here knows any tutorials about macros? Like common tricks and pitfalls?
Apr 20, 2012 at 3:35pm
Avoid them if you can (unless the avoidance is immoral).

Otherwise it is just a case of using them properly.
http://www.google.com/search?q=macro+pitfalls

Hope this helps.
Apr 20, 2012 at 6:23pm
There's not that much to know about macros - you have a left hand side that gets substituted by a right hand side.
Apr 21, 2012 at 3:39am
The same could be said for variable assignment.

The trick to remember is that this happens, for all intents and purposes, as a textual transformation before your program is compiled.
Pages: 12