Why to have properties to sort or duplicate arrays ?

Daniel Giddings dgiddings at bigworldtech.com
Thu Feb 1 16:27:04 PST 2007


Pierre Renié wrote:
> Daniel Giddings Wrote:
> 
>> It's a bit of a tradeoff. Clearing an object like that isn't the 
>> intended use, but a possible one. It's just bad design, or bad coding. 
>> Additionally if you want to prevent that happening, set the clear return 
>> type to void. If you want have a bad API design or code, C++ allows you 
>> to do so much more ;-)
> No, I want the best possible APIs and the cleanest codes.

Yes, that can be done through good design. I think the underlying 
question is should the compiler enforce properties be used as such, 
which would require another keyword for the property functions.

>>  From a usability point of view for libraries it allows the API designer 
>> to hook into property changes to validate assignments, proxy values to 
>> other objects and the like. All without having to have ugly setXXX, 
>> getXXX methods for each property. It comes down to how you use the 
>> language feature; if I was designing a library I would use the property 
>> syntax to validate the data, store it differently internally, etc, but I 
>> wouldn't have them do something other than "set or get the property" in 
>> a general sense. It's similar to overloading the arithmetic operators, 
>> it's really bad to do if they are overloaded to completely different 
>> semantics than expected.
> 
> Yes, properties should not do other things than "set or get the property".
> The properties "dup", "sort" and "reverse" do other things. They should not be properties.
> 

They are not properties, they are methods. The D syntax for properties 
allows you to leave off the () when calling the methods, which is bad 
programming style as they aren't intended as properties. The only way 
the compiler could enforce this is to add another keyword for methods 
you desire as properties, which I think is unnecessary.

ie myClass.myMethod is the same as myClass.myMethod()
and myClass.myMethod = 5 is the same as myClass.myMethod( 5 )

The alternative is to remove the property syntax shortcuts entirely and 
go back to setXXX and getXXX which are icky! and lead to unnecessarily 
verbose API's.

> Bill Baxter Wrote:
>> *All* functions and methods in D that take zero or 1 arguments act like properties.
> Does it mean that if I create a method clean() that returns something, it acts like a property?
> I don't want to make bad code. If I want to create a method that returns somthing without argument, how can I do? Will this method act like a property?

if you make a method clean, it can act like a "property" even if it 
doesn't return anything as it is a syntax "shortcut" if you like:

import std.stdio;

class C
{
     void clean()
     {
         writefln( "C.clean" );
     }
}

void main()
{
     C c = new C;
     c.clean; // works, but is bad programming style
              // c.clean() has a clearer meaning
}

this doesn't mean you should remove this feature (or any other) because 
bad programming style or design obfuscates the meaning of the code.



More information about the Digitalmars-d mailing list