使用头文件有什么好处和坏处? [英] What are the benefits and drawbacks of using header files?

查看:15
本文介绍了使用头文件有什么好处和坏处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java、C#、Scala 等编程语言以及 C、C++、Objective - C 等低级编程语言有一定的经验.

I had some experience on programming languages like Java, C#, Scala as well as some lower level programming language like C, C++, Objective - C.

我的观察是低级语言尝试将头文件和实现文件分开,而其他高级编程语言从不将其分开.这些语言使用一些标识符,如 public、private、protected 来尝试完成头文件的工作.C++ 也有标识符和头文件

My observation is that low level languages try separate out header files and implementation files while other higher level programming language never separate it out. Those languages use some identifiers like public, private, protected to try to do the jobs of header files. C++ also have both identifiers and header files as well

我看到了使用头文件的一个好处(在像 Code Complete 这样的书籍中),他们谈到使用头文件,人们永远无法查看我们的实现文件,它有助于封装.

I saw one benefit of using header file (in some book like Code Complete), they talk about that using header files, people can never look at our implementation file and it helps with encapsulation.

一个缺点是它为我创建了太多文件.有时,它看起来很冗长.

A drawback is that it creates too many files for me. Sometimes, it looks like verbose.

这只是我的想法,我不知道人们曾经看到和使用头文件是否还有其他优点和缺点

It is just my thought and I don't know if there are any other benefits and drawbacks that people ever see and work with header file

这个问题可能与编程没有直接关系,但我认为如果我能更好地了解编程到接口,设计软件.

This question may not relate directly to programming but I think that if I can understand better about programming to interface, design software.

推荐答案

我不确定你问的是什么问题,所以我会尝试重新表述:

I'm not sure exactly what question you are asking, so I will try to rephrase it:

将公共信息放在单独的(标头或接口)文件中,而不是简单地将信息标记为公共或私有,无论它出现在哪里有什么好处?

What is the benefit of putting public information in a separate (header or interface) file, as opposed to simply marking information as public or private wherever it appears?

拥有单独的界面或头文件的主要好处是它减少了读者的认知负担.如果您想了解一个大型系统,您可以一次处理一个 implementation 文件,而您只需阅读其他 implementations/classes/的 interfaces它依赖的模块.这是一个主要的好处,不需要单独的接口文件(例如 Java)或什至不能在单独的文件中表达接口的语言(例如 Haskell)通常提供 Doxygen 或 Haddock 等工具,以便从实现中生成一个单独的接口,供人们阅读.

The main benefit of having a separate interface or header file is that it reduces the cognitive load on the reader. If you are a trying to understand a large system, you can tackle one implementation file at a time, and you need to read only the interfaces of the other implementations/classes/modules it depends on. This is a major benefit, and languages that do not require separate interface files (such as Java) or cannot even express interfaces in separate files (such as Haskell) often provide tools such as Doxygen or Haddock so that a separate interface, for people to read, is generated from the implementation.

我非常喜欢标准 ML、Objective Caml 和 Modula-2/3 等语言,其中有一个单独的接口文件可供审查.在 C 中拥有单独的头文件也很好,但不是很好,因为通常编译器无法独立检查头文件.(C++ 头文件不太好,因为它们允许私有信息,例如私有字段或内联方法的实现,泄漏到头文件中,因此公共信息被稀释了.)

I strongly prefer languages like Standard ML, Objective Caml, and Modula-2/3, where there is a separate interface file available for scrutiny. Having separate header files in C is also good, but not quite as good because in general, the header files cannot be checked independently by the compiler. (C++ header files are less good because they allow private information, such as private fields or the implementations of inline methods, to leak out into the header files, and so the public information becomes diluted.)

对于典型的静态类型语言来说,在语言设计界的民间传说中,模块中只有大约 10% 的信息是公开的(以代码行数衡量).通过将此信息放在单独的头文件中,您可以将阅读器的工作量减少大约十倍.

It's folklore in the language-design world that for typical statically typed languages, only about 10% of the information in a module is public (measured by lines of code). By putting this information in a separate header file, you reduce the reader's workload by roughly a factor of ten.

这篇关于使用头文件有什么好处和坏处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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