12-17-2003, 06:10 AM
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:
Sample #2:
Sample #3:
Sample #4:
Sample #5:
Now pick.
:???:
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.
:???: