Wed Oct 17 - Avoiding Code Smells by Walter Bright

Stanislav Blinov stanislav.blinov at gmail.com
Sat Nov 3 21:28:22 UTC 2018


On Saturday, 3 November 2018 at 20:38:29 UTC, unprotected-entity 
wrote:

As has been pointed out several times before, this is a contrived 
example. Allow a simple transformation:

```
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()
     {
         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();
     s.func(); // 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... [except it's 
useless in this case]
}
```

The only difference is that `func` became a member function. And 
now what? You can just as easily "forget" what's in your 
struct/class as in your whole module.


More information about the Digitalmars-d-announce mailing list