1

Good practice aside, I'm attempting to make coding just a tad easier for myself.

Is it possible to make a header file to handle myriad inclusions so that a source can just include the one header?

Edit: Some of you are referencing Cocoa. I forgot to mention C/C++ programming. It's in the tags, but I forgot to put it here.

Edit: Never mind. It seems I stepped on an anthill with this question. Lesson learned.

9
  • Sure, there are ways! But why give up modularity? Commented Mar 20, 2013 at 21:00
  • 1
    In general, it is a bad idea. It ends up with lots of code indirectly including stuff it doesn't need. Commented Mar 20, 2013 at 21:00
  • 3
    @Dolphiniac: You should be concerned about tests as much as you are about your project. Tests are what saves you from the fear of refactoring your code, and refactoring your code is what gives you the opportunity of keeping your code clean. Be careful about the design of your test as much as you are careful about the design of your program. Commented Mar 20, 2013 at 21:03
  • 1
    @Dolphiniac: OK, maybe I got the premise a bit wrong and rushed into writing a comment before carefully reading the "to practice something" part. In that case, well, you're not gonna deliver that to anybody, nor are you going to use that as a unit test for a program you are going to deliver, so... up to you. But why not keep practicing good principles even when you don't strictly have to? It will just make it more natural for you to follow them Commented Mar 20, 2013 at 21:06
  • 1
    Hmmmmmm I wonder if there is a tool which could simplify #include statements like this? Commented Mar 20, 2013 at 21:09

4 Answers 4

8

Sure it is possible, but I wouldn't call that a good programming style.

You're going to increase source code dependencies, you won't be able to recompile a part of your project without recompiling everything, and you will lose track of what depends on what.

Personally, I would say don't do it.

Sign up to request clarification or add additional context in comments.

3 Comments

Allow me to reword just a tad, then. Say I'm doing network programming. Small test cases, not projects. I want to make a header file to include the necessary headers so I'm not retyping in each test source. Honestly, I realize this is not good programming practice, but I'm asking how, not should I.
@Dolphiniac Copy & paste? A quick script and a template file?
@Dolphiniac: Yes, as I wrote in the comments to the question, tests are important but as long as they are "practicing tests", which are neither going to be deployed nor used to test anything which is going to be deployed, then I would say it's up to you. Do what fits your needs best. Still, I would try to follow the best programming practices even when I do not have to, just to get more acquainted with them and make them become natural (so I don't even have to lose time thinking about them and questioning them, and problem solved).
2

It's not worth it.

A significant amount of compiler time, CPU cycles and trees are spent processing #include directives. If you don't need it, don't bother loading it.

Comments

2

Sounds like you want a precompiled header. While it does have the disadvantage of you losing track of which source file depends on what other external code, precompiled headers themselves give significant compile speed boosts.

A good practice to mitigate the disadvantage I mentioned above is to avoid including your own headers in that precompiled header. That way at least you can make sure your own classes don't have weird dependencies on eachother.

Comments

1

I would say that it depends on what you want to achieve. If you develop a cocoa application for mac os x, the default template makes you include all their cocoa libraries through one header file:

<Cocoa/Cocoa.h>

which includes:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>

which include:

#include <CoreFoundation/CoreFoundation.h>

#import <Foundation/NSObjCRuntime.h>

#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSByteOrder.h>
#import <Foundation/NSCalendar.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSError.h>
#import <Foundation/NSException.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSFormatter.h>
#import <Foundation/NSHashTable.h>
#import <Foundation/NSHTTPCookie.h>
#import <Foundation/NSHTTPCookieStorage.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSNotificationQueue.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSNumberFormatter.h>
#import <Foundation/NSObject.h>

etc...

You end up including more than 200 headers, nested on (at least) 4 levels. They include al that through a .pch file (precompiled header). It is damn fast to compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.