Gambas-it

Gambas3 => Programmazione => Topic aperto da: Gianluigi - 02 Marzo 2020, 19:56:39

Titolo: Non conoscevo Assert
Inserito da: Gianluigi - 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:
Titolo: Re:Non conoscevo Assert
Inserito da: vuott - 03 Marzo 2020, 12:28:17
...proviene dalla libreria standard del C:

http://man7.org/linux/man-pages/man3/assert.3.html
https://it.wikipedia.org/wiki/Assert.h
https://www.programmingsimplified.com/c/source-code/assert
https://stackoverflow.com/questions/1571340/what-is-the-assert-function
Titolo: Re:Non conoscevo Assert
Inserito da: Gianluigi - 03 Marzo 2020, 17:37:15
...proviene dalla libreria standard del C:

http://man7.org/linux/man-pages/man3/assert.3.html
https://it.wikipedia.org/wiki/Assert.h
https://www.programmingsimplified.com/c/source-code/assert
https://stackoverflow.com/questions/1571340/what-is-the-assert-function

 :ok: Ottimo

... e giustamente va rimarcato che serve durante la creazione del codice per il suo debug.

 :ciao: