Mostrando postagens com marcador refactoring. Mostrar todas as postagens
Mostrando postagens com marcador refactoring. Mostrar todas as postagens

Refactoring with Class Interceptors and Anonymous Methods

When it comes to database manipulation, there are few tools as convenient and easy to use as Delphi DataSets. Instanciate any TDataset, run a sql query, call the open method and bingo! We have a copy of the records (and a multitude of metadata) in memory, ready to be manipulated.

You may be very familiar with some variation of this code:

procedure Foo;
var
  qry: TSqlQuery;
begin
  qry := TSqlQuery.Create;
  try
    qry.CommandText := 'select some fields from some table';
    qry.Open;
    while not qry.Eof do
    begin
      { some proccessing oncurrent row  }
      qry.Next; 
   end;
   finally
     qry.Close;
     FreeAndNil(qry);
   end;
end;

Simple, isn't?

Yes, but... Well, image maintain a system where there are thousands of functions exactly the same as this, spreaded over hundreds or thousands of units and you will start to feel chills.

Refatoração com Class Interceptors e Métodos Anônimos

Quando assunto é manipulação de bancos de dados, há poucas ferramentas tão práticas e convenientes quanto os DataSets do Delphi. Instacie um dataset qualquer, informe uma consulta sql, chame o método open e bingo! temos uma cópia dos registros do banco (e uma infinidade de metadados) em memória, prontos para serem manipulados.

procedure Foo;
var
  qry: TSqlQuery;
begin
  qry := TSqlQuery.Create;
  try
    qry.CommandText := 'select some fields from some table';
    qry.Open;
    while not qry.Eof do
    begin
      //processa o registro atual..
      //e avança para o próximo
      qry.Next;
   end;
   finally
     qry.Close;
     FreeAndNil(qry);
   end;
end;

Prático e simples, não é?

Pois bem! Image dar manutenção em um sistema onde há milhares de funções exatamente iguais a esta, espalhadas por centenas de units e você começará sentir calafrios.