Jump to content
Mike Torrettinni

How do you organize units, forms?

Recommended Posts

A while ago I started organizing new forms, units to group and sort some methods, so I can quickly browse through methods and go where I need to go ( CTRL+click). Here is example how I organize small forms:

 

- I like to group methods as: Form methods, Controlx methods and Other methods (these are also sorted alphabetically):

- I also sort public methods

 

TfrmProjectActivation = class(TForm)
    lblProjID: TLabel;
    edtProjId: TEdit;
    lblActivationID: TLabel;
    edtActivationID: TEdit;
    btnClose: TButton;
    btnActivate: TButton;

    // Form methods
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);

    // Controls methods
    procedure btnActivateClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);

    // other methods
    procedure Activate;
    procedure InitializeURL;

  private
    { Private declarations }

    type
      TProjActType = (paSingle, paMultiple);

    var
      fActivationType: TProjActType;
      fProjID: integer;
      fURL: string;

  public
    { Public declarations }

    property ProjActivationType: TProjActType write fActivationType;
    property ProjID: integer write fProjID;
  end;

 

I don't organize the actual code, just definitions.

To me, this is useful and feels organized.

 

Does anybody else do this? Anybody can share any thoughts on such organization, does it have any disadvantages or can share something better?

 

Share this post


Link to post
Posted (edited)

One of the first things I do is delete those annoying comments the IDE inserts: Private/Protected etc. declarations.

Apart from that I sometimes use the GExperts sort expert to sort the methods inside a section alphabetically. But normally I just let the IDE put them in whatever order it uses.

Edited by dummzeuch
OMG, how many typos did I make?
  • Like 3

Share this post


Link to post

I did something similar in the past but have stopped since it does not work that well if you let the IDE's code completion or a tool like Modelmaker Code Explorer (which I simply cannot live without) create the methods for you. I find it more useful to stick to a good naming convention for methods and event handlers and also use MMX or the structure view to navigate around a unit.

  • Like 5

Share this post


Link to post

I just started looking into MMX for cyclic usage, not for anything else. I only use Structure view for Forms, not for code... interesting, will give it a try. Thanks for the tip!

Share this post


Link to post

Agree with @PeterBelow, good naming conventions + GExpert's Procedure List (or CNPack's counterpart) + MMX are enough to achieve quick method navigation.

  • Like 1

Share this post


Link to post
21 hours ago, Mike Torrettinni said:

I just started looking into MMX for cyclic usage, not for anything else. I only use Structure view for Forms, not for code... interesting, will give it a try. Thanks for the tip!

In my own code, and in code which I inherit from other sources, I like to use MMX to sort classes. Unfortunately, I work with a good deal of legacy code which is in source control, and applying class sorting would wreak havoc there.

  • Like 2

Share this post


Link to post
9 minutes ago, Bill Meyer said:

Unfortunately, I work with a good deal of legacy code which is in source control, and applying class sorting would wreak havoc there.

Amen! Anyway, sorting is just something that pleases the eyes. I only keep the methods sorted within their classes. Anything else I find via Ctrl+f or Shift+Alt+s.

  • Like 1

Share this post


Link to post
3 minutes ago, Sherlock said:

Amen! Anyway, sorting is just something that pleases the eyes. I only keep the methods sorted within their classes. Anything else I find via Ctrl+f or Shift+Alt+s.

Agreed. And it can be sorely tempting in that legacy code, especially in a class which is painfully large and ought to be refactored. But working in a group, it would be pretty hostile to apply the sort. 😉

  • Like 1

Share this post


Link to post
2 hours ago, Sherlock said:

Amen! Anyway, sorting is just something that pleases the eyes. I only keep the methods sorted within their classes. Anything else I find via Ctrl+f or Shift+Alt+s.

When I open a unit or form after a while, I go for FormCreate and FormShow first, to see what is part of initialization and visual setup. So, I like to know these methods are at the top. 

Share this post


Link to post
On 7/7/2019 at 1:27 PM, dummzeuch said:

One of the first things I do is delete those annoying comments the IDE inserts: Private/Protected etc. declarations.

Apart from that I sometimes use the GExperts sort expert to sort the methods inside a section alphabetically. But normally I just let the IDE put them in whatever order it uses.

Didn't know that GExperts implements such functionality. 

Does it sort function implementation too ?

Share this post


Link to post
5 hours ago, Mahdi Safsafi said:

Didn't know that GExperts implements such functionality. 

Does it sort function implementation too ?

No, it only sorts lines alphabetically in ascending or descending order. Optionally it ignores prefixes like "function" or "procedure". It does not work if the declaration spans multiple lines.

Share this post


Link to post

I prefer to have FormCreate, FormActivate, FormClose, FormDestroy, and related stuff at the top so I can see the main flow. 

 

One practice I've seen used in many projects is to have a static procedure at the top that's used to create the form dynamically and initialize things.

 

Otherwise, I try to group methods that are related, so if there are several methods related to a ListView, they'll be grouped close together.

 

Sorting alphabetically can be annoying, especially when people stick a 3-letter prefix on method names to say something about the type of control they're for.

  • Like 1

Share this post


Link to post
2 hours ago, David Schwartz said:

I prefer to have FormCreate, FormActivate, FormClose, FormDestroy, and related stuff at the top so I can see the main flow. 

 

One practice I've seen used in many projects is to have a static procedure at the top that's used to create the form dynamically and initialize things.

 

Otherwise, I try to group methods that are related, so if there are several methods related to a ListView, they'll be grouped close together.

 

Sorting alphabetically can be annoying, especially when people stick a 3-letter prefix on method names to say something about the type of control they're for.

Good to know I'm not the only that finds this order useful! 🙂

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×