Ravaut123
Ravaut123

Reputation: 2808

Faster code If/then

I found some code like

procedure UpdateComp(Sender: TObject)
begin
  if Sender = edtA then
  begin
    ...  //to something
  end;
  if Sender = edtB then
  begin
    ...  //to something
  end;
  if Sender = edtC then
  begin
    ...  //to something
  end;
  ...
end;

The if/then in the code is for more than 50 statement Each time is the sender match with a component on the form ones. Is it then logic to change the code like:

procedure UpdateComp(Sender: TObject)
begin
  if Sender = edtA then
  begin
    ...  //to something
  end
  else if Sender = edtB then
  begin
    ...  //to something
  end
  else if Sender = edtC then
  begin
    ...  //to something
  end;
  ...
end;

Or I am not correct to change this way?

A piece of the code I find on the way:

procedure TContactController.UpdateComponent(Sender: TObject);
var
  I: Integer;
begin
  UpdateComponentActive := True;

  If Sender = pnlBasicInformation
  then begin
    pnlBasicInformation.Caption := UpperCase(Trim(Format('%s %s', [CurrentContact.Name, CurrentContact.FirstName])));
    If CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId] <> ''
    then pnlBasicInformation.Caption := Format('%s - %s',
                                               [pnlBasicInformation.Caption,
                                                UpperCase(CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId])]);
    pnlBasicInformation.Caption := StringReplace(pnlBasicInformation.Caption, '&', '&&', [rfReplaceAll]);
  end;

  If Sender = gbtnLock
  then begin
    If CurrentContact.Locked
    then gbtnLock.ImageIndex := 7
    else gbtnLock.ImageIndex := 19;
  end;

  If Sender = edtInternalReference
  then edtInternalReference.Text := CurrentContact.InternalReference;

  If Sender = edtExternalReference
  then edtExternalReference.Text := CurrentContact.ExternalReference;

  If Sender = edtFirstName
  then edtFirstName.Text := CurrentContact.FirstName;

  If Sender = edtName
  then edtName.Text := CurrentContact.Name;

  If Sender = edtSubName
  then edtSubName.Text := CurrentContact.SubName;

  If Sender = cbContactFunction
  then begin
    cbContactFunction.ItemIndex := -1;
    For I := 0 to cbContactFunction.Items.Count-1 do
    begin
      If TContactFunction(cbContactFunction.Items.Objects[I]).Id = CurrentContact.ContactFunctionId
      then begin
        cbContactFunction.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = cbLanguage
  then begin
    cbLanguage.ItemIndex := -1;
    For I := 0 to cbLanguage.Items.Count-1 do
    begin
      If TLanguage(cbLanguage.Items.Objects[I]).Id = CurrentContact.LanguageId
      then begin
        cbLanguage.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = cbSalutation
  then begin
    cbSalutation.ItemIndex := -1;
    For I := 0 to cbSalutation.Items.Count-1 do
    begin
      If TSalutation(cbSalutation.Items.Objects[I]).Id = CurrentContact.SalutationId
      then begin
        cbSalutation.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = edtCallingCodeMobilePhone
  then edtCallingCodeMobilePhone.Text := CurrentContact.CallingCodeMobilePhone;

  If Sender = edtMobilePhone
  then begin
    edtMobilePhone.Text := CurrentContact.MobilePhone;
    edtMobilePhone.OnKeyPress := OnPhoneNumberKeyPress;
  end;

  If Sender = gbtnMobilePhone
  then gbtnMobilePhone.Enabled := Trim(CurrentContact.MobilePhone) <> '';

  If Sender = gbtnMobilePhoneSms
  then gbtnMobilePhoneSms.Enabled := Trim(CurrentContact.MobilePhone) <> '';

  If Sender = edtBirthDate
  then edtBirthDate.Text := Format('dd/mm/yyyy', [CurrentContact.BirthDate]);

  If Sender = rbGenderM
  then rbGenderM.Checked := (CurrentContact.Gender = 0);

  If Sender = rbGenderV
  then rbGenderV.Checked := (CurrentContact.Gender = 1);

  If Sender = edtIdentityCardNumber
  then edtIdentityCardNumber.Text := CurrentContact.IdentityCardNumber;

  If Sender = edtNationalNumber
  then edtNationalNumber.Text := CurrentContact.NationalNumber;

  If Sender = imgProfilePhoto
  then imgProfilePhoto.Picture.Assign(ProfilePhoto.Picture.Graphic);

  If Sender = gbtnRemovePhoto
  then gbtnRemovePhoto.Enabled := PhotoManager.PhotoExists(pmContact, CurrentContact.Id);

  If Sender = edtRemarks
  then edtRemarks.Text := CurrentContact.Remarks;

  If Sender = edtInfo
  then edtInfo.Text := CurrentContact.Info;

  If Sender = edtRowVersion
  then edtRowVersion.Text := Format('dd/mm/yyyy', [CurrentContact.RowVersion]);

  UpdateComponentActive := False;
end;

therefore I wanted to change the code with if / else as first changes

Upvotes: 0

Views: 202

Answers (2)

Stijn Sanders
Stijn Sanders

Reputation: 36850

A possible alternative would be to set the Tag property of the different edit fields to distinct numbers, and use a case (Sender as TEdit).Tag of-clause. This internally will use a jump-table and avoid having to work through a series of if-statements.

Another possibility, if you feel up for is, is the hard way and implement your own class inheriting from TEdit (or TCustomEdit) and add an extra event-handle just for this exact case where you now call UpdateComp.

Upvotes: 2

Victoria
Victoria

Reputation: 7912

Your proposal is correct and can save you a few CPU instruction executions (if you care that much about performance). But only if all the expressions are for equality. It's because the first code block always evaluates all the expressions (no matter if one evaluates to true) whilst your proposed code does not.

But in this case I would review the code and try to find as much as the controls you work with have in common and group them accordingly (if possible).

Upvotes: 4

Related Questions