Over the years, I have had the question of where to put uses list entries a lot.
Last year, there was one again from a very experienced developer: [WayBack] Where do you place your unit uses? Over the years, I’ve come to preferring to place my uses in the Interface section only, even if its types, constants… – Lars Fosdal – Google+
The answer is really simple, and comes down to this:
- use only the units you need (Law of Demeter)
- use the units as close as possible to where you need them (this helps Minimizing Scope which is related to Information Hiding and the Proximity Principle)
Besides these Clean Code and Code Complete arguments, there is another very important argument:
The larger the scope of a unit, the more resources it takes to compile your project.
This gets worse when you have cycles in your unit dependencies.
I think it gets more than progressively worse; I have seen ~5 million line projects use close to 2 gigabytes of RAM during compilation when they had deep/long cyclic dependencies, forcing a full project build with DDevExtensions configured correctly in order to avoid out-of-memory at all.
For the above question, the poll seems to indicate the public at large gets it right:
References
- [WayBack/Archive.is] Proximity Principle | Software architecture, design and coding
- [WayBack] Summary of ‘Clean code’ by Robert C. Martin · GitHub (or [WayBack] pastebin)
- Law of Demeter – Wikipedia
- [WayBack] 10.4. Scope | Code Complete: A Practical Handbook of Software Construction, Second Edition
A few tips from the thread:
Just a note: I use the Uses Report in Peganza Pascal Analyzer to identify the candidates for moving to the interface section. The task itself is easily done with Ctrl-Alt-Shift-Up (or Down) from MMX Code Explorer, which moves the unit under the cursor to the other section.
By the way I highly recommend this brilliant expert:
github.com – rfrezino/RFindUnit
+Lars Fosdal Use a proper static analysis tool on a regular basis. The Unit Dependency Analyzer as part of MMX Code Explorer can tell you that pretty fast.
–jeroen