Rule Definition
A binary search is a very efficient search when you are dealing with a large table. Note that a binary search will only work if the table is in order by the element you are searching. A binary search looks at the first item in the table. If a match is found, the search is done. If a match is not found, it checks to see if the item you are trying to match is larger or smaller than the middle element. If it is larger, than we know that we only have to look at the half of the table past the middle. If it is smaller, than we know that we only have to look at the half of the table before the middle. Based on this decision, we have eliminate half the table from the search. We next look at the middle item in the half that is left and compare it to the element. This will either find a match or eliminate another quarter of the table. Note that in establishing what the middle is if the number of elements is even, the programmer of a binary search can choose to round or truncate. This process continue until either a match is found or it is clear that the element is not in the table.
Remediation
If the data stored in the table come from an external file then be sure that this file is sorted before processing by the program.
Violation Code Sample
B-200-LOOP.
...
... processing not related to search ...
...
SET ITEM-INDX TO 1.
SEARCH ENTRIES
AT END
MOVE ERROR-MSG-TBL TO ITEM-NAME-PR
WHEN ITEM-NUMBER-TBL (ITEM-INDX) = ITEM-NUMBER-IN
MOVE ITEM-NAME-TBL (ITEM-INDX) TO ITEM-NAME-PR
...
... processing not related to search...
...
Fixed Code Sample
B-200-LOOP.
...
... processing not related to search ...
...
SEARCH ALL ENTRIES
AT END
MOVE ERROR-MSG-TBL TO ITEM-NAME-PR
WHEN ITEM-NUMBER-TBL (ITEM-INDX) = ITEM-NUMBER-IN
MOVE ITEM-NAME-TBL (ITEM-INDX) TO ITEM-NAME-PR
...
... processing not related to search...
...
Reference
Sort the table data either in Ascending or Descending order and use a SEARCH
ALL if the search condition is simple.
Related Technologies
Cobol
Technical Criterion
Efficiency - SQL and Data Handling Performance
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.