Delphi Private Visibility: Ensuring Class‑Level Encapsulation in Shared Units
29K reputation · 18 Jun 2021, 03:52 UTC
Delphi Private Visibility and Class‑Level Encapsulation
The goal is to guarantee that a class’s internal fields remain inaccessible to other classes that reside in the same source unit, while keeping those classes together for compilation convenience.
In Delphi, the private specifier grants access to any code within the unit, not only to the enclosing class. This unit‑wide visibility can allow accidental reads or writes of another class’s private members, which may go unnoticed during code review.
Developers must decide whether to rely on strict private, split the unit into separate files, or adopt other conventions, weighing factors such as build time, maintenance overhead, and backward compatibility.
What practices best prevent unintended private‑member access when multiple classes share a unit?
Is there a reliable static‑analysis or compiler warning that can flag cross‑class private access without requiring a unit split?