Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!n39g2000hsh.googlegroups.com!not-for-mail
From:  "G.A.M." <x0Zero@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: ML Classes - What is the right way to construct classes?
Date: Mon, 17 Sep 2007 15:47:54 -0000
Organization: http://groups.google.com
Lines: 40
Message-ID: <1190044074.433443.295080@n39g2000hsh.googlegroups.com>
NNTP-Posting-Host: 24.129.101.179
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1190044074 13014 127.0.0.1 (17 Sep 2007 15:47:54 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 17 Sep 2007 15:47:54 +0000 (UTC)
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: n39g2000hsh.googlegroups.com; posting-host=24.129.101.179;
Xref: news.mathworks.com comp.soft-sys.matlab:428922



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.

After writing this one class, I became concerned that this approach
introduces tremendous code maintenance challenges. What am I
misunderstanding? Is there a way to us OO concepts in ML without such
tedious and error-prone code? Or is it better to just not take the OO
approach?

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.

Thanks