The documentation for the lazy_build
feature in Moose has this to say:
Note that use of this feature is strongly discouraged. Some documentation used to encourage use of this feature as a best practice, but we have changed our minds.
However, it does not explain what the reasoning for this is, and either my google-fu is terrible or there is no public explanation for why this is discouraged.
What's the problem with lazy_build
that makes it discouraged today?
This is in Moose::Manual::BestPractices:
Avoid lazy_build
As described above, you rarely actually need a clearer or a predicate.
lazy_build
adds both to your public API, which exposes you to use cases that you must now test for. It's much better to avoid adding them until you really need them - use explicitlazy
andbuilder
options instead.
So what it's saying is that instead of using the property:
has attribute => (
...,
lazy_build => 1, # creates a builder called _build_attribute
);
You should instead be more explicit:
has attribute => (
...,
lazy => 1,
builder => '_build_attribute',
);
As that doesn't implicitly add clearer
and predicate
methods.