SUBS or GOSUB; for library projects, which do you guys prefer? - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: Chatting and Socializing (https://staging.qb64phoenix.com/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://staging.qb64phoenix.com/forumdisplay.php?fid=2) +--- Thread: SUBS or GOSUB; for library projects, which do you guys prefer? (/showthread.php?tid=819) |
SUBS or GOSUB; for library projects, which do you guys prefer? - Pete - 08-27-2022 I just wrapped up a new faster edition of my string math routines. Jack provided another source of string math code from Treebeard. Treebeard's structure have many, may subs and functions. If you added it to your routines, you would add 50 subs/functions to work with addition, subtraction, multiplication, division, and square root calculations. Now you only need to call one sub for each of those, and then those 5 subs call the other subs and functions. In my original string math, which did not include square root, everything was contained in one sub. YOU called one sub, using an operator like "+", "-", etc. and the string math operation was returned. That's a lot less clutter when adding such a routine to function in another program. So... Which method do you guys prefer, and what pros and cons do you see in both if SEVERAL are needed to complete a library? For me, it's the following. SUBS and FUNCTIONS: Pros: Easier for the developer to work with when debugging. Fewer problems or no need at all to clear variables. Advantage to passing variables by reference. It's a neat trick to change the names without additional renaming statements. Faster to code, provided you use TYPES for many passed variables to cut down on the variable lists passed. Can be identified in the editor (IDE) easily enough if a prefix is used for each like SUB Smath_Mult, SUB SMath_DIV, etc. Cons: Harder for the user to find their own program subs and functions n the mix. Cluttered SUB/FUNCTIONS lists to sort through in the IDE. Requires multiple CALL arguments instead of just a single call the library sub like CALL StringMath(). ------------------------------------------------------------------------------------------------------------------------ GOSUBs in a single SUB Pros: All code contained in one easy to port sub routine. Less project time working out passing variables; (e.g., GOSUB StringMult instead of String_Mult var1, va2, var3, etc. Keeps the subs and function list in the editor clean, making it easier to navigate the program to which it is ported. Cons: More work making sure variable names do not conflict with other GOSUB routines, meaning more need for unique variable names. More work zeroing variables. Possible stack space issues if too nested and a RETURN is missed. Harder for the developer to work on in the IDE, although Alt arrow left indexing helps some. -------------------------------------------------------------------------------------------------------------------------- Anyway, what other pros and cons come to mind and again, which method would you rather use as a library addition in your own programming projects? Pete RE: SUBS or GOSUB; for library projects, which do you guys prefer? - Jack - 08-27-2022 Pete, there's a place for an eval type math evaluater but I prefer to call each function individually for me it's easier to see the program flow that way and it may perform better, besides, trying to figure out the workings of someone else all-in-one math evaluator can be incredibly hard, what if there's a bug but can't figure out this conglomerate of functions ? RE: SUBS or GOSUB; for library projects, which do you guys prefer? - bplus - 08-27-2022 The only way you could possibly use a GoSub in a library is to have it inside a Sub or Function. RE: SUBS or GOSUB; for library projects, which do you guys prefer? - SMcNeill - 08-27-2022 (08-27-2022, 07:13 PM)bplus Wrote: The only way you could possibly use a GoSub in a library is to have it inside a Sub or Function. You can put a GOSUB library anywhere inside your main code. Code: (Select All) GOTO skipLibrary2022.08.27 Feel free to save that as a *.BM library file and $INCLUDE it in any of your programs, anywhere in the main module. Just call it with GOSUB proveExample. That said, I'd *never* go such a route. For starters, the routine is *only* callable from the main module -- it won't work with any of your other SUB or FUNCTION modules. Any variables in it are all in scope, so if you use a FOR i ...NEXT, you better make certain you're not corrupting the value of i elsewhere with it. There's no easy way to free variables used in the gosub, and all-in-all, it's just one of those tools of the caveman programmer like LET, in my opinion. I can't think of more than a handful of times in my long years of programming where I thought "GOSUB might be better than SUB for this". When I did, it was almost exclusively just for use *inside a SUB* itself. RE: SUBS or GOSUB; for library projects, which do you guys prefer? - TempodiBasic - 08-27-2022 @SmcNeill LOL I must think that it is your work the GOTO in mid2asc.c posted by MadSciJr! I know that it is possible to use GOTO in C, but never seen before this time. PS: I like more the closing trend with localization of variables... so I like SUB vs GOSUB and FUNCTION vs DEF FN! RE: SUBS or GOSUB; for library projects, which do you guys prefer? - SMcNeill - 08-27-2022 (08-27-2022, 09:05 PM)TempodiBasic Wrote: @SmcNeill Used to see it a lot in older code. One place in particular where I recall seeing it a ton was with ON ERROR routines: Code: (Select All) ON ERROR GOSUB foo Folks would tend to put their error handler close to the ON ERROR statement, but you didn't want it to execute with the main program code, so it tended to get wrapped in a GOTO and a label after it to make certain that it was never executed except when an error called it directly. I recall seeing that setup in a TON of old QBASIC type routines which did error trapping. RE: SUBS or GOSUB; for library projects, which do you guys prefer? - mdijkens - 08-28-2022 I'd prefer sub/function normally. To prevent clutter of functions in the IDE-list, I normally prefix them per group: function csv.read function csv.write function tim.date2Number function serial.open etc RE: SUBS or GOSUB; for library projects, which do you guys prefer? - TerryRitchie - 08-28-2022 I prefer SUBs/FUNCTIONs mainly because of the variable cons you listed under GOSUB. RE: SUBS or GOSUB; for library projects, which do you guys prefer? - Kernelpanic - 08-29-2022 (08-27-2022, 09:05 PM)TempodiBasic Wrote: @SmcNeill Yes, "goto" exists in C, and sometimes it can be useful too, for example to get out of a deeply nested loop. Sure, that can also be solved differently, but much more complicated - and therefore more error-prone. Here's an example of "goto" in C, but really just an example. One can easily program it differently. Many gotos . . . Code: (Select All) //Goto Benutzung - 28. Aug.2022 Screenshot: RE: SUBS or GOSUB; for library projects, which do you guys prefer? - Kernelpanic - 08-29-2022 I have to do something for Basic now! Loosely based on Ken (Excuse me) : Code: (Select All) Screen 12 |