Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: logical non-short-circuit operators

by moritz (Cardinal)
on Nov 02, 2011 at 18:34 UTC ( #935465=note: print w/replies, xml ) Need Help??

in reply to logical non-short-circuit operators

my $num = $recFono->field_as_text('102'); my $type = $recFono->field_as_text('104'); if ($num && $type) { something(); }

This has the benefits of

  • Letting the assignments standing out clearly
  • Shortening the individual lines, making them easier to read
  • Clearly conveys that you want the arguments of the logical operator evaluated independent of truth/falsehood of the first one

If you really want it on one line, you can write your own logical function:

sub myor { $_[0] or $_[1]; } if (myor( ..., ...)) { }

Replies are listed 'Best First'.
Re^2: logical non-short-circuit operators
by RMGir (Prior) on Nov 02, 2011 at 18:37 UTC
    ++, looks good.

    But I think you mean

    if ($num || $type) { # (|| rather than &&) something(); }

Re^2: logical non-short-circuit operators
by december (Pilgrim) on Nov 02, 2011 at 18:55 UTC

    You're right, of course. The reason I avoided the former approach was because I have a lot of similar statements and I was hoping to keep the assignments in the scope of the conditional so as not to pollute the namespace or by accident reuse an old variable.

      If you have many similar statements, maybe you could refactor them to use a table for the conditions and a loop (or so), or in some other way. Code duplication or near-duplication is usually a sign that you can do it better somehow.

      If you want to avoid the variables leaking out, you can just put another block around the whole thing (variable declarations and if block).

Re^2: logical non-short-circuit operators
by sundialsvc4 (Abbot) on Nov 03, 2011 at 14:18 UTC

    I strongly agree.   When I read this code, it is immediately obvious that “the designer’s intent” is that both function calls will be and should always be executed.

    Years ago, the manufacturers of a debugging tool would publish in Dr. Dobb’s Journal a short excerpt of “C” code with the challenge to figure out what the code actually did.   My impression was always, “gee, I can’t afford to use a language like that encounter code written like that.”   (Note that I’m not talking about the debugger tool that was being advertised.)   Code has to be clear and obvious above all other things, such that anyone who might be reading the unfamiliar code (perhaps under conditions of pressure and great haste) can instantly and correctly understand it.   The compiler can take care of itself.   (A tiny scrap of code, written by persons unknown, a mere four lines long but buried out of sight, once cost me $10,000 in real money.)

      Code has to be clear and obvious above all other things, such that anyone who might be reading the unfamiliar code (perhaps under conditions of pressure and great haste) can instantly and correctly understand it.

      Let' s posit a few equivalent scenarios.

      1. Every legal document has to be clear and obvious so that any newly qualified lawyer that just barely scraped through law school can immediately apply it to every possible situation to which it might be applicable.
      2. Every music score should be so simple and obvious that any school kid with a tin whistle should be able to render it with same fidelity as a full orchestra.
      3. Every engineering blueprint should be so clear and obvious that any half-competent DIY enthusiast with a Swiss Army knife and a Leatherman will be able to produce the component over a weekend.
      4. Every electronics schematic should be so clear and obvious that anyone with a soldering iron can knock one up in a couple of hours.
      5. Every annotation of the Human Genome should be so clear and obvious that any kid with a Junior Chemistry kit can whip themselves up a little brother or sister once they've finished their homework.

      Just because the world and his dog can pick up a copy of "Sams Teach Yourself HTML 4 in 24 Hours" and put together a web page with lurid fonts, pretty pictures, a hit counter and bunch of social site click buttons in a weekend, it doesn't mean that professional programmers should dumb down their code in the vain hope that: anyone who might be reading the unfamiliar code (perhaps under conditions of pressure and great haste) can instantly and correctly understand it."

      Programming is one of the most complex endeavours man undertakes. Doing it well requires knowledge of such a range of diverse and ever changing algorithms and disciplines; and it can be applied to a huge range of problems. The very expectation that even highly competent and experienced programmers coming from different disciplines or backgrounds should be able to instantly and correctly understand another's programs is ludicrous.

      It is like suggesting that an aroma therapist should be able to take over from a heart specialist or a brain surgeon at a moments notice.

      In no other professional discipline do its exponents make such claims. When will programmers stop devaluing the skill, expertise and learning required to perform their trade by making such puerile and facile claims?

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935465]
[stevieb]: I find the M::S makefiles it generates are quite straight forward, and I usually have to add a few things (github info etc). They're about 15 lines or so give or take.
[Corion]: I don't think the EUMM-generated Makefile is that complicated ;)
[stevieb]: Corion++
[jedikaiti]: RonW++ #I have so much accumulated in my memory that finding the right item at a given moment often fails
[LanX]: corion only > 800 lines
[stevieb]: ahhh you're talking about the actual Makefile, not the PP Makefile.PL
[Corion]: LanX: But most of that is just setup of variables to be used later, like CC , LIB etc.
[LanX]: yes and some builders try to avoid it

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-08-18 21:02 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.