Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Return DoDefault() surprise
Message
De
18/03/2011 17:42:54
 
 
À
18/03/2011 17:33:11
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Database:
Visual FoxPro
Divers
Thread ID:
01504135
Message ID:
01504184
Vues:
71
Rick's code is actually a hybrid - he set up an lnResult and is returning it at the end, but there are other RETURNs in it. Looks like he first wrote the code when he thought multiple RETURNs were OK, then had to do some addition/maintenance later so he put in lnResult, but didn't take the time to refactor and get rid of the other ones because it "ain't broke".

No offense to Rick, but I'm pretty sure he wouldn't hold up that snippet as an example of best coding practices.

>Yes, we all know this.
>
>In this case, I am calling into a method on a wwBusiness class from Rick Strahl's West Wind framework.
>
>Here's the code, for those who are curious. It's loaded with Return statements. It's not my work, so I have no choice but to call into it. And I'm certainly not going to change his source code. Sure, it has multiple Returns, however, they all seem properly coded and guarded to me. I'm not about to knock him. He's a master coder for sure, and today he might agree or do it differently, but this code has been around for years.
>
>
>*================================================================================
>* Class  wwbusiness of D:\WORK\LM_3\WW\CLASSES\WWBUSINESS.VCX
>
>Procedure wwbusiness.Query
>LPARAMETERS lcSelect, lcCursor, lnResultmode
>LOCAL lnResult
>
>lcSelect=IIF(EMPTY(lcSelect),THIS.cSQL,lcSelect)
>lcCursor=IIF(EMPTY(lcCursor),THIS.cSQLCursor,lcCursor)
>lnResultmode=IIF(EMPTY(lnResultmode),THIS.nResultmode,lnResultmode)
>
>THIS.seterror()
>
>IF !THIS.OPEN()
>   RETURN 0
>ENDIF
>
>IF EMPTY( lcSelect ) OR NOT VARTYPE( lcSelect ) = "C"
>   lcSelect = THIS.cSQL
>ENDIF
>IF EMPTY( lcSelect )
>   ** We get everything...
>   lcSelect = "*"
>ENDIF
>IF ATC(" FROM",lcSelect) = 0
>  IF THIS.nDataMode = 0
>   lcSelect = lcSelect + " FROM  '" + ALLTRIM(THIS.cDataPath + THIS.cFileName) + "' "
>  ELSE
>   lcSelect = lcSelect + " FROM " + ALLTRIM(THIS.cFileName) + " "
>  ENDIF   
>   *{lcSelect = "SELECT FROM " + Alltrim(THIS.cMasterTable) + " " + Subs(lcSelect,At(" ",lcSelect)+1)
>ENDIF
>IF NOT UPPER(lcSelect) = "SELECT "
>   lcSelect = "SELECT " + lcSelect
>ENDIF
>
>THIS.cSQL = lcSelect
>
>*** Run the SQL Statement
>DO CASE
>   CASE THIS.nDataMode = 0
>      IF ATC(" INTO",lcSelect) = 0
>         lcSelect = lcSelect + " INTO CURSOR " + lcCursor
>      ENDIF
>
>      &lcSelect
>
>      IF !USED(lcCursor)
>         RETURN 0
>      ELSE
>         lnResult = _TALLY
>      ENDIF
>   CASE THIS.nDataMode = 2
>      lcOldCursor = THIS.oSQL.cSQLCursor 
>      THIS.osql.cSQLCursor = lcCursor
>      lnResult = THIS.osql.Execute(lcSelect)
>      IF lnResult < 0
>         THIS.seterror(THIS.osql.cErrorMsg)
>         RETURN 0
>      ENDIF
>      
>      THIS.oSQL.cSQLCursor = lcOldCursor
>
>      lnResult = RECCOUNT()
>   CASE THIS.nDataMode = 4
>      lcOldCursor = THIS.oHTTPSQL.cSQLCursor 
>      THIS.oHTTPsql.cSQLCursor = lcCursor
>      lnResult = THIS.oHTTPsql.Execute(lcSelect) 
>      IF lnResult < 0
>         THIS.seterror(THIS.oHTTPsql.cErrorMsg)
>         RETURN 0
>      ENDIF
>      
>      THIS.oHTTPSQL.cSQLCursor = lcOldCursor
>ENDCASE
> 
>*** Convert data if necessary
>IF lnResultmode # 0
>   THIS.ConvertData(lnResultmode,,lcCursor)
>   USE IN (lcCursor)
>ENDIF
>
>RETURN lnResult
>
>EndProc
>
>
>
>
>
>
>>IMO multiple return/exit points are bad practice. I never, ever use them - so many Heisenbugs. If I have to work on someone else's code that has them, I refactor - no exceptions.
>>
>>It's off-topic, but if you get nothing else out of this thread, consider never having multiple exit points in your code.
>>
>>This topic has been heatedly debated here before and there's no need for me to get involved again - subscribers can search :)
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform