On Tue, 10 Feb 2009 10:54:52 -0600, Ron Adam wrote:
> Lie Ryan wrote:
>> On Thu, 05 Feb 2009 12:07:39 -0500, Scott Dial wrote:
>>> personally, I like this:
>>>> if some() and some_other() or \
>> some_more(complex=(True,)) and \
>> a_final_call(egg=(1,2,3)) \
>> :
>> do_something()
>>> My preference is to lead with keywords or symbols when it's convenient:
>> if (some()
> and some_other()
> or some_more(complex=(True,))
> and a_final_call(egg=(1, 2, 3))):
> do_something()
>>> For long math expressions spanning multiple lines I usually split before
> addition or subtraction signs.
>> if (some_long_value * another_long_value
> + some_long_value * a_special_quanity -
> an_offset_of_some_size):
> do_something()
>>> With syntax highlighting these becomes even more readable. The eye and
> mind just follow along the vertical line of highlighted keywords or
> operations until it reaches the end.
>> As far as optional things go, I'd like the '\' to be optional for
> multiple lines that end with a ':'. Adding parentheses around the
> expression works, but it seems like a compromise to me.
>> Cheers,
> Ron
Under ideal circumstances, when I see codes like that I'll turn it into a
function. Such a complex expression decreases readability exponentially,
turning it into a function would make it much more readable. Worse is if
the expression uses a lot of parentheses, it becomes a nightmare trying
to count the parentheses, keep it balanced, and keep it logically correct.
r = 1.0 / (
(n**p * p**n)**(n*p) +
l**w
)
where
r = readability
n = number of terms
p = number of parentheses used to change evaluation order
l = number of lines
w = average width of each line
n = 4, l = 4, p = 3, w = 20
the readability of that expression is:
r = 2.6547283374476086e-45