Qbasicnews.com

Full Version: Pick your favourite.. Agamemnus should love this BTW
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Pardon my English for English is not my first language.


Now...

Is structured programming bloatware?
Below are five code samples each doing the same thing.

Samples 1 & 2 are structured, sample 1 using IF...THEN...ELSE,
sample 2 uses SELECT CASE.

The rest is of the unstuctured type designed to run in
GWBASIC for that matter.
Just ingnore the line numbers and replace them with lables
were needed.

In my view sample #3 is good while #4 is bordering on the
unreadeble if a long program is written that way.
I would never program like the #4 is written.

#5 while possible is just plain ridicilous.

Of the #1 & #2 then #2 has an edge over #1 but since this
is just a comparison between two choicies (LEFT & RIGHT) some
might disagree.

However this is not the real dilemma.
The real dilemma is the choice between sample #2 & sample #3.

#3 is much more manageble while #2 is all over the place.
On the other hand #2 has the upper hand of
being *ahem* structured.

Now pick your favourite and just ingnore the line numbers and
replace them with lables were needed.


And here is the code.
It moves the letter 'A' left and right on screen.

Sample #1:
Code:
' Init constants
CONST FALSE = 0, TRUE = NOT FALSE
CONST ESC = 27
CONST LEFT = 1, RIGHT = 2

' Init screen
SCREEN 0, 1, 0, 0
WIDTH 80, 25
CLS
LOCATE 1, 1, 0, 7, 8

' Init variables
Hor% = 41
Vert% = 12
Direction% = RIGHT
ExitProg% = FALSE
TimeDelay! = .1


' Main loop
DO
        LOCATE Vert%, Hor%
        PRINT "A";
        LOCATE Vert%, Hor%

        IF Direction% = LEFT THEN
                Hor% = Hor% - 1
                IF Hor% = 1 THEN Direction% = RIGHT
        ELSEIF Direction% = RIGHT THEN
                Hor% = Hor% + 1
                IF Hor% = 80 THEN Direction% = LEFT
        END IF

        ' Delay loop and exit handler
        T! = TIMER + TimeDelay!
        IF T! > 86399 THEN T! = T! - 86399
        DO
                IF INKEY$ = CHR$(ESC) THEN
                        ExitProg% = TRUE
                        EXIT DO
                END IF
        LOOP UNTIL T! < TIMER
        IF ExitProg% = TRUE THEN EXIT DO

        PRINT " ";
LOOP

' End of program
CLEAR
CLS
END

Sample #2:
Code:
' Init constants
CONST FALSE = 0, TRUE = NOT FALSE
CONST ESC = 27
CONST LEFT = 1, RIGHT = 2

' Init screen
SCREEN 0, 1, 0, 0
WIDTH 80, 25
CLS
LOCATE 1, 1, 0, 7, 8

' Init variables
Hor% = 41
Vert% = 12
Direction% = RIGHT
ExitProg% = FALSE
TimeDelay! = .1


' Main loop
DO
        LOCATE Vert%, Hor%
        PRINT "A";
        LOCATE Vert%, Hor%

        SELECT CASE Direction%
        CASE LEFT
                Hor% = Hor% - 1
                IF Hor% = 1 THEN Direction% = RIGHT
        CASE RIGHT
                Hor% = Hor% + 1
                IF Hor% = 80 THEN Direction% = LEFT
        END SELECT

        ' Delay loop and exit handler
        T! = TIMER + TimeDelay!
        IF T! > 86399 THEN T! = T! - 86399
        DO
                IF INKEY$ = CHR$(ESC) THEN
                        ExitProg% = TRUE
                        EXIT DO
                END IF
        LOOP UNTIL T! < TIMER
        IF ExitProg% = TRUE THEN EXIT DO

        PRINT " ";
LOOP

' End of program
CLEAR
CLS
END

Sample #3:
Code:
10 SCREEN 0, 1, 0, 0: WIDTH 80, 25: CLS : LOCATE 1, 1, 0, 7, 8
20 TimeDelay! = .1
30 Hor% = 41
40 Vert% = 12
50 Direction% = 2
60 LOCATE Vert%, Hor%
70 PRINT "A";
80 LOCATE Vert%, Hor%
90 IF Direction% = 1 THEN Hor% = Hor% - 1: IF Hor% = 1 THEN Direction% = 2: GOTO 110
100 IF Direction% = 2 THEN Hor% = Hor% + 1: IF Hor% = 80 THEN Direction% = 1
110 T! = TIMER + TimeDelay!
120 IF T! > 86399 THEN T! = T! - 86399
130 IF INKEY$ = CHR$(27) THEN GOTO 170
140 IF T! > TIMER THEN GOTO 130
150 PRINT " ";
160 GOTO 60
170 CLEAR : CLS : END

Sample #4:
Code:
10 SCREEN 0, 1, 0, 0: WIDTH 80, 25: CLS : LOCATE 1, 1, 0, 7, 8
20 TimeDelay! = .1: Hor% = 41: Vert% = 12: Direction% = 2
30 LOCATE Vert%, Hor%: PRINT "A"; : LOCATE Vert%, Hor%
40 IF Direction% = 1 THEN Hor% = Hor% - 1: IF Hor% = 1 THEN Direction% = 2: GOTO 60
50 IF Direction% = 2 THEN Hor% = Hor% + 1: IF Hor% = 80 THEN Direction% = 1
60 T! = TIMER + TimeDelay!: IF T! > 86399 THEN T! = T! - 86399
70 IF INKEY$ = CHR$(27) THEN GOTO 90
80 IF T! > TIMER THEN GOTO 70 ELSE PRINT " "; : GOTO 30
90 CLEAR : CLS : END

