Autore Topic: Non conoscevo Assert  (Letto 383 volte)

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.159
  • Tonno verde
    • Mostra profilo
Non conoscevo Assert
« il: 02 Marzo 2020, 19:56:39 »
Un'interessante risposta sul Bugtracker di Benoit alla richiesta di Bruce:

This is just a shortcut to save having to include Print/Error commands just before the stop.
During development, I frequently leave unresolved error conditions hanging around with Stop's in them as I might be focussing on some other part of the code. These are usually the error conditions that don't happen often, such as I'm processing a large set of records with some complex code, maybe one record in the set has a null value that I haven't figured out what to do about yet as I'm looking at some other aspect of the processing....
So,
 
Codice: [Seleziona]
If not CurrentTransaction.Taxable Then Stop "Tax flag is null"
is a lot more concise than
 
Codice: [Seleziona]
  If not CurrentTransaction.Taxable Then
    Print "Tax flag is null"
    Stop
  Endif
(Especially in the middle of a complex set of if..elses)
and this is the important bit...
would immediately take my mind to the problem rather than having to think about what caused the Stop.
Also the reason for the stop might (and sometimes does) occur somewhere else..  so
 
Codice: [Seleziona]
If sFault then Stop sFault
where sFault is a message set nowhere near where the Stop command is in the code....

Benoit:

Why don't you use the ASSERT instruction for that?
Codice: [Seleziona]
Assert CurrentTransaction.Taxable Print "Tax flag is null"

Semplice dimostrazione di utilizzo:

Codice: [Seleziona]
Public Sub Main()

  Dim UnaStringaQualsiasi As String

  Assert UnaStringaQualsiasi Print "#ERRORE la stringa risulta vuota"

End

 :ciao:
« Ultima modifica: 02 Marzo 2020, 19:57:20 da Gianluigi »
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.334
  • Ne mors quidem nos iunget
    • Mostra profilo
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.159
  • Tonno verde
    • Mostra profilo
Re:Non conoscevo Assert
« Risposta #2 il: 03 Marzo 2020, 17:37:15 »
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro