Awesome Code of the Day:

Awesome Code of the Day:

procedure TMainForm.mnuItemSecMgrClick(Sender: TObject);
begin
  TfrmSecMgr.Create(Application).ShowModal;
end;

Comments

  1. OnClose -> Action := caFree; // right?

    ReplyDelete
  2. How would you do that in a better way? Why you consider this a bad practice? If you post your solution along with, it would be much more useful.

    ReplyDelete
  3. ERTUNÇ EFEOĞLU I believe the generic rule is "the creator is responsable for freeing", i.e.:
    var
    LForm: TSomeFormClass;
    begin
      LForm := TSomeFormClass.Create(Application|NIL); // create instance
      try
        LForm.ShowModal; // play with the instance
      finally
        LForm.Free; // free it
      end;
    end;

    within 1 screen, you can see the class being created, shown to user and freed, using the "Action := caFree" is OK, but the thing is that your code(see Nick's post) looks like voodoo, it's not really clear where it's being created, played with or freed.

    ReplyDelete
  4. Dorin Duminica Would not Delphi compiler manage what is to be disposed when a variable gets out of scope? I thought such a mechanism existed. Maybe mistaken. I've not been using Delphi professionaly for a long time. Just following the product for emotional reasons :)

    ReplyDelete
  5. Refactoring some of your old code again? ;)  Seriously though, where are you getting these code WTFs?

    ReplyDelete
  6. Maxim Abramovich Thanks, your suggestion is more clear than original code

    ReplyDelete
  7. ERTUNÇ EFEOĞLU you got the answer from Maxim Abramovich, he was faster (:

    ReplyDelete
  8. It is sad that I can't use Delphi professionally. You get rusty when you are not actively using any tool. Thanks both Maxim Abramovich and Dorin Duminica for quick reply.

    ReplyDelete
  9. Kevin Powick Old code from my new job.

    ReplyDelete
  10. Nick Hodges You do use hip-waders, I presume?  ;)

    ReplyDelete
  11. ERTUNÇ EFEOĞLU I'm actually very disappointed that Delphi is not used more widely, there are companies(soft) out there that don't know what Delphi is or even heard of it, can you believe that?
    However, Delphi is used widely for Windows "side-work", quick and dirty but it gets the job done (:

    ReplyDelete
  12. Dorin Duminica As long as 10 years ago, I knew of fresh CS grads refusing Delphi jobs in Silicon Valley because they did not want it on ther resumes. And the jobs they were turning down were 6 figures.

    ReplyDelete
  13. Bill Meyer WOW, currently I do about 99% of my work in Delphi(over 20 projects, a bit less than half are quite big) desktop and web, I feel very lucky (:
    Turning down Delphi(or any other) job seems to me very weird(to say the least), I'm doing any kind of work even the graphics(most devs wouldn't do that...).

    ReplyDelete
  14. Dorin Duminica I agree, it seemed very strange to me that these people would turn down the work, especially so soon after the dot com crash, but they did. And considering (despite their egos) that it would take them 6-12 months to become sufficiently conversant with the very large systems on which they would have been working, incredibly foolish. However, I also think it was a good thing for the company, that these (probable jerks) disqualified themselves.

    ReplyDelete
  15. Bill Meyer I don't think of "work" only, relationships are a reword also, even small jobs can lead to big things, so, turn down something? hell no!!

    ReplyDelete
  16. I'd probably consider this to be acceptable code, provided the form is being freed in OnClose.  But when doing ShowModal it's probably better to explicitly free it.  If you are doing just a Show, then it is often easier to let the form you are showing be responsible for its own memory management.

    ReplyDelete
  17. How about:

    class function TfrmSecMgr.Execute: Boolean;
    var
      F: TfrmSecMgr;
    begin
      F := TfrmSecMgr.Create(nil);
      try
        Result := IsPositiveResult(F.ShowModal);
      finally
        F.Free;
      end;
    end;

    ReplyDelete
  18. There's an easy trick to let automatically free objects as soon as they get out of scope, at least within methods/procedures (but not within classes). I have posted the implementation of such an helper class at http://members.adug.org.au/2011/12/05/smart-pointers/ (see unit Core.Classes.ObjectGuard;).

    ReplyDelete
  19. PS: The helper class is also to be found within my ToolBox (an extract of some generic classes and stuff) which can be found on my website: http://ddobjects.de/free-software

    ReplyDelete

Post a Comment