Algorithms should be free from rich types
bachmeier
no at spam.net
Fri Jun 30 19:09:23 UTC 2023
On Friday, 30 June 2023 at 16:33:31 UTC, H. S. Teoh wrote:
> Private does have its uses: to hide implementation details from
> unrelated parts of the code so that, especially in a large
> project with many contributors, you don't end up with
> accidental dependencies between parts of the code that really
> shouldn't depend on each other.
That can never happen if you have to explicitly override
something that's been marked private - it's an intentional
dependency.
> The other side to this, however, is that deciding what should
> be private and what shouldn't is a hard problem, and most
> people either can't figure it out, or can't be bothered to put
> in the effort to get it right, so they slap private on
> everything, making it hard to reuse their code outside of the
> narrow confines of how they initially envisioned it.
It's worse than that. Saying something is private is used as a
substitute for documenting or even commenting the code.
> So you end up with an API that covers the most common use cases
> but not others, which causes a lot of frustration when
> downstream code wants to do something but can't via the API, so
> they have to resort to copy-pasta or breaking private. (See:
> API design is hard.)
It's hard not because you don't know what others need, but
because you're marking stuff private and there's no way for
anyone else to override that decision.
One of the many examples related to the project I just released
is the R shared library. The developers have not exported most of
the functionality of the library. So when other developers
created the Matrix package (now installed by default) for greatly
expanded matrix types and operations, they had to resort to
copying and pasting large amounts of C code for no obvious
reason. Now there are two copies of all that code floating
around, but they're probably out of sync. And as I noted above,
private means the code is not documented or commented, so who
knows if that hasn't resulted in bugs in hard-to-catch edge cases.
I agree with the existence of private. In some cases, strictly
enforcing privacy is a good thing (though you can't prevent copy
and paste). It's difficult to justify the absence of a simple
override mechanism.
Where it gets really frustrating is when you've invested time
getting to 95% of what you need. You're at the point where it
almost works, but arbitrary decisions about private mean you'll
never be able to achieve 100% of what you need.
More information about the Digitalmars-d
mailing list