将所有项目头文件放入一个文件HEADERS.h是一个好主意吗? [英] Is it a good idea to put all project headers into one file HEADERS.h?

查看:253
本文介绍了将所有项目头文件放入一个文件HEADERS.h是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我跟我的老师谈过,问他这个问题。他告诉我可以参加小型项目,但我正在开始一个国际象棋程序,我想知道Stack Overflow是怎么看待这个问题的。我应该将所有头文件包含到一个文件中,还是将它们分开?

解决方案

通常,您需要单独的头文件。
$ b


    $ b

    包括超过必要的一些潜在的坏事情。
    $ b


    1. 编译时间较慢的原因。不必要的包含额外的头文件会减慢编译速度,因为每个源文件都需要担心比需要更多的信息。它始于一个小问题,并且在你知道它之前,数百名开发人员每个人都在浪费几十到几百个小时,因为这个问题太过无法解决。即使您正在处理小问题,理解头文件的正确分离也很重要 - 首次正确时很容易,但如果您忽略它,很难在稍后修复。


    2. 你失去了正确处理依赖关系的能力(取决于编译器/ IDE),并且构建了比你需要构建的更多信息。

    3. 你失去了可维护性。当您拥有大型的单一文件时,维护软件比当您拥有大量简洁的小文件时更难。 你让代码变得更加困难理解,因此更容易出现错误。通过将所有内容都放在一个文件中,在巨大的头部中更容易丢失。


    I talked to my instructor the other day and asked him this question. He told me that I could go for smaller projects, but I'm starting a chess program and I was wondering what Stack Overflow thinks about this issue. Should I include all headers into one file, or separate them?

    解决方案

    Normally, you want separate headers.

    Including more than necessary does a few potentially bad things.

    1. This is single greatest cause of slow compile times. Unnecessary inclusion of extra headers slows down compilation, since each source file has to worry about more info than it needs. It starts as a small problem, and before you know it hundreds of developers are wasting dozens to hundreds of hours each because the problem got too far out of hand to fix. Even though you're working on small problems, it's important to understand proper separation of headers - it's easy to get right the first time but very hard to fix later if you ignore it.

    2. You lose the ability (depending on compiler/IDE) to correctly handle dependencies, and wind up building more information than you need to build.

    3. You lose maintainability. It's more difficult to maintain software when you have large, single files, than when you have lots of small, concise files.

    4. You make the code more difficult to understand, hence more prone to bugs. By having everything in one file, it's easier to get "lost" in the huge header.

    这篇关于将所有项目头文件放入一个文件HEADERS.h是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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