Pregunta

I want to know how source file trees are organized in Pascal. From the Language Reference Guide from FPC, it seems that programs and units must be entirely contained in single files (unless the ${INCLUDE} directive is used).

I can think of several ways of organizing my programs:

  • Stick everything in one huge file (really don't like that),
  • Write several big unit files,
  • Write many small unit files (might turn into a spaghetti of dependencies),
  • Write several big units but split them into files using ${INCLUDE}

Using ${INCLUDE} at all seems like a bit of a hack to me, since a proper module system should make it unnecessary. On the other hand, I'm afraid that single-file modules would get big enough to be unwieldy.

How is this usually done in actual projects? Is there some option I have missed?

¿Fue útil?

Solución

If you design your application properly then each UNIT should be a reasonable size and have a single well-defined purpose. Dependencies should be somewhat hierarchical and you can always group any number of closely related UNITs into a library if your application is large enough to warrant it.

Otros consejos

There is absolutely no need to use a single file, nor is there a need to use {$INCLUDE} unless you have a specific need to do so.

I have several Delphi projects that are 100+ separate code units, used as needed with an addition to the appropriate uses clause (interface or implementation sections) where they are needed. You can see this pattern in the FPC source as well:

unit One;

interface
  uses System;

type
  TSomeThing=class(TBaseThing)
  ...
  public
  ...
  private
  ...
  end;

implementation

uses
  Math;

....

Break your source into logical units (eg., a unit for classes that are related to each other, types that are related or dependent on each other, functional units, and so forth). There are many examples of doing so in FP (they follow the same pattern as Delphi's VCL/RTL) to see how this is typically done.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top