在代码中使用目录?
您是否使用目录在大源代码文件的开头列出类的所有函数(可能还有变量)?我知道这种列表的替代方法是将大文件分割成较小的类/文件,以便它们的类声明足够不言自明......但是一些复杂的任务需要大量代码。我不确定花时间将实现细分为多个文件是否真的值得?或者可以在类/接口声明之外创建索引列表吗?
编辑:
为了更好地说明我如何使用目录,这是我的爱好项目中的一个示例。它实际上不是列出函数,而是函数内部的代码块..但是您可能无论如何都能明白这个想法..
/*
CONTENTS
Order_mouse_from_to_points
Lines_intersecting_with_upper_point
Lines_intersecting_with_both_points
Lines_not_intersecting
Lines_intersecting_bottom_points
Update_intersection_range_indices
Rough_method
Normal_method
First_selected_item
Last_selected_item
Other_selected_item
*/
void SelectionManager::FindSelection()
{
// Order_mouse_from_to_points
...
// Lines_intersecting_with_upper_point
...
// Lines_intersecting_with_both_points
...
// Lines_not_intersecting
...
// Lines_intersecting_bottom_points
...
// Update_intersection_range_indices
for(...)
{
// Rough_method
....
// Normal_method
if(...)
{
// First_selected_item
...
// Last_selected_item
...
// Other_selected_item
...
}
}
}
请注意,索引项没有空格。因此,我可以单击其中一个并按 F4 跳转到项目使用情况,然后按 F2 跳转回来(简单的 Visual Studio 查找下一个/上一个快捷方式)。
编辑:
此索引的另一种替代解决方案是使用折叠的 c# 区域。您可以将 Visual Studio 配置为仅显示区域名称并隐藏所有代码。当然,对源代码导航的键盘支持相当麻烦......
Do you use table-of-contents for listing all the functions (and maybe variables) of a class in the beginning of big source code file? 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.. but some complex tasks require a lot of code. I'm not sure is it really worth it spending your time subdividing implementation into multiple of files? Or is it ok to create an index-listing additionally to the class/interface declaration?
EDIT:
To better illustrate how I use table-of-contents this is an example from my hobby project. It's actually not listing functions, but code blocks inside a function.. but you can probably get the idea anyway..
/*
CONTENTS
Order_mouse_from_to_points
Lines_intersecting_with_upper_point
Lines_intersecting_with_both_points
Lines_not_intersecting
Lines_intersecting_bottom_points
Update_intersection_range_indices
Rough_method
Normal_method
First_selected_item
Last_selected_item
Other_selected_item
*/
void SelectionManager::FindSelection()
{
// Order_mouse_from_to_points
...
// Lines_intersecting_with_upper_point
...
// Lines_intersecting_with_both_points
...
// Lines_not_intersecting
...
// Lines_intersecting_bottom_points
...
// Update_intersection_range_indices
for(...)
{
// Rough_method
....
// Normal_method
if(...)
{
// First_selected_item
...
// Last_selected_item
...
// Other_selected_item
...
}
}
}
Notice that index-items don't have spaces. Because of this I can click on one them and press F4 to jump to the item-usage, and F2 to jump back (simple visual studio find-next/prevous-shortcuts).
EDIT:
Another alternative solution to this indexing is using collapsed c# regions. You can configure visual studio to show only region names and hide all the code. Of course keyboard support for that source code navigation is pretty cumbersome...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正确的。
不正确。虽然需要“大量”代码,但长时间运行代码(超过 25 行)确实是一个坏主意。
更糟糕的是。需要目录的函数必须分解为更小的函数。
将内容分割成更小的文件是绝对必要的。维护、调整和重用您的代码的人员需要他们可以获得的所有帮助。
不行,
如果非要使出这种招数,那就太大了。
此外,许多语言都有从代码生成 API 文档的工具。 Java、Python、C、C++都有文档工具。即使使用 Javadoc、epydoc 或 Doxygen,您仍然必须进行设计,以便将它们分解为可智能管理的部分。
让事情变得更简单。
使用工具创建索引。
Correct.
Incorrect. While a "lot" of code be required, long runs of code (over 25 lines) are a really bad idea.
Worse. A function that needs a table of contents must be decomposed into smaller functions.
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.
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.
Make things simpler.
Use a tool to create an index.
如果您创建一个大索引,则必须在更改代码时维护它。无论如何,大多数现代 IDE 都会创建类成员列表。创建这样的索引似乎是浪费时间。
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.
我永远不会在我的代码中做这种忙碌的工作。我手动做的最多的事情就是在文件/类的顶部插入几行,解释这个模块的作用以及它的用途。
如果方法及其接口的列表有用,我会通过 Doxygen 等工具自动生成它们。
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.
我曾经做过这样的事情。不是完整的目录,而是类似的原则——只是注释和相关代码之间的临时链接。还要链接一些代码片段,这些代码片段做出了我怀疑可能需要稍后修复的相同简化假设。
您可以使用 Visual Studio 的任务列表来获取某些类型注释的列表。注释的格式可以在“工具|选项”、“环境\任务列表”中配置。这不是我自己最终使用的东西,但如果您经常使用这个系统,它看起来可能有助于浏览代码。
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.
如果你可以像这样拆分你的方法,你可能应该编写更多的方法。完成此操作后,您可以使用 IDE 为您提供初始方法的静态调用堆栈。
编辑:您可以在编程时使用 Eclipse 的“显示调用层次结构”功能。
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.