views:

167

answers:

5
+10  A: 

I know that alternative to that kind of listing would be to split up big files into smaller classes/files, so that their class declaration would be self-explanatory enough.

Correct.

but some complex tasks require a lot of code

Incorrect. While a "lot" of code be required, long runs of code (over 25 lines) are a really bad idea.

actually not listing functions, but code blocks inside a function

Worse. A function that needs a table of contents must be decomposed into smaller functions.

I'm not sure is it really worth it spending your time subdividing implementation into multiple of files?

It is absolutely mandatory that you split things into smaller files. The folks that maintain, adapt and reuse your code need all the help they can get.

is it ok to create an index-listing additionally to the class/interface declaration?

No.

If you have to resort to this kind of trick, it's too big.

Also, many languages have tools to generate API docs from the code. Java, Python, C, C++ have documentation tools. Even with Javadoc, epydoc or Doxygen you still have to design things so that they are broken into intellectually manageable pieces.

  1. Make things simpler.

  2. Use a tool to create an index.

S.Lott
+5  A: 

If you create a big index you'll have to maintain it as you change your code. Most modern IDEs create list of class members anyway. it seems like a waste of time to create such index.

Al Bundy
+1: You have to maintain it. When you forget to, the index in the code and the rest of the code disagree. Is that a bug? Which should be fixed -- comment or code?
S.Lott
Well in c++ you generally have 3-instances of a function name inside your code: one in header, one in cpp implementation, and one invokation instance. One more in table-contents doesnt make much difference. Renaming it is done by search-and-replace in any case..
AareP
@AareP: What? Comparing documentation with definition and reference makes no sense at all. The function definitions and references are validated by the compiler as being identical. Your documentation is not validated by anything and can disagree with the material validated by the compiler. There's no comparison at all between comments and code -- except when you use a documentation-generating tool that checks the comments against the code.
S.Lott
@S. Lott: Of course I can't compare this approach with automatically generated or validated documentation.. But if you read my remarks about code-navigation with keyboard F4/F2-shortcuts, you can understand why my table-of-contents doesn't really require automatic validation.. I simply cannot move around my code if some information is out of date. It's not like table-of-contents of a book.. that one will never stay in sync without automatic generation (in msword) :)
AareP
@AareP: "I simply cannot move around my code if some information is out of date" False. You can certainly move around with this out of date. It's trivial to change the spelling of something you rarely -- or never -- use with your F4/F2 short cut. You're relying on luck to validate your spelling changes. If you rarely (or never) check each and every name, you'll never find the mistakes.
S.Lott
Well, at least it's not like in javascript - you mispell a variable name and everything works hanky panky as long as interpreter executes around that line of code.. Or in c++ - you write "int variable;" both in class declaration and function declaration, and compiler is happy to ignore possible mixup in variable usage..
AareP
@AareP: "at least it's not like in javascript". Yes it is. Two nearly-identical names in your "index" and you are messed up navigating to the wrong one. It is exactly like Javascript.
S.Lott
Except that in javascript you cause harm to the customer, who has no control over documentation, and even less control over code. In my case there's some other programmer, who has no control over comments (wont probably dare to modify somebody else's comments), but whose common sense says that you cannot trust comments or documentation, they can be out of date, or written in missleading way... So in my case that programmer won't go bonkers when some indices of table-of-context are incorrect. He will watch out for missordered and missplessed index-links the first time he navigates that code..
AareP
@AareP: "programmer won't go bonkers when some indices of table-of-context are incorrect" Yes, actually, they will. What causes you to claim they won't? They most certainly will delete code that's probably wrong, and comments that contain errors. And because the code and comments disagree, they will likely mess things up.
S.Lott
@S. Lott: "What causes you to claim they won't?" Own experience with other people's comments. I don't have big expectations when it comes to comments. Either they are too general and inexpressive (like "GetName returns name of an item") or out of date. And it takes a long time to fully comprehend the logic behind code and all comments. I dare not modify comments until I'm absolutely certain I understand all aspects of the code (even when editing it). Of course it's much easier when I have time to just rewrite everything to my own vision.
AareP
@AareP: Okay, I get it. You're just arguing that your indexing is perfect and cannot ever become confusing to people. You have exactly one example and it's perfect. Clearly, you're not *asking* a question in which you're thinking about the answers -- you're just repeating your position that this poorly-designed clump of code with an "index" will always work no matter how error-filled. Got it.
S.Lott
You are right, I'm not interested in opposite views about my questions (other that for debading reasons maybe), sorry.. Usually I'm Not coming to stackoverflow to solve my design problems.. but fortunately for everybody my interests have little matter here. Everybody can learn something from these questions and answers no matter what I think..
AareP
A: 

If you can split your method like that, you should probably write more methods. After this is done, you can use an IDE to give you the static call stack from the initial method.

EDIT: You can use Eclipse's 'Show Call Hierarchy' feature while programming.

kiwicptn
Which IDE are you using? At least visual studio 2003-2009 versions are really terrible with this kind of automatization. Jump-to-definition works something like in 90% percent of time.. It's like a keyboard with 90 out of 101 buttons working :)
AareP
Eclipse. You can do 'Open Call Hierarchy' (Ctrl-Alt-H), and pick the 'Show Callee' option.
kiwicptn
You mean while debugging? It's not really enough. What I need is a way to navigate through code when programming.
AareP
No, I mean while programming.http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.user/reference/cdt_u_call_hierarchy_view.htm
kiwicptn
+1  A: 

I would never ever do this sort of busy-work in my code. The most I would do manually is insert a few lines at the top of the file/class explaining what this module did and how it is intended to be used.

If a list of methods and their interfaces would be useful, I generate them automatically, through a tool such as Doxygen.

Ether
Does doxygen allow jumping between function-listing and actual code? Can you bind that to keyboard shortcuts?
AareP
Your IDE doesn't do that for you?
John Saunders
No idea.. I'm using those F2/F4 shortcuts, and notepad... just joking.. using visual studio, of course.. :)
AareP
A: 

I've done things like this. Not whole tables of contents, but a similar principle -- just ad-hoc links between comments and the exact piece of code in question. Also to link pieces of code that make the same simplifying assumptions that I suspect may need fixing up later.

You can use Visual Studio's task list to get a listing of certain types of comment. The format of the comments can be configured in Tools|Options, Environment\Task List. This isn't something I ended up using myself but it looks like it might help with navigating the code if you use this system a lot.

brone
This task-list seems like a good idea. There's of only few problems with it. Sometimes after updating some comments it looses focus from last selected item. Also sorting is broken, it's not always sorted by the line number. Otherwise it could defenetly have been a powerful tool.
AareP