HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Trim Functions in COBOL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
functionstrimcobol

Problem

A coworker, from a past programming life, called me up last week and asked about Trim Functions in COBOL. I sent her these PEFORMed "functions", which I've had lying about and without any professional opportunity to use them.

It's been my experience that most Cobologists working within their shop standard don't twiddle with CALLing real subroutines. Instead they PERFORM COPYed code, and refer to it as a subroutine. As such, I don't try to fight the tide.

Can anyone offer up any improvements? I know there are Intrinsic TRIM, TRIML, and TRIMR functions in some flavours of COBOL. However, I do not believe IBM Z-series Mainframe COBOL has these, so this is intended to be platform independent.

```
DATA DIVISION.

WORKING-STORAGE SECTION.

01 STR-VALUE-IN PIC X(1000).
01 STR-LENGTH-IN PIC 9(05) BINARY.
01 STR-VALUE-OUT PIC X(1000).
01 STR-LENGTH-OUT PIC 9(05) BINARY.
01 STR-HANDLING-COUNTERS.
05 STR-SUB01 PIC 9(05) BINARY.
05 STR-TRAILING-SPACES PIC 9(05) BINARY.
05 STR-LEADING-SPACES PIC 9(05) BINARY.
05 STR-START PIC 9(05) BINARY.
01 STR-HANDLING-SWITCHES.
05 GRAPH-FOUND-SW PIC X(01).
88 GRAPH-FOUND VALUE '1'.
88 GRAPH-NOT-FOUND VALUE '2'.

PROCEDURE DIVISION.

MOVE Whatever
TO STR-VALUE-IN.
MOVE FUNCTION LENGTH (Whatever)
TO STR-LENGTH-IN.
PERFORM 5200-RTRIM.
MOVE STR-VALUE-OUT (1:STR-LENGTH-OUT)
TO Wherever.

MOVE Whatever
TO STR-VALUE-IN.
MOVE FUNCTION LENGTH (Whatever)
TO STR-LENGTH-IN.
PERFORM 5100-LTRIM.
MOVE STR-VALUE-OUT (1:STR-LENGTH-OUT)
TO Wherever.

MOVE Whatever
TO STR-VALUE-IN.
MOVE FUNCTION LENGTH (Whatever)
TO STR-LENGTH-IN.
PERFORM 5300-TRIM.
MOVE STR-VALUE-OUT (1:STR-LENGTH-OUT)
TO Wherever.

5100-LTRIM.
* This resembles the VB function LTrim
* It removes any

Solution

It looks pretty good to me. Other than the missing periods at the end of 5930-TRIM-LEADING-SPACES and 5950-TRIM-TRAILING-SPACES, it works fine.

I do have a few minor suggestions:

-
The name STR-SUB01 is vague; STR-POS would be informative.

-
If the code is not going to be called very often, you could give STR-START a VALUE of 1 and shorten 5910-COMMON-TRIM-BEGIN to the more high-level

5910-COMMON-TRIM-BEGIN.
    INITIALIZE STR-HANDLING-SWITCHES STR-HANDLING-COUNTERS
      ALL TO VALUE.
    MOVE STR-LENGTH-IN
      TO STR-LENGTH-OUT.


-
The code in the PERFORM loops is nested more deeply than the conditions, which makes it hard to tell where the condition ends and where the code begins.

To nitpick:

  • The reference to VB functions is only useful if the programmer reading it knows VB.



  • If your code is compiled on an EBCDIC system, then it will trim values less than decimal 64, making the comment on ASCII values irrelevant.




It's been my experience that most Cobologists working within their shop standard don't twiddle with CALLing real subroutines. Instead they PERFORM COPYed code, and refer to it as a subroutine. As such, I don't try to fight the tide.

But you should fight the tide! While PERFORMs modularise code, only CALLs modularise data as well. CALL allows you to control the data that goes in and out and to break monolithic programs into comprehensible chunks. Using COBOL is not an excuse for ignoring modern programming guidelines.

The above rant brings me to one final suggestion: the code would be even better if it was a subprogram.

Code Snippets

5910-COMMON-TRIM-BEGIN.
    INITIALIZE STR-HANDLING-SWITCHES STR-HANDLING-COUNTERS
      ALL TO VALUE.
    MOVE STR-LENGTH-IN
      TO STR-LENGTH-OUT.

Context

StackExchange Code Review Q#69220, answer score: 5

Revisions (0)

No revisions yet.