Sealed classes - would you want them in D?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri May 11 20:22:52 UTC 2018


On Fri, May 11, 2018 at 02:04:34PM -0600, Jonathan M Davis via Digitalmars-d wrote:
> On Friday, May 11, 2018 19:45:10 rumbu via Digitalmars-d wrote:
[...]
> > The first example is unit testing. Having access to the private
> > members of a class inside the same module is a mistake because it
> > breaks the idea of encapsulation. Unit testing must be done
> > exclusively on public members of a class. If you are feeling the
> > urge to test a class private thing, there is something wrong with
> > your class design. In the parallel world of true OOP which D tries
> > to avoid as much as possible there is a saying for that: "Everytime
> > you test a private method, a unicorn dies".
> 
> I completely disagree with this. Testing private functions can be
> extremely valuable for ensuring that everything within the type
> functions the way that it should and can often lead to much better
> testing of how pieces of a type work, because you have better control
> over what's going on at that exact point in the call chain when you're
> testing it directly.

Yeah, in my own projects I have found that unittesting private functions
has been a life-saver in catching bugs early (especially if you're
writing a complex class / struct / type / module with many moving
parts), ensuring helper functions actually work, and also building
confidence that the code is actually doing what you think it's doing.
Testing only the external API means I have to write a ton of code before
a single test can be run, which means a lot more places for bugs to hide
and a lot more time wasted trying to locate a bug buried deep within
several levels of function calls under the public API.

If it's true that a unicorn dies everytime I test a private method, then
I say, kill 'em all off, they deserve to go extinct!

(None of this negates the fact that public APIs need to be thoroughly
tested, of course.  Like Jonathan, I just don't see how one could argue
that private methods should *not* be tested.)


T

-- 
Study gravitation, it's a field with a lot of potential.


More information about the Digitalmars-d mailing list