Are unused includes harmful in C/C++?

后端 未结 10 535
说谎
说谎 2020-12-23 14:27

What are the negative consequences of unused includes?

I\'m aware they result in increased binary size (or do they?), anything else?

相关标签:
10条回答
  • 2020-12-23 15:10

    They represent clumsy design.

    If you are not sure what to include and what not to include, it shows the developer had no idea what he was doing.

    Include files are meant to be included only when the are need. It may not be that much of issue as the computer memory and speed is growing by leaps and bounds these days but it was once perhaps.

    If an include is not needed but included anyhow, I would recommend to put a comment next to it saying why you included it. If a new developers get on to your code, he will have much appreciation for you, if you have done it the right way.

    0 讨论(0)
  • 2020-12-23 15:12

    They don't necessarily increase binary size, but will increase compile time.

    0 讨论(0)
  • 2020-12-23 15:13

    Yes, they can increase binary size because of extern unused variables.

    //---- in unused includes ----
    extern int /* or a big class */ unused_var;
    
    //---- in third party library ----
    int unused_var = 13;
    
    0 讨论(0)
  • 2020-12-23 15:15

    The main problem is clutter. These are the three main aspects in which the clutter manifests:

    1. Visual pollution; while you are trying to figure other includes that you do need.

    2. Logical pollution; it is more likely to have collision of functions, more time to compile (it might be really small for a couple of includes, but if it becomes a "policy" to not cleaning up unneeded includes, it might become a significant hurdle).

    3. Dependency opacity; since there are more headers to analyze is it harder to determine the dependency cycles in your code. Knowing what are the dependencies in your code is crucial when your codebase grows to any significant level beyond the hobbyist level.

    0 讨论(0)
提交回复
热议问题