Un'altra tecnica molto comoda quando si parla di "defensive programming" è la cosiddetta "programmazione funzionale".

Vediamo lo stesso programma di prima, però adattato a questa nuova tecnica.

Program Programma;

const ErrEmptyStrValue = 1;

function ReadString(Label : String;var S : String;var ErrOut: Integer) : Boolean;
begin
  write(Label+': ');
  readln(S);
  Result := S <> '';
  if Not Result then
    ErrOut := ErrEmptyStrValue;
end;

function GetFirstName(var S: String;var ErrOut : Integer);
begin
  Result := ReadString('First name',S,ErrOut);
end;

procedure GetLastName(var S : String;var ErrOut: Integer);
begin
  Result := ReadString('Last name',S,ErrOut);
end;

var FName,LName : String;
     ErrorOut : Integer;

begin
  if GetFirstName(FName,ErrorOut) then
  begin
     if GetLastName(LName,ErrorOut) then
     begin
        write('First name is '''+FName+' '' and Last name is '''+LName+'''');
     end;
  end;
end.  

Qui adesso abbiamo un programma che controlla che ogni funzione sia "true" prima di proseguire.
Inoltre, dotando le funzioni di un parametro ErrOut, possiamo usare un tipo Boolean e relegare la
gestione dell'errore in altri punti. Nel caso specifico, non gestiamo l'errore nel corpo principale
soltanto perché si tratta di un programma dimostrativo e perché ad ogni modo in un programma di
questa complessità usare la programmazione funzionale è - imho - fuori luogo, oltre che
tremendamente noioso .

Comunque il concetto di base è semplice: valori TRUE o FALSE a seconda dei casi, più una
variabile di supporto per la gestione corretta degli errori in altri punti. Oggi useremmo probabilmente una
eccezione per cose del genere, ma il concetto non cambia poi molto.

Il vantaggio principale di questo stile di programmazione è che sai *sempre* dove stai se qualcosa non và, il che non fà male di sicuro .

Saluti,

Andrea

powered by IMHO 1.2

posted on lunedì 6 giugno 2005 20:14 | Filed Under [ Programmazione generale ]

Comments

Gravatar
# re: Programmazione difensiva(2)
Posted by ReneMaxwell
on 21/02/2011 11:35
You should be really smart guy to make the superior dissertation writing just like this good topic . We like to buy a essay and thesis writing service just about that. I appreciate it!
Gravatar
# re: Programmazione difensiva(2)
Posted by term papers
on 17/09/2011 12:28
I LIKE YOUR BLOG
Gravatar
# re: Programmazione difensiva(2)
Posted by Resume Writers
on 01/11/2011 08:29
Appreciation for sharing, this website is to I too have to help. Very good, I like the way you write down.
Gravatar
# re: Programmazione difensiva(2)
on 03/02/2012 10:04
good info
Gravatar
# re: Programmazione difensiva(2)
on 18/04/2012 06:25
When you like to have your holiday outside the city, in the wilderness, then trust us that you will not find anything more beneficial than Yosemite cabin rentals suggested by this company.
Gravatar
# re: Programmazione difensiva(2)
on 28/04/2012 19:59
I am absolutely satisfied with the amount of fees I need to pay for the satellite Internet proposed by this provider.
Gravatar
# re: Programmazione difensiva(2)
Posted by cocoplove
on 12/06/2012 07:38
Sie hat glatte, dunkle, gepolsterte Kopfbügel, dass beats kopfhörer in ear 35mm-Dias nach unten in einen perfekt sitzenden Kreis, der zu jedem beats billig solo hd kopfhörer der Ohrmuschel klammert, die eingeschlossen sind in einem weichen, lederartigen Material. Auf der Außenseite solo hd beats jeder Hörmuschel ist eine Stahlscheibe mit einem markanten rot, das rechte Ohr Becher von dem ein Teil Ihrer scheint stumm dr dre monster beats in ear geschaltet, wenn sie gedrückt eingraviert. Im Gegensatz zu anderen Headsets, die den gesamten Gehörgang deckt, was Sie wütend schwitzen, von ourbeats durch Dre Online-Shop ist verstellbar und kann sich relativ lady gaga kopfhörer komfortabel.
Comments have been closed on this topic.