Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which are the advantadges of adding a use clause for a unit in the implementation section?

Tags:

delphi

If I put a unit in the uses clause of the implementation section of a unit, the identifiers declared in such unit are not available to the interface section.

What is the advantage of doing that and not being able to use identifiers from the referred unit in the interface?

Is there any practical advantage (such as avoiding unwanted side effects) if you bother to add used units in the implementation section instead of simply doing it the interface section?

like image 420
Pep Avatar asked Aug 02 '13 16:08

Pep


People also ask

Which of the following are benefits of code contracts?

Improved testing: Code contracts provide static contract verification, runtime checking, and documentation generation. Automatic testing tools: You can use code contracts to generate more meaningful unit tests by filtering out meaningless test arguments that do not satisfy preconditions.

Which clause is used to explain the backdrop of an agreement?

This Precedent background clause (or recitals) can be used to explain the background to, and purpose of, an agreement.

Why we use Repository pattern in MVC?

Why the repository pattern is used? The main purpose of the repository pattern is to isolate the data access layer and business logic.In Asp.Net MVC model is used to interact with the Data Access layer and Controller for performing Data access operation. The controller is responsible for passing data to the view.

What is the idea behind conflict driven clause learning Mcq?

In computer science, conflict-driven clause learning (CDCL) is an algorithm for solving the Boolean satisfiability problem (SAT). Given a Boolean formula, the SAT problem asks for an assignment of variables so that the entire formula evaluates to true.


2 Answers

Adding a unit to the uses clause of the implementation section allows that unit to be a private dependency only to the implementation, not to the interface. If UnitA uses UnitB, but no one outside of UnitA cares whether UnitA uses UnitB, because UnitA's interface does not use UnitB, then why advertise the dependency and clutter the interface? Also, if you ever need to remove UnitB and/or replace it with something else, declaring it in the uses clause of the implementation section avoids an interface change that would affect any unit that is using UnitA.

like image 78
Remy Lebeau Avatar answered Oct 20 '22 05:10

Remy Lebeau


The biggest issue is that uses in the interface section can lead to circular dependencies and compilation failure. If unit A uses unit B in the interface section, then unit B cannot use unit A in its interface section.

So, you are often forced into putting at least some uses into the implementation section.

Otherwise I personally prefer to put unit uses into the interface section if at all possible. The main reason being one of scoping and hiding. If there are name scoping clashes (two units define the same name, and the second use hides the first) then rather the same name was in scope throughout the entire unit.

like image 38
David Heffernan Avatar answered Oct 20 '22 04:10

David Heffernan