I'm using Objective-C for this question but this is not really language specific.
I have following method in my User class,
+(BOOL)canPerform:(NSString *)string
withCompletion:(void(^)(BOOL success,NSError *error))block;
In my ViewController
-(void)performTask{
if([User canPerform:@"My String" withCompletion:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"Task success!");
}
else{
NSLog(@"Task Failed with error : %@,error.localizedDescription");
}
}])
{
NSLog(@"Can perform task");
}
else{
NSLog(@"Can not perform task");
}
}
It's not necessary to mention what task I'm performing. That's not really my question.
My questions are:
Is this a good programming practice? Using a BOOL method that accepts a block, as the condition for an if statement ?
This method
[User canPerform]
does two things. First thing is, it checks if it can perform this specific task and if it can, it does perform that task. But method name does not reflect this. Method name iscanPeform
. I could useifCanThenPeformThisTask
but that sounds strange and doesn't feel like a BOOL method. What is the best approach to name this method?