我应该把许多函数放入一个文件?或者,或多或少,每个文件一个函数? [英] Should I put many functions into one file? Or, more or less, one function per file?

查看:163
本文介绍了我应该把许多函数放入一个文件?或者,或多或少,每个文件一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢整理我的代码,所以理想情况下,我需要每个文件一个类,或者当我有非成员函数,每个文件一个函数。

I love to organize my code, so ideally I want one class per file or, when I have non-member functions, one function per file.

是:


  1. 当我阅读代码时,我总是
    知道什么文件我应该找到
    某些功能或类。

  1. When I read the code I will always know in what file I should find a certain function or class.

如果每个头文件有一个类或一个非成员
函数,那么我不会
包含一个整体I
include 一个头文件。

If it's one class or one non-member function per header file, then I won't include a whole mess when I include a header file.

然而,将所有内容拆分成许多标题和许多实现文件慢慢地编译。在我的项目中,大多数函数访问一定数量的模板化的其他库函数。所以代码将被反复编译,每个实现文件一次。编译我的整个项目目前在一台机器上需要45分钟左右。有大约50个目标文件,每个使用相同昂贵的编译头。

However, splitting everything up into many header and many implementation files can considerately slow down compilation. In my project, most functions access a certain number of templated other library functions. So that code will be compiled over and over, once for each implementation file. Compiling my whole project currently takes 45 minutes or so on one machine. There are about 50 object files, and each one uses the same expensive-to-compile headers.

也许,每个文件有一个类(或非成员函数),但将多个或全部

Maybe, is it acceptable to have one class (or non-member function) per header file, but putting the implementations of many or all of these functions into one implementation file, like in the following example?

// foo.h
void foo(int n);

// bar.h
void bar(double d);

// foobar.cpp
#include <vector>
void foo(int n) { std::vector<int> v; ... }
void bar(double d) { std::vector<int> w; ... }

同样,优点是我可以只包括foo函数, bar函数,并且整个项目的编译将更快,因为 foobar.cpp 一个文件,因此 std: :vector< int> (这里只是一些其他昂贵的编译模板化结构的例子)只需要编译一次,而不是两次编译一个 foo.cpp bar.cpp 。当然,我的原因(3)对于这种情况是无效的:只是改变foo(){...}我必须重新编译整个,可能大的文件 foobar.cpp

Again, the advantage would be that I can include just the foo function or just the bar function, and compilation of the whole project will be faster because foobar.cpp is one file, so the std::vector<int> (which is just an example here for some other expensive-to-compile templated construction) has to be compiled in only once, as opposed to twice if I compiled a foo.cpp and bar.cpp separately. Of course, my reason (3) above is not valid for this scenario: After just changing foo(){...} I have to recompile the whole, potentially big, file foobar.cpp.

我很好奇你的意见。

推荐答案

,您应该将项目合并成逻辑分组,并根据这些分组创建文件。

IMHO, you should combine items into logical groupings and create your files based on that.

当我编写函数时,通常有六个或更多,彼此紧密相关。我倾向于把它们放在一个标题和实现文件中。

When I'm writing functions, there are often a half a dozen or so that are tightly related to each other. I tend to put them together in a single header and implementation file.

当我写类时,我通常限制自己每个头和实现文件一个重量级。我可能会添加一些方便的函数或微小的帮助类。

When I write classes, I usually limit myself to one heavyweight class per header and implementation file. I might add in some convenience functions or tiny helper classes.

如果我发现一个实现文件有成千上万行,这通常是一个迹象,我需要打破它。

If I find that an implementation file is thousands of lines long, that's usually a sign that there's too much there and I need to break it up.

这篇关于我应该把许多函数放入一个文件?或者,或多或少,每个文件一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