Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing my singleton class using Project_Prefix.pch - Bad practice? [closed]

I use my DataManager singleton class in 99% of my project. Instead of importing it in every class I thought about just importing it inside the pch file like I do with my constants. Are there any disadvantages to this? Is it considered bad practice?

Thanks

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constants.h"
    #import "DataManager.h"
#endif
like image 942
Segev Avatar asked Oct 03 '22 13:10

Segev


2 Answers

The evaluation for good practice of #importing XYZ.h would be, over the life of the project,

NSTimeInterval saved = (time that would have been spent compiling XYZ.h plus every header it #imports in each file that included it every build);

NSTimeInterval wasted = (time spent recompiling files that do not include XYZ.h when XYZ.h changed);

if (saved > wasted) goodPractice = YES;

So any system #import, and very likely most of your library #imports, are excellent candidates. Anything else; well, if 95% of your code is recompiled anyway with that particular header, sure putting it in there makes sense. There's a good writeup here on the subject.

like image 74
Alex Curylo Avatar answered Oct 19 '22 00:10

Alex Curylo


Its not bad to include an import in the pch if the header doesn't change much (and also when the import is not used in most files which is not your case). Check the accepted answer for this question and decide-

Is there a reason for which one should not add all the imports inside .pch file?

like image 38
Rakesh Avatar answered Oct 19 '22 00:10

Rakesh