views:

371

answers:

6

This is not about comprehension. This is about "I want to find where this class is defined."

I recently had this "insight" where

  1. each class in its own .cpp/.hpp file (Java style)
  2. each namespace is its own directory

So something like

Geometry::Math::Vector3

would be in Geometry/Math/Vector3.hpp and Geometry/Math/Vector3.cpp

Have others tried this? (And possible good/bad side effects?)

What other techniques do you use to organize where files reside for largeish C++ projects?

+6  A: 

If you use Vim or Emacs, etags / ctags will build indexes for you, to allow jumping between definitions. Microsoft's IDE can do similar things, as long as you don't mind it crashing w/ large variable names. Most (code) editors provide this functionality out of the box.

Pestilence
And go ahead and add a `tags` target to your makefile. Depending on the size of your code base and how OCD you are about this, you can even make the global target depend on it to get automatic rebuilding of the tags database (cast some time, though).
dmckee
What's this about Visual Studio "crashing w/ large variable names"? I haven't noticed any problems like this, but maybe my variable names aren't long enough...
Michael Burr
lol. I was on a big code base with long names and it was crashing Intellisense on VC6 (reasonable). So, naturally, I thought an upgrade would fix it. VC 2005 has the same problem. I spent *weeks* trying to fix it. Imagine an entire codebase where you *need* to turn it off. I narrowed it down to the length of our namespaces, but never found the "magic" acceptable length.
Pestilence
@dmckee Yes. I *always* add a "tags" target, if it's not already there.
Pestilence
A: 

I pretty much include a module's dependencies in it's separate folder.
And all the times I use the right click option Go to definition to jump.

Pentium10
+3  A: 

I find things with grep: grep -n 'class Myclass' *.h *.cpp.

But what you're describing is how Java organizes its files, so it is a reasonable idea.

Rex Kerr
I like to install http://betterthangrep.com/ and `:set grepprg=ack`. The result is a pretty awesome `:grep` :-)
ephemient
grep over ctags? really?
Pestilence
I prefer grep over ctags as my general-purpose tool--not only can I find class declarations but lots of other stuff really easily (and usually less clunkily than an editor/IDE does). But I agree that ctags is very useful, and simply for finding class declarations one can argue it's superior.
Rex Kerr
I used to use ctags all the time, but there are some things that ctags don't pick up. grep is always a reliable fallback, particularly if you have an "IDE" that lets you execute shell commands from within.
Ben Collins
+1  A: 

Having often had to work on large trees organised in a variety of different ways, I tend to rely on searching tools like grep - you'll have it on linux and will be able to get it via msys on windows. This way I can pull out all uses of a particular statement/function easily. I could even re-factor via sed if I was so-minded.

I quite like the Java layout and wouldn't say there's anything wrong with it, aside from sometimes deep folder trees. I personally organise source code into functional sections and it is very much subjective based on how the code breaks up.

Edit - you can also use find to search for file names and do operations on the files with -exec command+opts {} \;

Ninefingers
`grep` is OK especially in conjunction with `find`, but tags are faster and are well supported by most programmers editors that don't provide a native version of this functionality.
dmckee
A: 

I have a little one line shell script that I placed in my command path, named "fvi", and its contents are as follows:

vi $(find . \( -name "*.cpp" -o -name "*.h" \) -print | xargs grep -l "$@")

Then in the terminal window I just cd to the directory that I want to do a recursive search over, and enter

fvi some_keyword

... and vi is launched with the set of .cpp or .h files containing that keyword, which I can step through quickly with :n and :N. Works pretty well.

Jeremy Friesner
A: 

The organization approach you suggest seems sound to me. I use it myself and have not found any problems. Source files should not be 10kloc long. :o

I often bend the one file per class rule. When a few small classes are meant to be used together, I'll sometimes just put them all in one module. For example, a custom collection class and its iterators. So long as the file doesn't grow too large.

If you're using namespaces to wrap an enum, or to implement Boost's "detail" idiom, then I wouldn't put them in their own subdirectory.

Most decent IDEs have a symbols browser and a "Find In Files" feature to quickly find classes/functions in a project's source tree.

When I want to familiarize myself with a new C/C++ project, I run the source tree through Doxygen, and enable SOURCE_BROWSER and most EXTRACT_XXX options. Even if there are no Doxygen tags in the source code, you'll still have a nice way to quickly navigate through the source tree. The SOURCE_BROWSER is an awesome feature that produces code listings with hyperlinks to all known entities (namespaces, classes, functions, etc). To search for a class/function, you can go to the index page that Doxygen generates, and use the find feature of your web browser. You can also make Doxygen generate a search engine (which needs PHP).

Doxygen will also produce snazzy "collaboration" diagrams of your classes and header files, so you can see what the dependencies are.

Hope this helps.

Emile Cormier