I fairly recently (months ago) finally stumbled upon an indentation format that I actually liked for conditionals, instead of the many different ones that I'd used over the years that all didn't work well in some situations (didn't "scale" well). I've had the chance to use it for a large variety of situations and I like it even more.

It is mostly the same as what appears to be the most common style in this thread. The possible difference is when you have more complex expressions. I like spacing to make code easier to understand. I try to avoid complex conditional expressions, by breaking them up when possible. But, sometimes, a fairly complex expression makes sense (especially when you want a chain of elsifs, which makes splitting up a conditional more distruptive to the flow).

Here is a fairly worst-case example (based on actual code):

} elsif( StatusEvent::CONNECT_FAILED == $status->GetStateTransition() && CONNECT_TYPE_AUTO != $status->GetConnectType() || StatusEvent::DISCONNECTED == $status->GetStateTransition() && ( $IsAutoConnectDisabled || 0 == $statusNotAuto->GetTotalCount() ) && $statusNotAuto->GetTotalCount() == statusNotAuto->GetIdleCount() ) {

The indentation makes it easy to successively break down the expression. You can see the two main chunks of the expression separated by the left-most ||. Looking at each chunk, you see the two or three chunks separated by &&s. etc. Indentation corresponds to nesting level (as is done for most types of indentation).

You can treat the == operators the same as the logical operators and get this:

} elsif( StatusEvent::CONNECT_FAILED == $status->GetStateTransition() && CONNECT_TYPE_AUTO != $status->GetConnectType() || StatusEvent::DISCONNECTED == $status->GetStateTransition() && ( $IsAutoConnectDisabled || 0 == $statusNotAuto->GetTotalCount() ) && $statusNotAuto->GetTotalCount() == statusNotAuto->GetIdleCount() ) {

And how you cuddle the "}", "elsif(", ")", and "{" offers many WTDI that I see various trade-offs among; lots of room for personal style variations.

Anyway, you asked. (: Enjoy what style you pick (whether it includes parts of the above or not) and can get your cohorts to tolerate.

- tye        


In reply to Re: bloated 'if' formatting (nested) by tye
in thread bloated 'if' formatting by eff_i_g

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.