What is the 'correct' approach to clean code UI in Delphi Pascal?

150 Views Asked by At

I'm quite new to Delphi Pascal, and i'm not sure if this Question suits SO, but i don't really know where to ask this else.

My issue is, that i try to develop a Delphi Pascal application, that has an UI (nothing too complex, but rather an input mask with many inputs, such as TEdit, TComboBox, etc., aswell as a TVirtualStringTree that is used to display data (none database data for that matter)). Since there are many UI elements, i want to seperate some of the elements into their own units, to avoid overloading a single unit with dozens of procedures and functions handling the UI logic. However, since Delphi Pascal doesn't allow circular references, this doesn't seem straight forward (because i would need access to the original form, which contained the seperated element). I tried working around this, by just passing the said elements as a member into their own respective types in the seperated unit, but when i do this, i always get access violation exceptions (Am i doing something wrong?).

So therefore my question is, how to approach UI code cleanly in delphi pascal?

1

There are 1 best solutions below

2
mait On

Delphi does allow "circular references" to a point. In your main form you can add references to your other units in the interface uses clause. In the other units you reference the main form in the implementation uses clause. You could also do it the other way around. You just can't have references in the interface section for both units/forms.

I also don't think there is a huge problem with dozens of routines that handle the UI, existing in one unit. It comes down to striking a balance between one big file or lots of little files. Which is easier to maintain? Some Delphi source files have many thousands of lines (e.g. System.Classes.pas has more than 20,000 lines). Don't be afraid of big files.