Partial class implementation

Robert Fraser fraserofthenight at gmail.com
Thu Jul 19 23:18:31 PDT 2007


Hi Joel,

This is quite the interesting thread; these are the sort of issues I'd like to confront before I start on a larger D project.

> Yes this is a reasonable example.  The use of getters/setters is useful 
> for restricting use of the data to prevent bugs.  Its also useful for 
> changing the integrals of the object but keeping the same interface. 
> For instance if you decide that you wish to change your internal matrix 
> representation to a quaternion and a vector.
> 
> A great thing about D (and C#) is that you can add them properties later.

Indeed, my company style guide requires that getters/setters exist for all variables, most of which are private and used internally by other methods of the class. These and the constructors are the only methods allowed to refer to the variables directly. Personally, I find this going _way_ overboard (plus, the class internals start to get out of sync if legacy properties are supported for non-existent fields), but it's actually saved me once or twice.

> 4) When you inherit from a class if you have a lot of publics that 
> should be free functions then your going to have to maintain them in the 
> child class as well. (I also recommend not using inheritance for reuse, 
> but for polymorphic type operations.)

Agreed. IMO, composition is almost always preferable to inheritance if virtual dispatch is not necessary.

> This walking-the-OO-line a well known C++ verse Java debate.  I can't 
> say I side with the Java side because most of the opponents haven't used 
> C++ at least in this way (the same can't be said about C++ programmers).
> 

It's true I haven't used C++ outside a couple projects for school. I have used C before (since the tender age of 12; it was the first language I learned), and found the organizational benefits offered by OO very compelling. But since I haven't used a strongly-typed language supporting both free functions and class methods for any appreciable project (well, I'm working on one medium-sized one in D right now using only classes so far), I don't really know what I'm talking about. So I'm willing to give this whole free-function thing a try and see how it goes. Thanks for walking me through all that.

> Its also a common interview question.

So am I supposed to say "OO all the way!" or "free functions need love, too"?

- Fraser



More information about the Digitalmars-d mailing list