<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>t+1 - Latest Comments in Which code layout do you like more?</title><link>http://tplus1.disqus.com/</link><description></description><atom:link href="https://tplus1.disqus.com/which_code_layout_do_you_like_more/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 27 Jul 2009 13:03:58 -0000</lastBuildDate><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-13404198</link><description>&lt;p&gt;return ( b ? h(g(a)) : g(a))&lt;/p&gt;&lt;p&gt;... assuming ternary operators...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">miles</dc:creator><pubDate>Mon, 27 Jul 2009 13:03:58 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11722788</link><description>&lt;p&gt;Shouldn't the function name be "price" instead? Why introduce a variable just for  naming when the function name serves that purpose?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Brandon Corfman</dc:creator><pubDate>Thu, 25 Jun 2009 10:03:25 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11490909</link><description>&lt;p&gt;With multiple return points I find myself spelling out implicit else statement:&lt;/p&gt;&lt;p&gt;def(a,b):&lt;br&gt;__if b:&lt;br&gt;____return h(g(a))&lt;br&gt;__else:&lt;br&gt;____return g(a)&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Michael</dc:creator><pubDate>Fri, 19 Jun 2009 22:17:14 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11485018</link><description>&lt;p&gt;I'd prefer the first one, easy to read, easy to understand.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Guest</dc:creator><pubDate>Fri, 19 Jun 2009 18:08:03 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11455580</link><description>&lt;p&gt;I on the other hand love multiple return statements in loops and --while using php-- switches. I actually find that situation a lot using switches.&lt;/p&gt;&lt;p&gt;In some switches I don't use break at all since all my cases end with return. Some times however I have to do additional work before getting out of the function, then it's temp variable time again.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">rgz</dc:creator><pubDate>Fri, 19 Jun 2009 13:08:07 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11454997</link><description>&lt;p&gt;For me, it depends on the rest of the code in the function.  Multiple obvious return statements aren't "bad":&lt;/p&gt;&lt;p&gt;def f(...):&lt;br&gt;   if param is bad:&lt;br&gt;      return None&lt;/p&gt;&lt;p&gt;  ... do stuff ...&lt;br&gt;  return answer&lt;/p&gt;&lt;p&gt;but I don't like having returns sprinkled throughout the code, and especially not in loops, where it can complicate the logic of the execution flow.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Titus Brown</dc:creator><pubDate>Fri, 19 Jun 2009 12:54:47 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11441346</link><description>&lt;p&gt;the indentation disappeared. Using _ as space:&lt;br&gt;def(a,b):&lt;br&gt;__if b:&lt;br&gt;____return h(g(a))&lt;br&gt;__return g(a)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Bård Aase</dc:creator><pubDate>Fri, 19 Jun 2009 07:04:54 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11441325</link><description>&lt;p&gt;How about&lt;br&gt;def(a,b):&lt;br&gt;    if b:&lt;br&gt;        return h(g(a))&lt;br&gt;    return g(a)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Bård Aase</dc:creator><pubDate>Fri, 19 Jun 2009 07:03:52 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11441290</link><description>&lt;p&gt;I first learnt to program in the early 1990s using books, and thus methodologies, that were already old and discarded by their previous owners. All I really remember was the love of flowcharts and subtle re-enforcement of the idea that multiple return statements made code so hard to read that anyone else who saw it would meet an untimely end.&lt;/p&gt;&lt;p&gt;I think modern teaching styles have moved towards a more understanding based approach that credits the student with a little intelligence. Consider the following fragments of introductory programming:&lt;/p&gt;&lt;p&gt;* All functions must return a value&lt;br&gt;* No code after a return is executed&lt;br&gt;* a function can only return one value&lt;/p&gt;&lt;p&gt;Nice nuggets of information that can be memorised by rote. So very early on one gets into the habit of putting the return at the end of a function because it has to be there and thinking nothing more of it. It took a long time for me to unlearn that habit.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Charles</dc:creator><pubDate>Fri, 19 Jun 2009 07:01:23 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11441045</link><description>&lt;p&gt;hmmm&lt;/p&gt;&lt;p&gt;def f(a, b):&lt;br&gt;    temp = g(a)&lt;br&gt;    if b:&lt;br&gt;        return h(temp)&lt;br&gt;    return temp&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Martin</dc:creator><pubDate>Fri, 19 Jun 2009 06:39:36 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11440857</link><description>&lt;p&gt;def f(a, b):&lt;br&gt;  if b:&lt;br&gt;    return h(g(a)&lt;br&gt;  return g(a)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">^love *encounter ~flow</dc:creator><pubDate>Fri, 19 Jun 2009 06:23:11 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11386799</link><description>&lt;p&gt;Absolutely... I try to do a single return where it makes no difference otherwise, but multiple returns where it makes the function that much clearer. Where I do multiple returns, I try to put a block of early return conditions together at the start of the function, then the body, then a single 'real' return value (if possible)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tennessee Leeuwenburg</dc:creator><pubDate>Fri, 19 Jun 2009 00:39:35 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11386700</link><description>&lt;p&gt;If you follow that too strictly, you can end up with huge, unwieldy blocks of conditionals. Suppose you have a function where you want to "fail fast". Well, a single return point means you need to create a huge if/else. This is HUGE, and new programmers fall into this trap all the time.&lt;/p&gt;&lt;p&gt;With multiple returns, we can do:&lt;/p&gt;&lt;p&gt;def launch_rocket(mode):&lt;br&gt;   if mode == "test":&lt;br&gt;       return True&lt;br&gt;   #100 lines of launch code&lt;/p&gt;&lt;p&gt;With only one return, we get a 100-line conditional block, not counting any other block within it:&lt;/p&gt;&lt;p&gt;def launch_rocket(mode):&lt;br&gt;    success = None&lt;br&gt;    if mode == "test":&lt;br&gt;       return True&lt;br&gt;    else:&lt;br&gt;        #100 lines of launch code&lt;br&gt;        return result&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Aaron Oliver</dc:creator><pubDate>Fri, 19 Jun 2009 00:35:39 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11386250</link><description>&lt;p&gt;They are not the same, I mean, in this case they are but only because the functionality of g(x) is external to f(a, b). In fact f(a, b) is pretty much a wrapper for calling g(x) given a and b.&lt;/p&gt;&lt;p&gt; Suppose you are writing new code, g(x) is not defined yet and once it is defined it will only be called from f(a, b), in fact g(x) is only being defined to be wrapped by f(a, b), g(x) is a helper function.&lt;/p&gt;&lt;p&gt;In production, these functions will have a longer names as fubar_bloops(a, b) and fubar_bloops_helper(x) or something similar.&lt;/p&gt;&lt;p&gt;This is ugly. Instead you should write g(x) inside f(a, b) because that is what you really mean, using that approach you end up with the first layout.&lt;/p&gt;&lt;p&gt;Aaaaaaanyway, in this context I'd use *this* layout:&lt;/p&gt;&lt;p&gt;def f(a, b):&lt;br&gt;    return h(g(a)) if b else g(a)&lt;/p&gt;&lt;p&gt;or just&lt;br&gt;f = lambda a, b: h(g(a)) if b else g(a)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">rgz</dc:creator><pubDate>Fri, 19 Jun 2009 00:18:37 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11385829</link><description>&lt;p&gt;The first approach can make for easier debugging, since you can then step through your method watching where your return value is updated, then easily override it through the debugger if you need to. It also means the function *always* returns on the same line number, so you can put a break point in immediately before it and catch the function immediately before returning. If means you can do things like&lt;/p&gt;&lt;p&gt;bp 1043, retVal=None&lt;/p&gt;&lt;p&gt;and let the debugger catch the time through the code you are returning None.&lt;/p&gt;&lt;p&gt;Cheers,&lt;br&gt;-Tennessee&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tennessee Leeuwenburg</dc:creator><pubDate>Thu, 18 Jun 2009 23:59:01 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11215878</link><description>&lt;p&gt;this one wins&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">tv</dc:creator><pubDate>Thu, 18 Jun 2009 22:03:26 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11194913</link><description>&lt;p&gt;I can use both, depending on the context. The difference is in the intent. Is the call to g() essential to executing f() or is it coincidentally needed for both conditions?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Calvin Spealman</dc:creator><pubDate>Thu, 18 Jun 2009 21:47:31 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11135624</link><description>&lt;p&gt;I'm not consistent with these things. Why would you be? I go by what is more aesthetically pleasing, in your case (which by the way says nothing about mutability?) I'd go for two returns because it's more succinct somehow.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">LE</dc:creator><pubDate>Thu, 18 Jun 2009 18:30:30 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11134128</link><description>&lt;p&gt;This.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</dc:creator><pubDate>Thu, 18 Jun 2009 17:48:45 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11131114</link><description>&lt;pre&gt;def to_int_or_list(fpnumber, as_list=True):&lt;br&gt;    return list(int(fpnumber)) if as_list else int(fpnumber)&lt;br&gt; &lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;if the funcction was doing something more obscure, then I might employ a temporary variable and use one exit point or two - whatever seemed more natural.&lt;/p&gt;&lt;p&gt;- Paddy.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Paddy3118</dc:creator><pubDate>Thu, 18 Jun 2009 16:37:09 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11130857</link><description>&lt;p&gt;I prefer the first. However, if its a simple 5 line function then the second. Larger functions then I try to keep one return point...&lt;/p&gt;&lt;p&gt;If you end up with more than 2 return points it gets ugly and confusing.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Dougal Matthews</dc:creator><pubDate>Thu, 18 Jun 2009 16:34:29 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11129684</link><description>&lt;p&gt;Returning in the middle of a function is bad.  It is like using goto.  It violates D-diagramm conformance (you can find out more about this by using Google).  Using a variable has the additional benefit that it can have a meaningful name which makes it more clear *what* the function returns -- for example price = ... And the return price.  temp in your example is a bad name anyway.&lt;/p&gt;&lt;p&gt;In you case you can use Pythons relatively new ternary operator to have both advantages: Only one assignment and only one return statement at the end of the function:&lt;/p&gt;&lt;p&gt;def f(a, b):&lt;br&gt;    price = h(g(a) if b else g(a)&lt;br&gt;    return price&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Markus Gritsch</dc:creator><pubDate>Thu, 18 Jun 2009 16:17:45 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11129304</link><description>&lt;p&gt;Well shoot. Ignore the second part of my comment then.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Aaron Oliver</dc:creator><pubDate>Thu, 18 Jun 2009 16:12:03 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11129010</link><description>&lt;p&gt;The only reason I don't like the second one is that it packs too much functionality on a single line. I don't care about preferences for a "single return". But I don't like how g(a) is called from two different places in the second version. Imagine adding logging or exception handling to this function. Which version is more modifiable?&lt;/p&gt;&lt;p&gt;I prefer dgou's code.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Shinners</dc:creator><pubDate>Thu, 18 Jun 2009 16:06:07 -0000</pubDate></item><item><title>Re: Which code layout do you like more?</title><link>http://blog.tplus1.com/blog/2009/06/18/which-code-layout-do-you-like-more/#comment-11122109</link><description>&lt;p&gt;return h(g(a)) if b else g(a)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Brandon Corfman</dc:creator><pubDate>Thu, 18 Jun 2009 15:49:46 -0000</pubDate></item></channel></rss>