Sample #5:
Code:
10 SCREEN 0, 1, 0, 0: WIDTH 80, 25: CLS : LOCATE 1, 1, 0, 7, 8: TimeDelay! = .1: Hor% = 41: Vert% = 12: Direction% = 2
20 LOCATE Vert%, Hor%: PRINT "A"; : LOCATE Vert%, Hor%: IF Direction% = 1 THEN Hor% = Hor% - 1: IF Hor% = 1 THEN Direction% = 2: GOTO 40
30 IF Direction% = 2 THEN Hor% = Hor% + 1: IF Hor% = 80 THEN Direction% = 1
40 T! = TIMER + TimeDelay!: IF T! > 86399 THEN T! = T! - 86399
50 IF INKEY$ = CHR$(27) THEN CLEAR : CLS : END ELSE IF T! > TIMER THEN GOTO 50 ELSE PRINT " "; : GOTO 20

Now pick.
:???:
#1 with 2-space tabs.
Same.

(I'll have to lookup the locate command :o)
#3 with labels.
Quote:#3 with labels.

LOL! 8)
I myself would proberbly pick #1 in a
two choice senario.

Still, I kind of miss the old days.
We'll never understand you, aga....

(Agree with everyone else, tho)
Here's a better choice . . . Smile

[modified]

Code:
SCREEN 0, 1, 0, 0: WIDTH 80, 25: CLS : LOCATE 1, 1, 0, 7, 8
timeDelay! = .1: horiz% = 41: vert% = 12
t1! = TIMER + timeDelay!: IF t1! > 86400 THEN t1! = t1! - 86400
direction% = 1

DO

IF INKEY$ = CHR$(27) THEN EXIT DO

IF TIMER > t1! THEN
t1! = TIMER + timeDelay!: IF t1! > 86400 THEN t1! = t1! - 86400
PRINT " ";
LOCATE vert%, horiz%: PRINT "A"; : LOCATE vert%, horiz%
IF horiz% = 80 THEN direction% = -direction% ELSE IF horiz% = 1 THEN direction% = -direction%
horiz% = horiz% + direction%
END IF

LOOP
Quote:Here's a better choice . . . Smile

Code:
SCREEN 0, 1, 0, 0: WIDTH 80, 25: CLS : LOCATE 1, 1, 0, 7, 8
timeDelay! = .1: horiz% = 41: vert% = 12
t1! = TIMER
direction% = 1

DO

IF INKEY$ = CHR$(27) THEN EXIT DO

IF ABS(TIMER - t1!) > timeDelay! THEN
t1! = TIMER
PRINT " ";
LOCATE vert%, horiz%: PRINT "A"; : LOCATE vert%, horiz%
IF horiz% = 80 THEN direction% = -direction% ELSE IF horiz% = 1 THEN direction% = -direction%
horiz% = horiz% + direction%
END IF

LOOP

OK...

Good points... bad points
The bad points first:

You have:

IF ABS(TIMER - t1!) > timeDelay! THEN
This is one calculation (TIMER - t1!) and
one function (ABS) the comp has to go through constantly
as it executes the main DO...LOOP.
It's slow.

Still, the accuracy of the logic might be better than mine
especally in extreme boundary cases that might occur once in a
blue moon.
This is untested (by me anyhow).

However I loved the play on the positive and the negative
values on direction.
I wonder how to implement this in game engines as far as
readabillity and logic goes.
(I am just beginning looking into game engines and graphics
programming.)

But yeah, nice idea.

P.S.
I forgot to flush the keyboard buffer in the Init section
in my samples.
Damm, perfection is so hard too reach.
#1 or #2. Both are good. The others are not proper form anymore. However, they all should compile to just about the same code. Why you're considering structured programming "bloatware" is beyond me...especially when they all compile to the same thing. Readable sourcecode, whether its larger or not, is going to help your development in the long run. Using line numbers and GOTOs isn't going to give you any speed boosts in this particular case...there are times when it *might* be *slightly* faster to use GOTOs and line numbers or labels, but this isn't one of them.
adosorken:

For the record:
I began my programing on the ZX Spectrum with line numbers
and whatnot.
Then I moved to QBAISC 1.0 and 1.1 (crippleware) and VBDOS.

While programing in VBDOS I got involved with a complex and
large project.
I may have upgraded my BASIC but I didn't upgrade my mentality.

The results where catastrophic and the project came a total mess
whith a very large number of globals for e.x.

Since then I have *HATED* unstuctured programming but
the funny thing is now after some years have passed I
don't see it as totally evil anymore and I still kind of miss the
quick and dirty feel of it.

There is even a case too be made that it is the prefered way
in small projects. (Aga.. Wink Wink!)
And I totally understand that.

However,
you will not catch me *DEAD* using the unstructured way.
I have learnd my lesson.

But there is always the temtation for e.x. when you are doing
Q&D converters or something.

But I stand fast on my commitment.

So there you have it.
Pages: 1 2 3