1

Declaring members as published has advantages over public:

  • Ability to read/write the member in the Object Insepoctor of the IDE
  • RTTI and its uses

So is there ever a benefit to declaring members public instead of published? Does published have any downsides? Or should I always declare members published, as a rule?


PS: Not a duplicate; I read that question and all its answers prior to posting my question. The "possible duplicate" question explains the difference between the two keywords, but doesn't gives guidance when either should be used, or the advantages/disadvantages of using either.

Community
  • 1
  • 1
DBedrenko
  • 4,871
  • 4
  • 38
  • 73
  • 8
    I'd say you should do it vice-versa. Declare them as `public` and `publish` just when needed (not always you want to have the property in OI, nor always you use old kind of RTTI). – TLama Aug 19 '15 at 14:48
  • @TLama Why wouldn't you want it in the Obj Inspector? It's visibility is public, it's in the API, there is some contract established with the outside world, so why wouldn't you want to display all visible members in the Inspector? – DBedrenko Aug 19 '15 at 14:53
  • 4
    Because there are properties that are only runtime, like e.g. `Handle`, `Parent`, `MouseInClient` and similar. Not everything makes sense to be in OI. That's why I would consider thinking vice-versa. – TLama Aug 19 '15 at 15:04
  • possible duplicate of [What's the difference between public and published class members in Delphi?](http://stackoverflow.com/questions/3157648/whats-the-difference-between-public-and-published-class-members-in-delphi) – Sam M Aug 19 '15 at 19:37
  • 2
    @SamM: ISTM that the difference is known, but not necessarily the implications. – Rudy Velthuis Aug 19 '15 at 19:39
  • @SamM I read that question and all its answers prior to posting my question. The question you linked explains the difference but not necessarily gives guidance when either should be used, or the advantages/disadvantages. Rudy is similarly right. – DBedrenko Aug 19 '15 at 20:08

1 Answers1

5

Declaring as published imposes a storage cost because the size of your executable is swollen to contain the RTTI. Declaring as public avoids that cost. It's unlikely that would ever matter, especially given the immense size of modern Delphi executables, containing as they do huge amounts of code that never even executes.

For components that can be edited in the Object Inspector, the difference between public and published is more significant. As you say, this is how a component determines which properties are visible in the Object Inspector. Some properties should be visible there, others not. User "TLama" gives fine examples of properties that need to be public, but should not be editable in the Object Inspector: Parent, Handle and so on.

Beyond that it comes down to opinion. It's entirely up to you to decide what to do.

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490