(08-15-2022, 10:04 PM)TempodiBasic Wrote: Hi Bartok
I have no great theorical knowledge about "the art of coding"... so I can talk about my ideas and thought on your 2 first examples in the while that you can explaing better to me what it means "beautyfy" a code:
I've used to code as Bplus said... large indipendent block of code that do something with no correlation with the rest of code can be closed in a GOSUB (if it uses global main variables) or in a SUB/FUNCTION (passing global main variables needed by parameters or SHARED) so at the place of [a lot of code 1] you can write SubCode1 and at the place of [a lot of code 2] you can write SubCode2.
What is the difference? You read one row of code at the place of a lot of lines of code... so you can have a easier aspect of the code that gives a more editable and debuggable code.
Thank you for the answer.
What you have said replies to the "Second paranoia" of the first message. The longer version of the main code was already structurated like that and [a lot of code 1&2] already refered to "a lot" of SubCode, through the call of GOSUBs. In fact, the longer version of the "main code" lasts almost 400 lines, while the code is about 2500. So, in the main code, a lot of GOSUBs are called, there are some discursive parts and in GOSUBs, SUBs are called many many times.
Following what you say, it is possibile to maximize the concept (that was my dilemma), in order to have, in the "main code", absolutely nothing more than what is strictly necessary, as to say merging many GOSUBs and texts in more generic GOSUBs: in that way, the shorter version of the main code decreases from 400 to 200 lines, while the length of the code remains stable. I thought a lot about that and I reached a kind of conclusion: this extreme synthesis in the main code makes its own structure clearer, this is true, but maybe it doesn't make the code in general much more editable and debuggable, beacause the maximization of the synthesis in the "main code" leads to an explosion of nested GOSUBs. So, I reached a kind of rule: to have GOSUBs that, in general, are called directly from the main code, to try to avoid nested GOSUBs, and to have a nested GOSUB ony for the necessities of the GOSUB of the upper level, not for those of the main code. Futhermore, I noticed that the extreme syntesis in the main code leads some to other issues, that are difficult to explain, but that consit to have, in certain cases, some nested GOSUBs, inside a main GOSUB, that aren't logically releated, without it is possibile to avoid that. So, I have opted, on this issue, for the original version of the main code.
(08-15-2022, 10:04 PM)TempodiBasic Wrote: Talking about the two specific examples that you have posted (the first and the second ones) in the first you have a nested SELECT CASE so the logical flow is clear!
In the second example you have 2 equential SELECT CASE... the code does the same thing until when one of the conditions in the first SELECT CASE is activated AND the flow doesn't come back there after the calling.
to be clearer in the first SELECT CASE of the second example posted, if a condition matches (esc, riavvio, menu) then after activated the linked code the flow mustn't come back there in that SELECT CASE...
When I posted the first message, I didn't have thought about your objection, so I have omitted some part of the code. The 2 situation (with the 2 separated SELECT CASE, and with the 2 nested SELECT CASE), make actually the same things: as you can see below, the possibility of the repetition of the first SELECT CASE was been solved with a nested DO-LOOP:
Code: (Select All)
DO
'[A LOT OF OTHER CODE]
DO
DO
DO
_LIMIT 30
KeyPress$ = INKEY$
LOOP UNTIL KeyPress$ = "1" OR KeyPress$ = "2" OR KeyPress$ = CHR$(27) OR KeyPress$ = CHR$(9) OR KeyPress$ = CHR$(0) + CHR$(59)
SELECT CASE KeyPress$
CASE CHR$(27)
esc~` = 1
CASE CHR$(9)
riavvio~` = 1
CASE CHR$(0) + CHR$(59)
menu~` = 1
END SELECT
IF esc~` = 1 OR riavvio~` = 1 OR menu~` = 1 THEN EXIT DO
'[A LOT OF CODE 1] <---------------------------------------------
DO
_LIMIT 30
KeyPress$ = INKEY$
LOOP UNTIL KeyPress$ = CHR$(27) OR KeyPress$ = CHR$(9) OR KeyPress$ = CHR$(0) + CHR$(59) OR KeyPress$ = CHR$(0) + CHR$(77)
SELECT CASE KeyPress$
CASE CHR$(27)
esc~` = 1
CASE CHR$(9)
riavvio~` = 1
CASE CHR$(0) + CHR$(59)
menu~` = 1
END SELECT
IF esc~` = 1 OR riavvio~` = 1 OR menu~` = 1 THEN EXIT DO
'[A LOT OF CODE 2] <---------------------------------------------
LOOP
LOOP UNTIL esc~` = 1 OR riavvio~` = 1 '<--------------------------------------------- if menu~` = 1, then the first SELECT CASE is repeated.
LOOP UNTIL esc~` = 1
END
With the nested SELECT CASE, conversely, I made like this:
Code: (Select All)
DO
'[A LOT OF OTHER CODE]
DO
DO
_LIMIT 30
KeyPress$ = INKEY$
LOOP UNTIL KeyPress$ = "1" OR KeyPress$ = "2" OR KeyPress$ = CHR$(27) OR KeyPress$ = CHR$(9) OR KeyPress$ = CHR$(0) + CHR$(59)
SELECT CASE KeyPress$
CASE "1", "2"
'[A LOT OF CODE 1] <---------------------------------------------
IF esc~` = 1 OR riavvio~` = 1 OR menu~` = 1 THEN EXIT SELECT
DO
_LIMIT 30
KeyPress$ = INKEY$
LOOP UNTIL KeyPress$ = CHR$(27) OR KeyPress$ = CHR$(9) OR KeyPress$ = CHR$(0) + CHR$(59) OR KeyPress$ = CHR$(0) + CHR$(77)
SELECT CASE KeyPress$
CASE CHR$(27)
esc~` = 1
CASE CHR$(9)
riavvio~` = 1
CASE CHR$(0) + CHR$(59)
menu~` = 1
CASE CHR$(0) + CHR$(77)
'[A LOT OF CODE 2] <---------------------------------------------
END SELECT
CASE CHR$(27)
esc~` = 1
CASE CHR$(9)
riavvio~` = 1
CASE CHR$(0) + CHR$(59)
menu~` = 1
END SELECT
LOOP UNTIL esc~` = 1 OR riavvio~` = 1
LOOP UNTIL esc~` = 1
END
I agree that the soluton with the nested SELECT CASE, it is better: there is one less DO - LOOP and maybe it is clearer. So, in this case, I opted for the modified solution.