public
Created

A question of taste, style, and naming

  • Download Gist
And the results are...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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.
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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?

propagateError maybe?

fillInTheOutErrorWithWhateverLastErrorYouHave ? I try to be as descriptive as possible.

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.