Command–query separation principle [re: @mustuse as a function attribute]

mw mingwu at gmail.com
Wed Oct 19 05:41:19 UTC 2022


D does not strictly follow DbC, and has many legacy features from 
Java/C++ world, and many libraries does not follow command query 
separation principle at all. Also there is no compiler 
enforcement. That's the current status, we have to accept that.

In theory:

paths.remove(i);  // is just a command, should not return 
anything, and the `paths` object itself should have been 
modified, which is the OP author's expectation.

However, for whatever reason (implementation performance concern 
maybe?) the `remove()` method right now returns(!) a new object, 
which actually is the new `paths` object with the updated state, 
so the current correct way to use it is:

paths = paths.remove(i); // works - what I erroneously thought 
the previous line was doing


So the DIP to add @mustuse as a function attribute is a remedy to 
D's not being strictly follow DbC and the command query 
separation principle. At least it forces the programmers do not 
ignore the important return value as in the above example.

I really cannot see why @mustuse as a function attribute got 
rejected.

DbC is a seemingly simple concept, but actually it's deeper than 
you often thought, it affects many details of language design.



More information about the Digitalmars-d mailing list