Saturday, December 7, 2019

Lost in Units...

If you start a new project or perhaps the POC of your new project, you normally save the mainform and the project file to a sub-directory of your choice.

And then?

How do you name your units or your forms? Do you put your units in sub-directories? Do you have naming conventions?

In the past I used a "w" in front of my unit name if it was a form. (Winform)  And of course the Filename had only 8 chars because pre Windows 95 filenames had this limit...

I hate projects where every unit has it's own sub-directory...

So how can we do it the right way?

With MVVM or I think any other pattern to separate the Forms from the Business-Code you get more units as without or the "Thing" we call RAD.

Comparing RAD with MVVM we get 2 more units (ViewModel, Model) perhaps beyond the Model we get 2 different implementation of a DTO Class. Perhaps one more Unit for a separated Interface-Unit. So in worst case we get 6 units per "View/Form". If it is a From that contains more than one view it gets "worse".

I like to have "View/Form" related units together not all Forms in one sub-dir and the modal or the Viewmodel at a different place.

If you have a project with 200 forms, with MVVM we get something between 600 and 1200 units. This is a mess if you have no strategy for your unit names or where to put the files.

So where is you limit putting ever file in the same directory? For me it's the height of the project tree in the Project-Window. A little bit of scrolling in the tree is fine but if this tree is to big I get lost. So some time in a growing project I have to put files into sub-directories. I hate to do this, because if I need a reference to this units, every unit has to be included into the DPR file or I have to include every sub-directory into my search path.

Let's start with naming.

Since I'm using MVVM I use the "MVVM-Naming" in every project, so

MainForm -> Main.View.pas
Global interfaces -> MyInterfaces.pas
Global settings -> config.pas
Global ifdef -> IFDEF.pas
Application startup -> bootstrap.pas
Application construction -> Composition.Root.pas

Forms are named "XXXX.View.Pas"
ViewModels of course "XXXX.ViewModel.pas"
Models of course "XXXX.Model.pas"

"Just Units" - that are not service related are placed in a sub-Directory ("noPlatform") only if they are not related to a special platform.

Platform related Units I have sub-directories like ("Windows","iOS","Android","OSX","Linux");

So my "normal" Dir-Tree is looking like this:

- Artwork of course for all Icons and Bitmaps especially for FMX!
- BIN as Target to resources
- POC for Proof on concepts
- XE10.* for RTL Source file copies (I always have to change some files/parts)
- Tests for all Unittests.
- tools for all this small tools you need to build this project.

For a smaller list of matching units with the namespaces I had - or we had - an idea at our last delphi-breakfast today.

So please support my feature request :


No comments:

Post a Comment