[Adium-devl] Early Returns and the Coding Style Docs

Evan Schoenberg evan.s at dreskin.net
Tue Mar 28 17:28:07 UTC 2006


On Mar 28, 2006, at 8:07 AM, Colin Barrett wrote:

>
> On Mar 27, 2006, at 10:46 PM, David Smith wrote:
>
>> 	What do people think of this? Am I in a minority here in liking
>> early returns? (I know I'm a minority in liking braces on their own
>> lines, I'm not even trying to argue that one anymore ;) )
>
> While it's true the second example has less indentation depth, I
> found the first example to be easier to follow on a cursory
> examination of the code.
>
> I don't have a problem with returning on the precondition, especially
> if it's got a nice comment ;). That makes perfect sense. What I'm
> objecting to is the use of continue. I think it makes it a bit more
> difficult to just quickly scan the code and determine what's going
> on. But that's just me.


I agree with David's initial assessment and Colin's response ;)  The  
continue is jarring.. but the precondition early return (basically a  
return_if_fail()) is clean and makes the subsequent code cleaner as  
well.
What do y'all think about this proposed guideline for multiple-returns:
------
In small methods, use a single return, as follows:
- (void)foo:(id)bar
{
	id theFoo;
	if (bar == muk) {
		theFoo = mukValue;
	} else if (bar == luk) {
		theFoo = lukValue;
	} else {
		theFoo = nil;
	}

	return theFoo;
}

In larger methods, preconditions such as (inValue != nil) should be  
handled with an early return if appropriate:
- (id)foo:(id)bar
{
  	id theFoo;

  	if (!bar) return nil;

	/* Code here */

  	return theFoo;
}

Valued returns in the middle of large blocks of code are discouraged,  
but use your judgement for what makes the code easiest for a newcomer  
to follow.
---------

-Evan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
URL: <http://adiumx.com/pipermail/devel_adium.im/attachments/20060328/349e3f3f/attachment.bin>


More information about the Adium-devl mailing list