performance - Faster code If/then -


i found code

procedure updatecomp(sender: tobject) begin   if sender = edta   begin     ...  //to   end;   if sender = edtb   begin     ...  //to   end;   if sender = edtc   begin     ...  //to   end;   ... end; 

the if/then in code more 50 statement each time sender match component on form ones. logic change code like:

procedure updatecomp(sender: tobject) begin   if sender = edta   begin     ...  //to   end   else if sender = edtb   begin     ...  //to   end   else if sender = edtc   begin     ...  //to   end;   ... end; 

or not correct change way?

a piece of code find on way:

procedure tcontactcontroller.updatecomponent(sender: tobject); var   i: integer; begin   updatecomponentactive := true;    if sender = pnlbasicinformation   begin     pnlbasicinformation.caption := uppercase(trim(format('%s %s', [currentcontact.name, currentcontact.firstname])));     if currentcontact.homeaddress.postalcode.city.citydescription[applicationmanager.currentlanguageid] <> ''     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   begin     if currentcontact.locked     gbtnlock.imageindex := 7     else gbtnlock.imageindex := 19;   end;    if sender = edtinternalreference   edtinternalreference.text := currentcontact.internalreference;    if sender = edtexternalreference   edtexternalreference.text := currentcontact.externalreference;    if sender = edtfirstname   edtfirstname.text := currentcontact.firstname;    if sender = edtname   edtname.text := currentcontact.name;    if sender = edtsubname   edtsubname.text := currentcontact.subname;    if sender = cbcontactfunction   begin     cbcontactfunction.itemindex := -1;     := 0 cbcontactfunction.items.count-1     begin       if tcontactfunction(cbcontactfunction.items.objects[i]).id = currentcontact.contactfunctionid       begin         cbcontactfunction.itemindex := i;         break;       end;     end;   end;    if sender = cblanguage   begin     cblanguage.itemindex := -1;     := 0 cblanguage.items.count-1     begin       if tlanguage(cblanguage.items.objects[i]).id = currentcontact.languageid       begin         cblanguage.itemindex := i;         break;       end;     end;   end;    if sender = cbsalutation   begin     cbsalutation.itemindex := -1;     := 0 cbsalutation.items.count-1     begin       if tsalutation(cbsalutation.items.objects[i]).id = currentcontact.salutationid       begin         cbsalutation.itemindex := i;         break;       end;     end;   end;    if sender = edtcallingcodemobilephone   edtcallingcodemobilephone.text := currentcontact.callingcodemobilephone;    if sender = edtmobilephone   begin     edtmobilephone.text := currentcontact.mobilephone;     edtmobilephone.onkeypress := onphonenumberkeypress;   end;    if sender = gbtnmobilephone   gbtnmobilephone.enabled := trim(currentcontact.mobilephone) <> '';    if sender = gbtnmobilephonesms   gbtnmobilephonesms.enabled := trim(currentcontact.mobilephone) <> '';    if sender = edtbirthdate   edtbirthdate.text := format('dd/mm/yyyy', [currentcontact.birthdate]);    if sender = rbgenderm   rbgenderm.checked := (currentcontact.gender = 0);    if sender = rbgenderv   rbgenderv.checked := (currentcontact.gender = 1);    if sender = edtidentitycardnumber   edtidentitycardnumber.text := currentcontact.identitycardnumber;    if sender = edtnationalnumber   edtnationalnumber.text := currentcontact.nationalnumber;    if sender = imgprofilephoto   imgprofilephoto.picture.assign(profilephoto.picture.graphic);    if sender = gbtnremovephoto   gbtnremovephoto.enabled := photomanager.photoexists(pmcontact, currentcontact.id);    if sender = edtremarks   edtremarks.text := currentcontact.remarks;    if sender = edtinfo   edtinfo.text := currentcontact.info;    if sender = edtrowversion   edtrowversion.text := format('dd/mm/yyyy', [currentcontact.rowversion]);    updatecomponentactive := false; end; 

therefore wanted change code if / else first changes

your proposal correct , can save few cpu instruction executions (if care performance). if expressions equality. it's because first code block evaluates expressions (no matter if 1 evaluates true) whilst proposed code not.

but in case review code , try find as controls work have in common , group them accordingly (if possible).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -