Continuing indentation

Carl Meyer carl at oddbird.net
Wed Mar 2 19:02:02 EST 2016


On 03/02/2016 04:54 PM, Chris Angelico wrote:
> On Thu, Mar 3, 2016 at 10:46 AM,  <codewizard at gmail.com> wrote:
>> On Wednesday, March 2, 2016 at 3:44:07 PM UTC-5, Skip Montanaro wrote:
>>>
>>>     if (some_condition and
>>>         some_other_condition and
>>>         some_final_condition):
>>>         play_bingo()
>>
>> How about:
>>
>>   continue_playing = (
>>       some_condition and
>>       some_other_condition and
>>       some_final_condition
>>   )
>>
>>   if continue_playing:
>>       play_bingo()
>>
>> or:
>>
>>   play_conditions = [
>>       some_condition,
>>       some_other_condition,
>>       some_final_condition,
>>   ]
>>
>>   if all(play_conditions):
>>       play_bingo()
> 
> Those feel like warping your code around the letter of the law,
> without really improving anything.

Not at all! Taking a series of boolean-joined conditions and giving the
combined condition a single name is often a major improvement in
readability. Not primarily for code-layout reasons, but because it
forces you to name the concept (e.g. "continue_playing" here.)

I often find that the best answer to "how do I wrap this long line?" is
"don't, instead extract a piece of it and give that its own name on its
own line(s)." The extracted piece might be a new variable or even a new
function. The pressure to do this type of refactor more frequently is
one reason I continue to prefer relatively short (80 char) line length
limits.

This is closely related to the XP guideline "when you're tempted to add
a comment, instead extract that bit of code into a function or variable
and give it a name that clarifies the same thing the comment would have."

Names are important!

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20160302/e365e275/attachment.sig>


More information about the Python-list mailing list