Interesting idea about throwing away the result of ++. I disagree...

But, why would we care?

I settled on this code because I believe it specifically addresses the large-scale operation we want, which is increment $flip each time, but reset it to zero when it gets to 4. As opposed to: set $flip according to the following formula...

Each time through, we want to increase the value of $flip by 1. The idiomatic way to say that is ++$flip.

What, fundamentally, is the difference between

$flip = ++$flip % 4
and
$flip = ($flip + 1) % 4
(except being three characters longer) when the entire point is to increase $flip by 1?

I guess my question is: why do you think I am throwing away a result which you only accomplish differently? "++" vs. "+ 1" -- they both add one to the value of $flip. See below for the readability benefits of using ++ to add one to a variable.

I like the shorter (by two characters, but hey...) version

($flip += 1) %= 4
but by your argument, you shouldn't like it, either, since it increments $flip, just to "throw the result of the += away." I don't understand your objection.
It is also much closer to being needlessly obfuscated, with all the obvious negatives... (though my coworkers might be shocked to hear that I don't prefer obfuscation :-)

I'm sure you considered, as I did:

for (1..12){ print "$Cheers[++$flip % 4]\n"; }
and rejected it because we want to emphasize the purpose of $flip, which is to cycle among the elements of the array. We assign to $flip to keep its value within the legal bounds. By this we make it obvious (at a glance) to the poor programmer who comes behind us that $flip will only hold values between (in this example) 0 and 3, because those are the only values we want.

I would argue that the preincrement adds a huge "obviousness" value for the aforementioned fellow programmer. ++$flip triggers an immediate (subconscious?) understanding of our intent. Similarly, <nobr>$flip = ++$flip % 4</nobr> is, I believe, immediately obvious in a way that <nobr>($flip += 1) %= 4</nobr> can never be. <nobr>($flip + 1)</nobr> is plain in its intent (make $flip increase by one each time), but not as plain as ++$flip.

Russ


In reply to RE(2): Flipper (Maintainability vs. personal style?) by Russ
in thread Flipper by Russ

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.