Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

brain teaser
Is this a brain teaser or am I just trying to get someone to do my work for
me?
The latter, but here it is anyway.  Refactor the following paragraph in
order to eliminate the gotos.

SEND-PAGE.
PERFORM CALC-PAGE-FIELDS-BEFORE
PERFORM SEND-OUT
SUBTRACT DOCSND FROM BYTES-TO-SEND
COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
IF BYTES-TO-SEND > ZERO
MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
IF LAST-CALL
OR REFLEN > EXT-OUTPUT-LEN
ADD DOCSND TO DOCREF
IF LAST-CALL
SET LAST-SEND TO TRUE
END-IF
GO TO SEND-PAGE
ELSE
MOVE REFLEN TO SAVE-LEN
END-IF
ELSE
IF EXTRA-PAGE-NEEDED
GO TO SEND-PAGE
END-IF
END-IF
EXIT.

Frank


---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO  USA

Report this thread to moderator Post Follow-up to this message
Old Post
Frank Swarbrick
05-08-06 11:55 PM


Re: brain teaser
Frank Swarbrick wrote:
> Is this a brain teaser or am I just trying to get someone to do my work fo
r
> me?
> The latter, but here it is anyway.  Refactor the following paragraph in
> order to eliminate the gotos.
>
>  SEND-PAGE.
>      PERFORM CALC-PAGE-FIELDS-BEFORE
>      PERFORM SEND-OUT
>      SUBTRACT DOCSND FROM BYTES-TO-SEND
>      COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
>      IF BYTES-TO-SEND > ZERO
>          MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
>          IF LAST-CALL
>          OR REFLEN > EXT-OUTPUT-LEN
>              ADD DOCSND TO DOCREF
>              IF LAST-CALL
>                  SET LAST-SEND TO TRUE
>              END-IF
>              GO TO SEND-PAGE
>          ELSE
>              MOVE REFLEN TO SAVE-LEN
>          END-IF
>      ELSE
>          IF EXTRA-PAGE-NEEDED
>              GO TO SEND-PAGE
>          END-IF
>      END-IF
>      EXIT.                   <<<<---  EXIT must be in a paragraph by itself.[/colo
r]

It is probably going to be something like:

Send-Page.
PERFORM
WITH TEST AFTER
UNTIL Bytes-To-Send NOT > ZERO
AND NOT Extra-Page-Needed
...

END-PERFORM

Report this thread to moderator Post Follow-up to this message
Old Post
Richard
05-08-06 11:55 PM


Re: brain teaser
"Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> wrote in message
news:4c9qskF151cl5U1@individual.net...
> Is this a brain teaser or am I just trying to get someone to do my work
> for
> me?
> The latter, but here it is anyway.  Refactor the following paragraph in
> order to eliminate the gotos.
>
> SEND-PAGE.
>     PERFORM CALC-PAGE-FIELDS-BEFORE
>     PERFORM SEND-OUT
>     SUBTRACT DOCSND FROM BYTES-TO-SEND
>     COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
>     IF BYTES-TO-SEND > ZERO
>         MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
>         IF LAST-CALL
>         OR REFLEN > EXT-OUTPUT-LEN
>             ADD DOCSND TO DOCREF
>             IF LAST-CALL
>                 SET LAST-SEND TO TRUE
>             END-IF
>             GO TO SEND-PAGE
>         ELSE
>             MOVE REFLEN TO SAVE-LEN
>         END-IF
>     ELSE
>         IF EXTRA-PAGE-NEEDED
>             GO TO SEND-PAGE
>         END-IF
>     END-IF
>     EXIT.

* Add a new 88 called TIME-TO-STOP
SEND-PAGE.
PERFORM
WITH TEST AFTER
UNTIL ((BYTES-TO-SEND <= ZERO AND NOT EXTRA-PAGE-NEEDED) OR
TIME-TO-STOP)
PERFORM CALC-PAGE-FIELDS-BEFORE
PERFORM SEND-OUT
SUBTRACT DOCSND FROM BYTES-TO-SEND
COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
IF BYTES-TO-SEND > ZERO
MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
IF LAST-CALL
OR REFLEN > EXT-OUTPUT-LEN
ADD DOCSND TO DOCREF
IF LAST-CALL
SET LAST-SEND TO TRUE
END-IF
ELSE
MOVE REFLEN TO SAVE-LEN
SET TIME-TO-STOP TO TRUE
END-IF
END-IF
END-PERFORM
EXIT.

- Oliver


Report this thread to moderator Post Follow-up to this message
Old Post
Oliver Wong
05-09-06 02:55 AM


Re: brain teaser
Frank Swarbrick <Frank.Swarbrick@efirstbank.com> wrote:

> eliminate the gotos
* add ws sw-loop-off

