Is defining get/set methods for every field overkill?

Dukc ajieskola at gmail.com
Thu Nov 17 09:52:11 UTC 2022


On Thursday, 17 November 2022 at 04:39:35 UTC, thebluepandabear 
wrote:
> I am debating whether or not I should add getter methods to 
> these properties. On one hand, it will inflate the codebase by 
> a lot, on the other hand -- in other languages like Java it is 
> a good practice

D has far less need for getters/setters than Java or C++. The 
reason is [Uniform Function Call 
Syntax](https://ddili.org/ders/d.en/ufcs.html). This means that a 
member of a `struct` or `class` can start out as a normal field 
and be later converted to getter/setter if needed, without 
breaking calling code.

You still might want to use setters when you want to be extra 
conservative (client code taking address of a struct field will 
still break if your getter replacing it can't return by `ref`), 
but for the vast majority of purposes that is an overkill IMO.


More information about the Digitalmars-d-learn mailing list