views:

369

answers:

7

I am wondering what kind of methods are "helper methods". Where do we have to draw te border to say that an specific method is a "helper method"? Actually, I consider any method to be a "helper method" if it's not an initialization method, a method implemented for conforming to an protocol or delegation response methods. Any other method I implement to perform anything is a "helper method" in my perspective, but I feel that there are more categories to think of.

Why I want this to know? Because I want to make mor use of the #pragma mark HEADLINE stuff in Xcode. I beliefe it's good to organize the code in some way.

Please help me to get my picture of an "helper method" clear. Examples appreciated!

+3  A: 

I look at it as Methods that aren't required for the proper functioning of library, but make things easier or more readable for the end user.

Hmmm... didn't give an example. Lets say you have a function to get users from a database, and it took the form of

User GetUsers(UserRole)
{
//Do Something
}

And example helper method might be

User GetAdmins()
{
   return GetUsers("Admins");
}

Its a crude pseudo code type example, but gives you the gist. The functionality of GetAdmins is complete in GetUsers(), but the GetAdmins helper function helps readability ( and nothing more ).

Make sense?

Serapth
"not required" sounds good...
Thanks
Actually, I would call your GetAdmins() function a (convenience) wrapper.
Tom
True, although wouldn't you consider a conveinence wrapper a type of helper method? I was going to use an example of calculating the distance between two points, but that could just as easily be considered a core method. It really is all rather subjective in the end. I think the key ( in either case ), is that it is not a core requirement.
Serapth
+1  A: 

I'd say a method that is specific to a particular class but does not rely on access to private implementation details of that class is a helper method for that class. Helper methods should not be stateful either.

Frank Schwieterman
+1  A: 

I think of a helper method as one that has only one caller. It could be inlined, but because its code is external to the caller, the caller is easier to understand, with less innate complexity.

Carl Manaster
And why helper method could be used by more than one caller? There are cases there you extracting some shared logic to serve several methods, also very used for testing purpose.
Artem Barger
That's a fine thing, too. But to me, that method isn't a helper method; it's an encapsulation of common functionality. The question is about what "helper" means, and to me, it means the method has little reason to exist beyond simplifying the called method; once a method has two callers, it has a much more important reason.
Carl Manaster
+1  A: 

I think you question is context dependent. In general you could say, as you stated, that every method could be a helper method in certain application. But I believe in most of the cases helper methods is more about dividing some general method in several more smaller one to provide more clear picture and good code.

Artem Barger
+1  A: 

Generally I define helper methods as methods that make a class easier to use, but provide no new behaviours that can't be implemented by calling other more complicated methods.

Examples:

  • +[NSObject new] is a helper method for [[NSObject alloc] init];
  • NSClassFromString(str) is a helper method for objc_getClass([str UTF8String]);
  • +[NSURL URLWithString:str] is a helper method for [[[NSURL alloc] initWithString:str] autorelease];
  • -[NSURLRequest initWithRequest:request delegate:delegate] is a helper method for [request initWithRequest:request delegate:delegate startImmediately:NO]
  • -[NSString substringFromIndex:] is a helper method for -[NSString substringWithRange:]
rpetrich
+1  A: 

I'd chime in with something that provides some general utility, rather than a shim around a more complex function or something to do with implementation specifics. For example, a helper method for me would be something like a method on NSString which returned an NSDate directly, rather than requiring you to use NSDateFormatters all over the place.

In this respect, I'd categorize helper methods as either something to help you DRY-up your code by wrapping some frequently-used sequence into a single function, or something which you write to wrap up some chunk of awkward code from another API (yes, these two are usually the same thing).

At the risk of looking like I'm trying to pimp my own code here, I'll point you at the Extensions directory of my toolkit repository, which contains a whole bunch of stuff I wrote as 'helper methods' on standard Objective-C classes. Case in point: +(NSError *)errorWithCFStreamError:(CFStreamError)streamError, which is a 'helper method' for converting an old-style CFStreamError value to a proper NSError/CFErrorRef object using the new values exported in OS X 10.5 (and iPhone).

Jim Dovey
+1  A: 

On Objective-C, I think you could call any private method a "helper method", where by private I mean that the method signature doesn't appear in the .h file. I.e. these are methods you'd likely declare in a class continuation in the .m file:

@interface MyClass ()
- (void)somePrivateMethod;
@end

What some other answers are calling helper methods -- optional but public methods that are not crucial to the API -- I would rather call "convenience methods".

Daniel Dickison