Path: news.mathworks.com!newsfeed-00.mathworks.com!panix!bloom-beacon.mit.edu!llnews!53ab2750!not-for-mail
Newsgroups: comp.soft-sys.matlab
Subject: Re: ML Classes - What is the right way to construct classes?
References: <1190044074.433443.295080@n39g2000hsh.googlegroups.com>
From: Peter Boettcher <boettcher@ll.mit.edu>
Message-ID: <muymyvl9s8z.fsf@G99-Boettcher.llan.ll.mit.edu>
Organization: MIT Lincoln Laboratory
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (gnu/linux)
Cancel-Lock: sha1:mB/CyolZxsOhook5+UGDmGzZzUY=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 68
Date: Mon, 17 Sep 2007 12:22:20 -0400
NNTP-Posting-Host: 155.34.163.114
X-Complaints-To: news@ll.mit.edu
X-Trace: llnews 1190045813 155.34.163.114 (Mon, 17 Sep 2007 12:16:53 EDT)
NNTP-Posting-Date: Mon, 17 Sep 2007 12:16:53 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:428931



"G.A.M." <x0Zero@gmail.com> writes:

> I wrote my first class in Matlab recently. My impression was that I
> had to write an awful lot of repetitive (boilerplate) code. I had a
> struct with over 15 fields. It was very tedious work to make what
> should have been a simple class. Is there a code generator available
> that will write all this code given the field definitions? If not, it
> seems like there should be.
>
> And what about maintaining the class code when I need to change
> existing fields? ... I don't like the idea of having to change code in
> many different places (because it is easy to miss one of those changes
> and end up with a bug).
>
> For example, if I need to change field name, I have to do it in about
> half a dozen places. There are at least two different index.types in
> each of subsref and subsasgn, plus the get and set methods and
> constructor. Plus there are the string names that usually have to
> match the field names (but sometimes differ in case) and that doubles
> the potential for maintenance errors to arise.

I don't think this is unique to MATLAB.  Even if the implementation is
hidden from the interface, changing the implementation requires many
changes.  Think about C++.  If you actually do all the
implementation-hiding, then you have a member myData.  Then you have
getMyData() and const getMyData and setMyData and clearMyData etc,
etc, etc.  To change the name, you now have to change the name
everywhere.

Going to a full-blown class is probably only called for a very few
sorts of objects.  Things that need constructors / destructors.
Things that would benefit operator overloading.  Things that actually
benefit from subclassing and inheritance.

[snip]

> I volunteered to help a friend fix up some old ML code that has global
> variables everywhere and lots of duplicate code. Sometimes what should
> be the same global variable is actually two or more different vars
> with different names! And instead of creating functions, the program
> was built by copy/pasting code from one file to another, often with
> subtle changes. But the program works correctly and I don't want to
> break it as we refactor it. I'm actually enjoying working on the code,
> but I just need some advice on apply OO concepts in ML. For example,
> I'm getting rid of all the duplicate code by creating functions with
> parameters; but without applying OO concepts (such as encapsulation) I
> don't know of a good way to ensure that this refactoring doesn't
> introduce new bugs.

In the same way that it's possible to write OO C code, you can do the
same in MATLAB.  That's what I would suggest.

Use structs to wrap data that refers to the same object.  Refactor
into functions like you're talking about.  If any given function
modifies an object, pass the struct back out again.  This gives you
most the benefits you're looking for, without getting too verbose.

Are you REALLY going to come back to this code later, and decide to
change the underlying data structure?  AND need to do so without
touching the higher-level code?  If not, then you don't need private
data and accessors.  One less thing to break...

Not breaking the code during refactoring is a different problem.  I
don't think MATLAB classes can help you there...

Just my not-so-humble opinion... 

-Peter