Tutorial :What would you consider to be called a “helper method”?



Question:

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!


Solution:1

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?


Solution:2

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.


Solution:3

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.


Solution:4

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.


Solution:5

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:]


Solution:6

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).


Solution:7

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".


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »