Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting around circular references in Delphi [duplicate]

Is there a way of getting around circular unit references in Delphi?

Maybe a newer version of delphi or some magic hack or something?

My delphi project has 100 000+ lines of code mostly based on singleton classes. I need to refactor this, but that would mean several months of "circular reference" hell :)

like image 670
Tom Avatar asked Apr 15 '10 12:04

Tom


People also ask

What does circular unit reference mean in Delphi?

F2047 Circular unit reference to '%s' (Delphi)One or more units use each other in their interface parts. As the compiler has to translate the interface part of a unit before any other unit can use it, the compiler must be able to find a compilation order for the interface parts of the units.

Should circular references avoid?

However, as a general practice, there are several reasons why you may want to avoid circular references between objects. Data and graph consistency. Updating objects with circular references can create challenges in ensuring that at all points in time the relationships between objects are valid.

Are circular references OK?

Circular Reference means that your formula is trying to calculate the origin cell. Typically, this is considered an error. However, there are times where this error can actually be useful and you might to want to create a circular reference on purpose.


2 Answers

I've been maintaining close to a million lines of legacy code for the past 10 years so I understand your pain!

In the code that I maintain, when I've encountered circular uses, I frequently have found that they are caused by constants or type definitions in unit A that are needed by unit B. (Sometimes it's also a small bit of code (or even, global variables) in Unit A that is also needed by unit B.

In this situation (when I'm lucky!) I can carefully extract those parts of the code into a new unit C that contains the constants, type definitions, and shared code. Then units A and B use unit C.

I post the above with some hesitance because I'm not an expert on software design and realize there are many others here who are far more knowledgeable than I am. Hopefully, though, my experience will be of some use to you.

like image 185
RobertFrank Avatar answered Oct 13 '22 08:10

RobertFrank


  1. It seems, you have quite serious code design issues. Beside many signs of such issues, the one is the unit circular references. But as you said - you cannot refactor all the code.
  2. Move all what is possible to IMPLEMENTATION section. They are allowed to have circular references.
  3. To simplify task (2), you can use 3d party tools. I would recommend - Peganza Pascal Analyzer (http://www.peganza.com). It will suggest what you can move to the implementation section. As will give you much more hints to improve your code quality.
like image 45
da-soft Avatar answered Oct 13 '22 08:10

da-soft