patternModerate
Current and estimated arrival times
Viewed 0 times
arrivalcurrentestimatedandtimes
Problem
This is my first COBOL program, and I'd like to get a critique as I am unfamiliar with best practices.
Particularly, I'd like to know how to get the input and output to be more elegant and less dependent on how the file fields are structured.
Some type of analogy to C's
https://gist.github.com/anonymous/5427201
Particularly, I'd like to know how to get the input and output to be more elegant and less dependent on how the file fields are structured.
Some type of analogy to C's
scanf(), getch(), or any of the Java console IO routines would be helpful.https://gist.github.com/anonymous/5427201
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TIME-STORAGE.
03 CURRENT-TIME-NUMERIC PIC 9(8).
03 ETA-NUMERIC PIC 9(8) REDEFINES CURRENT-TIME-NUMERIC.
03 CURRENT-TIME REDEFINES CURRENT-TIME-NUMERIC.
05 CURRENTHOUR PIC 99.
05 CURRENTMINUTE PIC 99.
05 FILLER PIC 9(4).
03 ARIVAL-TIME-NUMERIC PIC 9(8).
03 ARIVAL-TIME REDEFINES ARIVAL-TIME-NUMERIC.
05 ARRIVALHOUR PIC 99.
05 ARRIVALMINUTE PIC 99.
05 FILLER PIC 9(4).
PROCEDURE DIVISION.
BEGIN-BUG-NORMAN.
DISPLAY "when will you be ariving?"
DISPLAY "HH [enter]"
ACCEPT ARRIVALHOUR
DISPLAY "MM [enter]"
ACCEPT ARRIVALMINUTE
ACCEPT CURRENT-TIME FROM TIME.
SUBTRACT ARIVAL-TIME-NUMERIC
FROM CURRENT-TIME-NUMERIC
GIVING ETA-NUMERIC.
DISPLAY "RESULTING ETA:"
DISPLAY ETA-NUMERIC.
DISPLAY "HOURS:"
DISPLAY CURRENTHOUR
DISPLAY "MINUTES:"
DISPLAY CURRENTMINUTE
STOP RUN.Solution
A couple of things I noticed:
The logic problems have already been address in another answer, so I will not address that here. I have included the code below with my suggestions added to it:
- No Identification Division. "The
IDENTIFICATION DIVISIONhas no effect on the execution of the program but is, nevertheless, required as a means of identifying the program to the computer" - Stern & Stern
- There is no real reason for the first
REDEFINES. Also, you seem to be using it wrong. A redefines clause is simply another way to reference the same working storage location. If you want to store different data, that will require different variables. While you can keep redefining the same variable and using it in different way, it can get messy and storing things gets harder.
- While you can have level 3 variables, I have found it to be best practice to go up in increments of 5
- A lot of the time, if you are working in a real COBOL environment, you will need an environment division. Clearly here you don't, but keep that in mind
- You should start all variable names in the
WORKING-STORAGE SECTIONwithWS-because if this program need to interact with any other programs, you will have aLINKAGE-SECTIONwith its own set of variables.
- Only use periods to end paragraphs. Technically, you can use a period to end anything. This leads to messy code. Also, a period closes ALL open statements. If you have a triple nested
IFand you end with one period, it will close all 3. When you get to that point, use the proper scoop terminators such asEND-IFandEND-PERFORM
- Spacing is important as well.
The logic problems have already been address in another answer, so I will not address that here. I have included the code below with my suggestions added to it:
INDENTIFICATION DIVISION
PROGRAM-ID. THE-TIMER.
AUTHOR. name.
DATE-WRITTEN. date.
DATE-COMPILED. date.
*****************************************************************
*header comment about the program change logs ect *
*****************************************************************
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TIME-STORAGE.
05 WS-CURRENT-TIME PIC 9(8).
10 WS-CURRENTHOUR PIC 99.
10 WS-CURRENTMINUTE PIC 99.
10 WS-FILLER PIC 9(4).
05 WS-ARIVAL-TIME PIC 9(8).
10 WS-ARRIVALHOUR PIC 99.
10 WS-ARRIVALMINUTE PIC 99.
10 WS-FILLER PIC 9(4).
05 WS-ETA PIC 9(8).
10 WS-ETAHOUR PIC 99.
10 WS-ETAMINUTE PIC 99.
10 WS-FILLER PIC 9(4).
PROCEDURE DIVISION.
BEGIN-BUG-NORMAN.
DISPLAY "when will you be ariving?"
DISPLAY "HH [enter]"
ACCEPT WS-ARRIVALHOUR
DISPLAY "MM [enter]"
ACCEPT WS-ARRIVALMINUTE
ACCEPT WS-CURRENT-TIME FROM TIME
SUBTRACT WS-ARIVAL-TIME
FROM WS-CURRENT-TIME
GIVING WS-ETA
DISPLAY "RESULTING ETA: "
DISPLAY ETA-NUMERIC
DISPLAY "HOURS: "
DISPLAY CURRENTHOUR
DISPLAY "MINUTES: "
DISPLAY CURRENTMINUTE
IF WS-ETAHOUR > 5
DISPLAY " It could be a while"
END-IF
STOP RUN.Code Snippets
INDENTIFICATION DIVISION
PROGRAM-ID. THE-TIMER.
AUTHOR. name.
DATE-WRITTEN. date.
DATE-COMPILED. date.
*****************************************************************
*header comment about the program change logs ect *
*****************************************************************
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TIME-STORAGE.
05 WS-CURRENT-TIME PIC 9(8).
10 WS-CURRENTHOUR PIC 99.
10 WS-CURRENTMINUTE PIC 99.
10 WS-FILLER PIC 9(4).
05 WS-ARIVAL-TIME PIC 9(8).
10 WS-ARRIVALHOUR PIC 99.
10 WS-ARRIVALMINUTE PIC 99.
10 WS-FILLER PIC 9(4).
05 WS-ETA PIC 9(8).
10 WS-ETAHOUR PIC 99.
10 WS-ETAMINUTE PIC 99.
10 WS-FILLER PIC 9(4).
PROCEDURE DIVISION.
BEGIN-BUG-NORMAN.
DISPLAY "when will you be ariving?"
DISPLAY "HH [enter]"
ACCEPT WS-ARRIVALHOUR
DISPLAY "MM [enter]"
ACCEPT WS-ARRIVALMINUTE
ACCEPT WS-CURRENT-TIME FROM TIME
SUBTRACT WS-ARIVAL-TIME
FROM WS-CURRENT-TIME
GIVING WS-ETA
DISPLAY "RESULTING ETA: "
DISPLAY ETA-NUMERIC
DISPLAY "HOURS: "
DISPLAY CURRENTHOUR
DISPLAY "MINUTES: "
DISPLAY CURRENTMINUTE
IF WS-ETAHOUR > 5
DISPLAY " It could be a while"
END-IF
STOP RUN.Context
StackExchange Code Review Q#25295, answer score: 19
Revisions (0)
No revisions yet.