Skip to content

Instantly share code, notes, and snippets.

@protocool
Created May 7, 2012 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save protocool/2628895 to your computer and use it in GitHub Desktop.
Save protocool/2628895 to your computer and use it in GitHub Desktop.
A question of taste, style, and naming
Had some great suggestions.
'set' came up a few times. It makes sense, "set the error ptrptr with your
last error". Unfortunately there's too much baggage with property setters
for that to work.
'propagate' was a good one too, but it also has a little bit of baggage,
at least for me, because propagate is often a word I use to describe the
larger concept of errors propagating up the stack. That's not the job of
this one little method.
'assign' was another one, and for a while, the main contender because, well,
that's what the method's job was.
However, thanks to bewebste and cocoadog (on twitter), I now see that there's
already an established convention for this sort of method.
The 'get' prefix.
I have a knee-jerk reaction to creating methods that start with 'get' only
because it hurts to inherit iOS code from Java programmers who use a 'get'
prefix for all of their accessors.
But the convention for 'get' that they are talking about is *nothing* like
a java accessor.
The thing you're asking to 'get' isn't returned, it's retrieved (or built,
which is important too) and put *into* the supplied destination arg.
So, you'd have:
- (NSError*)lastError;
- (void)getLastError:(NSError**)outError;
The contract for "fill in my error pointer" is everything from 'get' onwards.
So if it was appropriate you could extend the contract of the return value
to add some more meaning and sugar by returning YES/NO for getLastError: depending
on whether or not there was an error:
if ([self getLastError:outError) {
return;
}
So there you have it. 'get'.
Thanks very much for your time folks.
So I have a method like this (well, not quite like this, it's not an NSError** but
it's in the same spirit):
- (void)doSomethingWithFoo:(Foo*)foo error:(NSError**)outError;
But there's a few places where I might 'fill in' the out error which look like:
if (condition) {
if (outError) {
*outError = [self lastError];
}
return;
}
Blech.
Much nicer if I could do:
if (condition) {
[self lastError:outError];
return;
}
The method to do that is just:
- (void)lastError:(NSError**)outError
{
if (outError) {
*outError = [self lastError];
}
}
The problem I'm having is the verb for the method name.
I can't figure out what you'd call a method who's job is "fill in the outError
with whatever lastError you have".
Ideas?
@fcoury
Copy link

fcoury commented May 7, 2012

propagateError maybe?

@paulmederos
Copy link

fillInTheOutErrorWithWhateverLastErrorYouHave ? I try to be as descriptive as possible.

@bewebste
Copy link

bewebste commented May 7, 2012

I'd probably go with -getLastError:, to match methods like -[NSData getBytes:] or -[NSValue getValue:] which copy a return value out to a provided address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment