Questions about using `assert` in Java
Daohan Qu
quadhier at outlook.com
Mon Jul 17 13:54:13 UTC 2023
Hi Pavel,
> There have been studies looking into correlation between assertion density
> and fault/defect density. Here's one such study: "Assessing the Relationship
> between Software Assertions and Code Quality: An Empirical Investigation".
Thanks and yes! And there are also some more recent studies about this [1, 2].
They all seem to imply that using assert is beneficial.
> That said, Java assertions are a bit anaemic. I wish they generated helpful
> messages automatically (though I realise that it might have security implications).
> Consider these assertions:
>
> * assert x >= 0;
> * assert x && y;
> * assert a == b;
> * assert ( f ? foo() : bar() );
>
> I would like the compiler to transform those into these (respectively):
>
> * assert x >= 0 : x;
> * assert x && y : x + ", " + y;
> * assert a == b : a + ", " + b;
> * assert ( f ? foo() : bar() ) : f;
Interesting, this idea may also apply to assert statements of other languages. :P
[1] https://ieeexplore.ieee.org/document/7194623
[2] https://dl.acm.org/doi/10.1145/3084226.3084259
Regards,
Daohan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20230717/7a084330/attachment-0001.htm>
More information about the core-libs-dev
mailing list