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

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

问题描述

我在Java,C#,Scala等编程语言以及C,C ++,Objective - C等一些低级编程语言方面有过一些经验。我的观察是低级语言尝试分离出头文件和实现文件,而其他更高级别的编程语言从不分离它。这些语言使用一些标识符,如public,private,protected来尝试执行头文件的作业。 C ++也同时拥有标识符和头文件。我看到了使用头文件的一个好处(在Code Complete这样的书中),他们谈论了使用头文件,人们永远不会看我们的实现文件,它有助于封装。



缺点是它为我创建了太多的文件。有时,它看起来像冗长的。



这只是我的想法,我不知道是否有任何其他的好处和缺点,人们看到和使用头文件



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


将公共信息放入单独的(标题或界面)文件中有什么好处,而不是仅仅将信息标记为公共或私人文件?


具有单独的接口或头文件的主要好处是它可以减少阅读器的认知负载。如果您试图了解一个大型系统,您可以一次解决一个 implementation 文件,并且您只需要读取其他实现/类/接口的接口它依赖的模块。这是一个主要的好处,不需要单独的接口文件(如Java)或甚至不能在单独的文件(如Haskell)中表达接口的语言通常提供诸如Doxygen或Haddock之类的工具,因此一个单独的接口供人阅读,它是从实现中生成的。



我非常喜欢Standard ML,Objective Caml和Modula-2/3等语言,其中是一个单独的界面文件供审查。在C中使用单独的头文件也很好,但不是很好,因为一般情况下,头文件不能由编译器独立检查。 (C ++头文件不太好,因为它们允许私有信息(例如私有字段或内联方法的实现)泄漏到头文件中,因此公共信息变得稀释。)

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


I had some experience on programming languages like Java, C#, Scala as well as some lower level programming language like C, C++, Objective - 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

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?

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.

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.)

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天全站免登陆