在代码中使用目录?

发布于 2024-08-25 04:49:48 字数 1451 浏览 1 评论 0原文

您是否使用目录在大源代码文件的开头列出类的所有函数(可能还有变量)?我知道这种列表的替代方法是将大文件分割成较小的类/文件,以便它们的类声明足够不言自明......但是一些复杂的任务需要大量代码。我不确定花时间将实现细分为多个文件是否真的值得?或者可以在类/接口声明之外创建索引列表吗?

编辑:

为了更好地说明我如何使用目录,这是我的爱好项目中的一个示例。它实际上不是列出函数,而是函数内部的代码块..但是您可能无论如何都能明白这个想法..

/*
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦年海沫深 2024-09-01 04:49:48

我知道这种列表的替代方法是将大文件分成较小的类/文件,以便它们的类声明足够不言自明。

正确的。

但是一些复杂的任务需要大量代码

不正确。虽然需要“大量”代码,但长时间运行代码(超过 25 行)确实是一个坏主意。

实际上不是列出函数,而是函数内的代码块

更糟糕的是。需要目录的函数必须分解为更小的函数。

我不确定花时间将实现细分为多个文件是否真的值得?

将内容分割成更小的文件是绝对必要的。维护、调整和重用您的代码的人员需要他们可以获得的所有帮助。

除了类/接口声明之外还可以创建索引列表吗?

不行,

如果非要使出这种招数,那就太大了。

此外,许多语言都有从代码生成 API 文档的工具。 Java、Python、C、C++都有文档工具。即使使用 Javadoc、epydoc 或 Doxygen,您仍然必须进行设计,以便将它们分解为可智能管理的部分。

  1. 让事情变得更简单。

  2. 使用工具创建索引。

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.

你的心境我的脸 2024-09-01 04:49:48

如果您创建一个大索引,则必须在更改代码时维护它。无论如何,大多数现代 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.

没有心的人 2024-09-01 04:49:48

我永远不会在我的代码中做这种忙碌的工作。我手动做的最多的事情就是在文件/类的顶部插入几行,解释这个模块的作用以及它的用途。

如果方法及其接口的列表有用,我会通过 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.

蓝色星空 2024-09-01 04:49:48

我曾经做过这样的事情。不是完整的目录,而是类似的原则——只是注释和相关代码之间的临时链接。还要链接一些代码片段,这些代码片段做出了我怀疑可能需要稍后修复的相同简化假设。

您可以使用 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.

烟燃烟灭 2024-09-01 04:49:48

如果你可以像这样拆分你的方法,你可能应该编写更多的方法。完成此操作后,您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文