SEND-PAGE.
move 0              to sw-loop-off
perform until sw-loop-off = 0
move 1           to sw-loop-off
PERFORM CALC-PAGE-FIELDS-BEFORE
PERFORM SEND-OUT
SUBTRACT DOCSND FROM BYTES-TO-SEND
COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
IF BYTES-TO-SEND > ZERO
MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
IF LAST-CALL
OR REFLEN > EXT-OUTPUT-LEN
ADD DOCSND TO DOCREF
IF LAST-CALL
SET LAST-SEND TO TRUE
END-IF
move 0  to sw-loop-off
ELSE
MOVE REFLEN TO SAVE-LEN
END-IF
ELSE
IF EXTRA-PAGE-NEEDED
move 0   to sw-loop-off
END-IF
END-IF
end-perform.
EXIT.

----------------------
* Better:

routine-before.
move 0 to sw-loop-off
perform send-page until sw-loop-off = 0
...


SEND-PAGE.
move 1           to sw-loop-off
PERFORM CALC-PAGE-FIELDS-BEFORE
PERFORM SEND-OUT
SUBTRACT DOCSND FROM BYTES-TO-SEND
COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
IF BYTES-TO-SEND > ZERO
MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
IF LAST-CALL
OR REFLEN > EXT-OUTPUT-LEN
ADD DOCSND TO DOCREF
IF LAST-CALL
SET LAST-SEND TO TRUE
END-IF
move 0    to sw-loop-off
ELSE
MOVE REFLEN TO SAVE-LEN
END-IF
ELSE
IF EXTRA-PAGE-NEEDED
move 0    to sw-loop-off
END-IF
END-IF
EXIT.

Report this thread to moderator Post Follow-up to this message
Old Post
Roberto
05-09-06 08:55 AM


Re: brain teaser
Errr, No.
The default if neither TEST BEFORE nor TEST AFTER phrase
is sepecified is TEST BEFORE.
Therefore you will not perform anything.


"Roberto" <re.lotto@kataLAmail.com> schrieb im Newsbeitrag
news:1hf2bsv.1bvmkwfqdabhaN%re.lotto@kataLAmail.com...
> Frank Swarbrick <Frank.Swarbrick@efirstbank.com> wrote:
> 
> * add ws sw-loop-off
>
> SEND-PAGE.
>     move 0              to sw-loop-off
>     perform until sw-loop-off = 0
>                move 1           to sw-loop-off
>        PERFORM CALC-PAGE-FIELDS-BEFORE
>        PERFORM SEND-OUT
>        SUBTRACT DOCSND FROM BYTES-TO-SEND
>        COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
>        IF BYTES-TO-SEND > ZERO
>          MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
>          IF LAST-CALL
>          OR REFLEN > EXT-OUTPUT-LEN
>              ADD DOCSND TO DOCREF
>              IF LAST-CALL
>                  SET LAST-SEND TO TRUE
>              END-IF
>              move 0  to sw-loop-off
>           ELSE
>              MOVE REFLEN TO SAVE-LEN
>          END-IF
>        ELSE
>            IF EXTRA-PAGE-NEEDED
>              move 0   to sw-loop-off
>          END-IF
>        END-IF
>     end-perform.
>     EXIT.
>
> ----------------------
> * Better:
>
> routine-before.
>     move 0 to sw-loop-off
>     perform send-page until sw-loop-off = 0
>     ....
>
>
> SEND-PAGE.
>     move 1           to sw-loop-off
> PERFORM CALC-PAGE-FIELDS-BEFORE
>     PERFORM SEND-OUT
>     SUBTRACT DOCSND FROM BYTES-TO-SEND
>     COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
>     IF BYTES-TO-SEND > ZERO
>         MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
>         IF LAST-CALL
>         OR REFLEN > EXT-OUTPUT-LEN
>             ADD DOCSND TO DOCREF
>             IF LAST-CALL
>                 SET LAST-SEND TO TRUE
>             END-IF
>             move 0    to sw-loop-off
>         ELSE
>             MOVE REFLEN TO SAVE-LEN
>         END-IF
>     ELSE
>         IF EXTRA-PAGE-NEEDED
>             move 0    to sw-loop-off
>         END-IF
>     END-IF
>     EXIT.



Report this thread to moderator Post Follow-up to this message
Old Post
Roger While
05-09-06 08:55 AM


Re: brain teaser
On Mon, 8 May 2006 15:18:12 -0600, "Frank Swarbrick"
<Frank.Swarbrick@efirstbank.com> wrote:

>Is this a brain teaser or am I just trying to get someone to do my work for
>me?

It doesn't qualify as a brain teaser.

Report this thread to moderator Post Follow-up to this message
Old Post
Howard Brazee
05-09-06 12:55 PM


Re: brain teaser
(*SET JOBDONE TO FALSE*)
PERFORM SEND-PAGE UNTIL JOBDONE


