Wed Oct 17 - Avoiding Code Smells by Walter Bright
unprotected-entity
unprotected-entity at gmail.com
Sat Nov 3 20:38:29 UTC 2018
On Saturday, 3 November 2018 at 06:57:50 UTC, Neia Neutuladh
wrote:
>
> We object because the people complaining can't point at a use
> case that seems reasonable. If you provided real-world
> examples, we'd consider them.
>
------
module test;
struct S
{
private uint a;
void setA(uint n)
{
// damn implicit conversions! I just hate them!
if(n != 4294967286)
this.a = n;
else
assert(0);
}
}
void func(ref S s)
{
s.a = -10; // this might be on line 856 in your module.
// there's a good chance, that when you get to
write line 856,
// that you've forgotten why you had written setA,
on line 7.
// i.e. to get around D's outrageous implicit
conversions!
// Even worse, in D, you may never know you mistyped here,
// until things go wrong..
// maybe terribly wrong.
// That's why I made 'a' private, and declared an interface
for using it.
}
// compile with: -unittest -main
unittest
{
S s = S();
func(s); // oh no...why did you do that! no error though.
s.setA(-10); // gee.. if only I could declare 'a' to be:
__private
// then would I know something is wrong.
}
----------
More information about the Digitalmars-d-announce
mailing list