Reputation: 5936
In the interest of cleaning up my code, is it good practice to use #define
for more than a few lines of code? Most examples are a few lines, defining dimensions etc.
In my example I have separate header file for the following piece of code. Is it good practice to use #define
this way?
#define KCHECKREFERENCE if([self.partClasses containsObject:@"Part1"]||[self.partClasses containsObject:@"Part2"]||[self.partClasses containsObject:@"Part3"]||[self.partClasses containsObject:@"Part4"]||[self.partClasses containsObject:@"Part5"]||[self.partClasses containsObject:@"Part6"]||[self.partClasses containsObject:@"ICDomesticEICPart7"]||[self.partClasses containsObject:@"ICDomesticEICPart8"] ){\
[self.currentPartView save];\
self.previousPartView = self.currentPartView;\
int nextPartNumber = 1;\
ICCertificateComponent *part = [self loadPart:nextPartNumber];\
self.currentPartView = part;\
CGRect nextPartViewFrame = self.currentPartView.view.frame;\
nextPartViewFrame.origin.x = 320.0f;\
self.currentPartView.view.frame = nextPartViewFrame;\
CGRect previousPartViewFrame = self.previousPartView.view.frame;\
previousPartViewFrame.origin.x = -320.0f;\
nextPartViewFrame.origin.x = 0;\
[self.view insertSubview:self.currentPartView.view belowSubview:self.navBarView];\
NSTimeInterval duration = 0.1;\
[UIView animateWithDuration:duration\
animations:^{\
self.previousPartView.view.frame = previousPartViewFrame;\
self.currentPartView.view.frame = nextPartViewFrame;\
}\
completion:^(BOOL finished) {\
self.currentPartNumber = 1;\
self.navBarView.prevButton.enabled = NO;\
self.navBarView.nextButton.enabled = YES;\
[self.previousPartView.view removeFromSuperview];\
self.previousPartView = nil;\
if (self.currentPartView.showsShareOptions == YES) {\
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStyleBordered target:self action:@selector(shareButtonPressed:)];\
}\
}\
];\
Upvotes: 0
Views: 114
Reputation: 4585
I know couple cases where define is good practice. For example _countof
macro
#if !defined(_countof)
#if !defined(__cplusplus)
#define _countof(_Array) (sizeof(_Array) / sizeof(0[_Array]))
#else
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
#endif
#endif
But yours KCHECKREFERENCE is evil. You should use inline
funcions instead. Why? At least because you can't set breakpoint inside yours macro.
Upvotes: 2
Reputation: 62052
No. And especially not for something so specific.
Rather than #define
, put this code in a method.
Upvotes: 5