weakchar

the "Weak Character" test

Rule "weakchar" gives this warning when it finds an ' IF charvar="" '
test that does not take into consideration that charvar might also
have the UNKNOWN value.

   IF AVAILABLE customer THEN
      IF customer.e-mail <> "" THEN
         RUN SendMail (customer.e-mail, subject, body).
      ELSE
         IF customer.fax <> "" THEN
            RUN SendFax (customer.fax, subject, body).

Trouble will happen if customer.e-mail happens to have the UNKNOWN value. The IF
condition did not check that.

So what is the proper way to check if a character field is not blanc
or unknown? You can think of several solutions, it is a matter of
taste or style, or perhaps even code-religion to pick the best. Here are a couple of
possible solutions:

   IF customer.e-mail > "" THEN ...

   IF NOT (customer.e-mail LE "") THEN ...
   
   IF NOT (customer.e-mail="" or customer.e-mail=?) THEN ...

   IF customer.e-mail<>"" AND customer.e-mail<>? THEN ...

This prolint rule gives probably many false positives and false negatives, it
needs improvement. One difficult issue is: what about the ELSE branche
of the IF statement?

How to suppress these warnings:

You can put the directive {&_proparse_ prolint-nowarn(weakchar)} directly before
the IF statement. See also: suppress warnings.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jurjen's picture

be careful with NOT and ELSE!

The description of this "weakchar" rule suggests that IF (stringvariable > "") THEN is the best and easiest way to test for ="" or =?.
But it can easily go wrong (different then expected) if you also add the NOT keyword.
For example:

    stringvariable = ?.
    IF stringvariable > "" THEN
       MESSAGE "true".
    ELSE
       MESSAGE "false".

The sample will display "false", as expected, because the stringvariable is unknown. But what would you expect when you add NOT to the boolean expression?

    stringvariable = ?.
    IF NOT (stringvariable > "") THEN
       MESSAGE "true".
    ELSE
       MESSAGE "false".

You expect to see "true"? Well, surprise, it does not!
Explanation:
the result of boolean expression stringvariable > "" is not False but it is Unknown. NOT unknow is still unknown.
The THEN branch executes when the boolean expression is TRUE, the ELSE brancch is executed when the boolean expression is FALSE or UNKNOWN.

So, the weakchar test may be a good idea, but be very careful if you combine it with NOT !!!