[Chicago] Factoring fun with functions in Python

Garrett Smith g at rre.tt
Wed Nov 14 21:08:19 CET 2012


That's right - the blog post example is about translating statements
into functions to better elaborate intent, which hopefully makes the
program more obvious.

So lines count is indeed a better proxy for what I'm getting at.

But it's hard to get too nested when your functions average 1 - 2 LOC :)

On Wed, Nov 14, 2012 at 1:34 PM, Dan Krol <orblivion at gmail.com> wrote:
> I took that to mean something different:
>
> ----------
>
> Nested:
>
> for x in [1,2,3]:
>     for y in [4,5,6]:
>         print x, y
>
> Flatter:
>
> from itertools import product
>
> for x, y in product([1,2,3], [4,5,6]):
>     print x, y
>
> ------------
>
> This sort of "flatness" is inherently easier to read than the radical
> function splitting because it's sequential. It describes a narrative.
> Instead of "for each of these, then again for each of those, do this",
> which is harder to follow, you say "for each combination of these and
> those, do this".
>
> What you're eliminating with the splitting into functions isn't what
> would I thought was referred to as "nested". Your original code was
> only a little nested. You eliminated some nesting but you mostly
> eliminated length.
>
> Though maybe I've just been following a false Zen.
>
> On Wed, Nov 14, 2012 at 11:23 AM, Garrett Smith <g at rre.tt> wrote:
>> On Wed, Nov 14, 2012 at 12:24 PM, Dan Krol <orblivion at gmail.com> wrote:
>>> There must be something to be said for hierarchy.
>>
>> Indeed:
>>
>> python -c "import this" | grep nested
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago


More information about the Chicago mailing list