in reply to Re^2: Regex question
in thread Regex question

Well, I can't entirely agree. The code in my prior post is intended to be instructional, in the vein of "crawl before you walk; walk before you run." Given that, perhaps it should have been ("may be," if I get around to it) extensively commented.

  1. Your pseudo-code 2 doesn't match what amounts to a spec in Pseudo-code 1.
    • Pseudo-code 1 doesn't say anything about GOing to the store, so knock out lines 5 and 13
    • Pseudo-code 1 says, unambiguously, that what you want is
      1. 2% milk (no need to survey the types).
      2. the least expensive item satisfying the prior criterion. *
  2. Hence, there is no need for line 9, "compare prices to the types"
  3. Line 10 contradicts the spec; see my line 3 below.
  4. And to carry on with the absurdities, your lines 2, 11, and 12 are implicit in "buy"

* We can regard "gallon" as ambiguous, as that could mean "any combination of containers of 2% milk which aggregate to a gallon," but it might also mean that you have a specific reason for wanting the milk in a "one gallon container."

That leaves:

2: =~s/Select the type 2% where the prices is <= other prices./Buy the le +ast expensive 2% milk/;

Perhaps next time you go to the store to buy apples you should watch out for the oranges. Of course, I need to watch out too... for the absurdities to which my logic leads me.  ;-)

Replies are listed 'Best First'.
Re^4: Regex question
by Your Mother (Archbishop) on May 21, 2009 at 01:09 UTC

    Yeah, I understand and agree explicit code can be good sometimes but my pseudo-code was arbitrarily picked to show how the same exact task could be made more difficult to follow by being too verbose. There is no ambiguity in the AM's answer above and your version was harder to read and therefore more likely to disguise bugs and poor optimization/operator choices; which it has and it does compared to the original answer. Your solution drops anything other than ASCII alphanumerics and, bizarrely, commas; and regular expressions are a confusing and sub-optimal choice for single character looping and comparisons.

    Things like a next as the last instruction in a loop is explicit to the point of... well, it's so obvious, it becomes distracting noise. Overly verbose or simplistic becomes confusing (What? Why would anyone do that? I must be missing something, better read this again five times) or talking down. Talking down to other devs is a good way to make them slowly despise you.

    Put another way: elegant code lifts me up, makes algorithms obvious, condenses ideas into easily assimilated blocks, saves me time, and helps make me become a better hacker; simplistic code doesn't. I don't like golf in the code base more than anyone else but I sure as monkeys prefer a Schwartzian transform over 4 back to back for loops.

    (Update: I missed the rest of the thread earlier that already covered some of this. Sorry about that. I also think this reads harsher than I meant. I do agree that code can be made explicit to everyone's benefit; just wanted to say that explicitness for its own sake is self-defeating.)