Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Should 'use warnings' check for contradictions?

by SuperCruncher (Pilgrim)
on Jun 09, 2002 at 13:55 UTC ( #172909=perlmeditation: print w/replies, xml ) Need Help??

Recently I was writing a program to send me text messages when specific teams scored goals in the World Cup. Thanks to several monks who have already help me out with crontab problems :-)

Anyway, looking at some of the source code I noticed a very subtle bug. The program works as follows:

  • Compare current results with results from program's last invocation
  • If the number of goals scored by either team is not the same, there must have been a goal
My program contained the code
if ( ($team1_score ne $team1_scorec) or ($team2_score ne $team2_score) + ) { $changed_matches{$team1} = $changed_matches{$team2} = "$team1 vs $team2 ($team1_scorec - $team2_scorec)"; }
Obviously this code contains an error. In the second 'clause' of the 'if' statement, I'm comparing a variable to itself. I meant to write $team2_scorec instead.

So surely the expression $team2_score ne $team2_score is a contradiction--it will never evaluate as true.

So, wouldn't it be good if Perl could warn about this? It would have saved me a lot of trouble.

I suppose it might be possible that a thread could access the variable and change it, but I would imagine that the ne operator would be implemented in a sort of 'atomic' fashion.

I'd be interested to hear your views on this. Would it be difficult to create a pragma to spot contradictions and tautologies as well?

Replies are listed 'Best First'.
Re: Should 'use warnings' check for contradictions?
by chromatic (Archbishop) on Jun 09, 2002 at 16:43 UTC

    Have a look at B::Lint in 5.8.

    Also, what if $team2_score has FETCH magic that causes it to be different between the two sides of the assertion? It'd be weird, but p5p is good at coming up with crazy corner cases contra even the most intelligent suggestions. :)

Re: Should 'use warnings' check for contradictions?
by Juerd (Abbot) on Jun 09, 2002 at 17:00 UTC

    No. As someone pointed out, the variables could be tied variables. Or you could be using shared memory to enable some nice race conditions.

    Your code has two things that look too much alike: you have score and scorec. I don't know what the "c" in scorec means, and someone else looking at your code might also not be able to guess its meaning. If you had used a well chosen word instead of "c", you would have seen the bug immediately, and other programmers would understand your code more easily.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

Re: Should 'use warnings' check for contradictions?
by no_slogan (Deacon) on Jun 09, 2002 at 16:34 UTC
    In your example, it's obvious that the loop condition can never be true. However, in general, determining whether a given expression is satisfiable (that is, can possibly be true) is an NP complete problem. There's no known efficient algorithm for it. You can put in more and more simple checks for more and more obvious cases, but how far down that path do you want to go?

    It's also useful to be able to do something like this:

    $DEBUG = 0; ... print "foo\n" if $DEBUG;
    without having use warnings scream at you. So a condition that's always false is not always wrong.

    Maybe you should choose variable names that don't look so similar to each other. For instance, $team1_score and $old_team1_score would be less likely to get confused than $team1_score and $team1_scorec.

      It's "only" NP complete if the clauses are known. But here the clauses are expressions, and can have side-effects (ties, threads, overload) so determining whether this would never be true quickly reduces to the halting problem.

      And no automaton that's powerful to run Perl on which the halting problem is decideable is known.


Re: Should 'use warnings' check for contradictions?
by clintp (Curate) on Jun 09, 2002 at 15:05 UTC
    Rather than a warning, some might look at this as an optimization opportunity. :)
Re: Should 'use warnings' check for contradictions?
by BrotherAde (Pilgrim) on Jun 12, 2002 at 22:02 UTC
    Scorec? Isn't that an actor/director? Aaah, no, that's Scorecc...

    Sorry, couldn't help it... Nothing to c here, move along...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://172909]
Approved by virtualsue
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-12-06 21:29 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (33 votes). Check out past polls.