SEND-PAGE.
PERFORM CALC-PAGE-FIELDS-BEFORE
PERFORM SEND-OUT
SUBTRACT DOCSND FROM BYTES-TO-SEND
COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
IF BYTES-TO-SEND > ZERO
MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
IF LAST-CALL
ADD DOCSND TO DOCREF
SET LAST-SEND TO TRUE
ELSE
IF REFLEN > EXT-OUTPUT-LEN THEN
ADD DOCSND TO DOCREF
ELSE
MOVE REFLEN TO SAVE-LEN
SET JOBDONE TO TRUE
END-IF
END-IF
ELSE
IF NOT EXTRA-PAGE-NEEDED
SET JOBDONE TO TRUE
END-IF
END-IF

Report this thread to moderator Post Follow-up to this message
Old Post
Günther De Vogelaere
05-09-06 11:55 PM


Re: brain teaser
Roger While <simrw@sim-basis.de> wrote:

> The default if neither TEST BEFORE nor TEST AFTER phrase
> is sepecified is TEST BEFORE.

Oopps, I know it. I wroted wrong sentence, sorry.

This the correct source:

SEND-PAGE.
move 0              to sw-loop-off
* ---------------------------------------- changed
perform until sw-loop-off = 1
* ----------------------------------------
move 1           to sw-loop-off
PERFORM CALC-PAGE-FIELDS-BEFORE
PERFORM SEND-OUT
SUBTRACT DOCSND FROM BYTES-TO-SEND
COMPUTE REFLEN = DOCUMENT-LEN + SAVE-LEN - DOCSNDTOT
IF BYTES-TO-SEND > ZERO
MOVE DOCUMENT-AREA (DOCREF:) TO SAVE-AREA (1:REFLEN)
IF LAST-CALL
OR REFLEN > EXT-OUTPUT-LEN
ADD DOCSND TO DOCREF
IF LAST-CALL
SET LAST-SEND TO TRUE
END-IF
move 0  to sw-loop-off
ELSE
MOVE REFLEN TO SAVE-LEN
END-IF
ELSE
IF EXTRA-PAGE-NEEDED
move 0   to sw-loop-off
END-IF
END-IF
end-perform.
EXIT.

Report this thread to moderator Post Follow-up to this message
Old Post
Roberto
05-09-06 11:55 PM


Re: brain teaser
Frank Swarbrick wrote:

> IBM COBOL for VSE does not require EXIT to be in a paragraph by itself
> (thank goodness!).

No, but Cobol standard does and that makes your code less portable.

>  Even if it did I'd just use CONTINUE instead, though
> it's meaning would not be as obvious...

What is 'obvious' is entirely what one is used to. To some an 'EXIT'
may _obviously_, well, do an exit.  I have seen an attempt to EXIT from
inside the middle of an IF.

> That doesn't seem to take in to account the case when LAST-CALL is true or
> REFLEN > EXT-OUTPUT-LEN,

Those are only tested when Bytes-To-Send > ZERO so they don't need to
be in the UNTIL.

> in which case we want to do another send unless
> BYTES-TO-SEND = ZERO.

Exactly.

> I just think it's cleaner and easier to understand *with* the GOTOs...

What is 'easier to understand' is entirely what one is used to.  If you
were to work with clean gotoless code then it would become easier to
understand, but you need to force yourself by either doing it or
working with other languages such as C or Python or and/by doing OO.

Fiddling with single paragraphs to remove a couple of gotos is not
going to make the transition to a better way of dealing with program
structure.

It is like having wattle and daub huts and then hearing that
wood-framing with siding is better.  Trying to put in a piece of
wood-frame or putting siding over the wattle just doesn't do it.

> Anyway, to clarify what my goal is here...  We'll always send one "page".
> If DOCUMENT-AREA contains more than one page (REFLEN > EXT-OUTPUT-LEN) the
n
> it should send the remaining *full* pages.  Any partial page that is left
> over should be saved in SAVE-AREA and not sent, unless there are no more
> pages to be sent (LAST-CALL is true), in which case we do want to send the
> partial page.

Having a specification for what 'send-page' does is good.

One of the reasons that I said 'probably' in the original is that
without the full spec or the full program it may have not been possible
to see all the conditions that may occur.


Report this thread to moderator Post Follow-up to this message
Old Post
Richard
05-09-06 11:55 PM


Re: brain teaser
Frank Swarbrick wrote:

> I'm not go to fan, honestly.  But sometimes they are useful, at least in t
he
> absense of things such as "EXIT PERFORM" and "EXIT PERFORM CYCLE".

I dislike the use of the 'EXIT ...' statements which are a form of
goto. The main reason is that I move code around: cut a block from
within an inline perform or a nested if and make it into a paragraph so
I can reuse it, or just to simplify the levels of nesting. _Anything_
that is positionally dependent is just not used.


Report this thread to moderator Post Follow-up to this message
Old Post
Richard
05-09-06 11:55 PM


Sponsored Links




Last Thread Next Thread Next
Pages (5): [1] 2 3 4 5 »
Search this forum -> 
Post New Thread

Cobol archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:48 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.