Disadvantages of CAN-FIND function

Hi Everybody,
Can anyone let me know if there is any disadvantage of using CAN-FIND function?

Any special information on CAN-FIND function...???
 
Check the "notes" section of the documentation for CAN-FIND.

Personally I'd just as soon use FIND NO-ERROR and test for availability.
 
No Disadvantages at all, it helps to get faster the result and makes the script shorter.
 
No disavantages. Faster because not brings the record to record buffer. Usage depends on either make use of the record(Find) or just to check the availability(Can-Find)
Regards
Philip
 
Posted by TomBasCom,
Personally I'd just as soon use FIND NO-ERROR and test for availability
Can you please let me know the reason??? I need to know why not FIND with NO-ERROR instead of CAN-FIND...

I agree with "philippoommen1" point. Is there anyone to go against it? Please let me know.

Thanks for your eager participation as of now. Expect the same till this thread completes.
 
CAN-FIND is a very pessimistic keyword and I'm an optimistic coder.

99.99% of the time if the record is available I'm going to use it. After all, that's why I'm looking for it. And 99.999% of the time the record is going to be available. Exception code is an exception. Don't confuse coding defensively with coding pessimistically. So CAN-FIND is just a ridiculous circumlocution.

I would rather code:
Code:
find customer no-lock where custNum = cNum no-error.
if available( customer ) then
  do:
    /* stuff */
  end.

than

Code:
if can-find( customer no-lock where custNum = cNum ) then
  do:
    find customer no-lock where custNum = cNum no-error.
    /* stuff */
  end.

My version is also more efficient. I only read the index to find the record once. The CAN-FIND approach reads it twice, once for CAN-FIND and again when FIND executes.

CAN-FIND might be occasionally useful in a WHERE clause. Although even then I find it awkward and only rarely interesting.

CAN-FIND is one of those ancient 4GL statements that is used a lot in very old legacy code for reasons that largely are no longer valid. Don't keep using it just because you've seen it a lot in some nasty code that nobody in their right mind would be using as an example of fine programming examples.
 
what about checking if a record exists before creating it.

Code:
IF NOT CAN-FIND( FIRST myTable
                          WHERE  myTable.fielda =  1337
                        )
then do:
  create myTable.
  assign

  .
end.


i got this quite often and in this case i dont need the record in the record buffer.
 
I still wouldn't use CAN-FIND. Nor would I use FIRST.

If I'm going to create it when not found then I'm still in that "I plan to use the record" mode. And I still expect that successful finds will vastly outweigh unsuccessful finds. This is easy to confirm -- just look at your database's ratio of records reads to creates. I'll bet you that creates are just a very tiny percentage of reads. (And a lot of those creates are done more directly than "try to find it, if that fails create it".)

CAN-FIND is an old legacy construct. Like several other old 4GL verbs it made a very exciting demo in 1988.

IMHO it is primarily used out of habit by people who have been over-exposed to some very, very bad coding styles.
 
what about checking if a record exists before creating it.

Code:
IF NOT CAN-FIND( FIRST myTable
                          WHERE  myTable.fielda =  1337
                        )
then do:
  create myTable.
  assign

  .
end.
i got this quite often and in this case i dont need the record in the record buffer.


I still would use a find like this.

Code:
FIND mytable WHERE mytable.fielda = 1337 EXCLUSIVE-LOCK NO-ERROR.
IF NOT AVAILABLE mytable
THEN DO:
    CREATE mytable.
    ASSIGN mytable.fielda = 1337.
END.
/* If you want you can now update all other fields, because the record is always available in the record buffer */
ASSIGN FieldB = "kkkk" 
           etc...

Most of the times you want to create a record if it does not exist, or update non indexed fields when the records does exist. In this sample you can do both.

I seldom use CAN-FIND. I only use it in case I really do not need the record, but I need to be sure that it is (not) available. For instance to check if I can delete a record if there are no related records available.
 
Back
Top