There's a very good chance I'm over thinking this, but here it is. I'm moving a segment of code that loads data records from the database into a data structure into it's own include file. Splitting it out because I now have more than one program that needs to so work with this data. The program that originally used this logic put the data into global variables. The new programs will not; the data will be local to the function using it.
Here's the over-thinking part: it just feels like I should not be passing the global variables as parameters to the load function. Sample code below, not the most beautiful code, but I think it shows my point. For some reason, option 2 seems more correct, but I can't really say why. Hoping to benefit from others experience.
Here's the over-thinking part: it just feels like I should not be passing the global variables as parameters to the load function. Sample code below, not the most beautiful code, but I think it shows my point. For some reason, option 2 seems more correct, but I can't really say why. Hoping to benefit from others experience.
Code:
#COMPILE EXE #DIM ALL #INCLUDE "windows.inc" TYPE MyheaderType Element1 AS LONG element2 AS LONG END TYPE TYPE MyDetailtype idx AS LONG Squared AS QUAD END TYPE GLOBAL gHeaderData AS MyHeaderType GLOBAL gDetailData() AS MyDetailType GLOBAL gCS AS Critical_Section FUNCTION PBMAIN () AS LONG LOCAL Success AS LONG LOCAL xLoop AS LONG LOCAL lHeaderData AS MyHeaderType LOCAL lDetailData() AS MyDetailType REDIM lDetailData(0 TO 0) REDIM gDetailData(0 TO 0) ' Option 1 Success = LoadData(gheaderData, gDetailData()) IF ISTRUE(Success) THEN ? "loaded!" ELSE ? "Epic failure!" END IF ' option 2 Success = LoadData(lHeaderData, lDetailData()) IF ISTRUE(Success) THEN EnterCriticalSection gCS gHeaderData = lHeaderData REDIM gDetailData(1 TO UBOUND(lDetailData)) FOR xLoop = 1 TO UBOUND(lDetailData) gDetailData(xLoop) = lDetailData(xLoop) NEXT xLoop LeaveCriticalSection gCS ? "loaded!" ELSE ? "Epic failure!" END IF END FUNCTION FUNCTION LoadData(DataHeader AS MyheaderType, DataDetail() AS myDetailType) AS LONG LOCAL xLoop AS LONG ' get header data DataHeader.Element1 = 1 DataHeader.Element2 = 2 ' Load details REDIM DataDetail(1 TO 10) FOR xLoop = 1 TO 10 DataDetail(xLoop).idx = xLoop DataDetail(xLoop).Squared = (xLoop ^ 2) NEXT xLoop FUNCTION = 1 END FUNCTION​
Comment