Thread Subject: ML Classes - What is the right way to construct classes?

Subject: ML Classes - What is the right way to construct classes?

From: "G.A.M.

Date: 17 Sep, 2007 15:47:54

Message: 1 of 8

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

Subject: ML Classes - What is the right way to construct classes?

From: Peter Boettcher

Date: 17 Sep, 2007 16:22:20

Message: 2 of 8

"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

Subject: ML Classes - What is the right way to construct classes?

From: "G.A.M.

Date: 17 Sep, 2007 16:35:00

Message: 3 of 8

On Sep 17, 12:22 pm, Peter Boettcher <boettc...@ll.mit.edu> wrote:
> "G.A.M." <x0Z...@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

Peter - those are good tips I can start applying right away. But one
thing puzzles me. In the OO languages I know, I can easily separate
the private field name from the name of the accessor property, so the
situation you cited in your C++ example isn't something that really
occurs. But it does occur in ML. Furthermore, in strongly typed
languages it is easier to tell if the identifier name refers to the
thing you are renaming - hence automated refactoring tools are
possible (and very helpful). When field names are specified as strings
and indentifiers, as in ML, it becomes very tricky to update the code.

I wrote a project for one of my school projects using the OO approach
in C. After that project I had a new appreciation for the features of C
++ and I haven't gone back to C. I'm getting the impression that ML is
more like C where using a true OO approach is pretty painful. I've
learned how to write code in OO languages that is pretty robust and
I'd like to learn how to do the same in ML. But it doesn't seem like
using ML classes is going to be the way to do that.


Subject: ML Classes - What is the right way to construct classes?

From: Peter Boettcher

Date: 17 Sep, 2007 17:45:16

Message: 4 of 8

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

> Peter - those are good tips I can start applying right away. But one
> thing puzzles me. In the OO languages I know, I can easily separate
> the private field name from the name of the accessor property, so the
> situation you cited in your C++ example isn't something that really
> occurs.

You'll have to give me an example of this. I don't understand what
you mean.

> But it does occur in ML. Furthermore, in strongly typed
> languages it is easier to tell if the identifier name refers to the
> thing you are renaming - hence automated refactoring tools are
> possible (and very helpful). When field names are specified as strings
> and indentifiers, as in ML, it becomes very tricky to update the code.

Again, I'm not sure what you mean.

You don't HAVE to use the get/set paradigm if you don't want. You can
write separate getMyData() functions if you really want to. Is that
what you mean? get('mydata') inputs the requested *public* attribute
as a string. How is that different from encoding the public attribute
in the name of the function itself? Neither one needs to relate to
the actual storage field.

> I wrote a project for one of my school projects using the OO approach
> in C. After that project I had a new appreciation for the features of C
> ++ and I haven't gone back to C. I'm getting the impression that ML is
> more like C where using a true OO approach is pretty painful. I've
> learned how to write code in OO languages that is pretty robust and
> I'd like to learn how to do the same in ML. But it doesn't seem like
> using ML classes is going to be the way to do that.

Again, I'll just suggest that you separate the word "OO" into the
component parts. Putting an object into a struct, and grouping it
together with the function that operate on the struct, is OO. Your
code becomes "oriented" on the "objects" in the struct. It sounds
like you are also after other features, like interface control,
inheritance, operator overloading, etc. Just like in C++, doing those
things correctly takes a pile more code, compared to a bare-bones
class with a few public member variables. The latter is quite
appropriate for many applications.

I'm not going to argue that the MATLAB class setup isn't clunky. It
is. That's why I'm recommending that you don't even go there unless
you need complete features for your application.

-Peter

Subject: ML Classes - What is the right way to construct classes?

From: per isakson

Date: 17 Sep, 2007 18:17:08

Message: 5 of 8

 "G.A.M." <x0Zero@gmail.com> wrote in message

> I wrote my first class in Matlab recently.

Have you thought of using "closures" (nested functions
together with function handles) instead? Assignments with
Matlab objects are "by value".
<snip>

> But the program works correctly and I don't want to
> break it as we refactor it.
 
MUNIT (free download) at http://xtargets.com is the best I
have found.
<snip>

/ per

Subject: ML Classes - What is the right way to construct classes?

From: "G.A.M.

Date: 18 Sep, 2007 16:41:23

Message: 6 of 8

On Sep 17, 2:17 pm, "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote:
> "G.A.M." <x0Z...@gmail.com> wrote in message
> > I wrote my first class in Matlab recently.
>
> Have you thought of using "closures" (nested functions
> together with function handles) instead? Assignments with
> Matlab objects are "by value".
> <snip>
>
> > But the program works correctly and I don't want to
> > break it as we refactor it.
>
> MUNIT (free download) athttp://xtargets.comis the best I
> have found.
> <snip>
>
> / per

per- thanks for the link. This looks like one of the most interesting
sites I have seen in my short time using ML. I am particularly
intrigued by this statement:

"Fairly soon after I discovered nested functions I realized the dual
between objects and NF's. An object is really just a set of nested
functions over a common scope."

I'm hoping that this statement holds the key to my original question.
I can see a lot of possibilities in this idea.

I'll take your advice and check out MUNIT. I also believe MTemplates
may be very useful in my quest to write robust ML code.

Furthermore, the site has a tutorial on "Advanced Layout Management
With Handle Graphics" which answers my other question about docking my
own windows.

This is a pretty interesting post too: http://xtargets.com/snippets/posts/show/71

Subject: ML Classes - What is the right way to construct classes?

From: per isakson

Date: 18 Sep, 2007 19:33:20

Message: 7 of 8

> > <snip>
> per- thanks for the link. This looks like one of the most
> interesting sites I have seen in my short time using ML.

ML is **not** Matlab. See

http://en.wikipedia.org/wiki/Metalanguage and
http://en.wikipedia.org/wiki/ML_%28programming_language%29

>I am particularly intrigued by this statement:
>
> "Fairly soon after I discovered nested functions I
> realized the dual between objects and NF's. An object is
> really just a set of nested functions over a common
> scope."

I use this approach extensively and I recommend you to give
it a try. In a post a couple of weeks ago I gave another
couple of references:

Subject: Re: What am I doing wrong here
From: Per Isakson
Date: 02 Sep, 2007 14:33:00

/ per

Subject: ML Classes - What is the right way to construct classes?

From: "G.A.M.

Date: 6 Oct, 2007 02:13:17

Message: 8 of 8

On Sep 18, 3:33 pm, "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote:
> > > <snip>
> > per- thanks for the link. This looks like one of the most
> > interesting sites I have seen in my short time using ML.
>
> ML is **not** Matlab. See
>
> http://en.wikipedia.org/wiki/Metalanguageandhttp://en.wikipedia.org/wiki/ML_%28programming_language%29
>
> >I am particularly intrigued by this statement:
>
> > "Fairly soon after I discovered nested functions I
> > realized the dual between objects and NF's. An object is
> > really just a set of nested functions over a common
> > scope."
>
> I use this approach extensively and I recommend you to give
> it a try. In a post a couple of weeks ago I gave another
> couple of references:
>
> Subject: Re: What am I doing wrong here
> From: Per Isakson
> Date: 02 Sep, 2007 14:33:00
>
> / per


Closure (computer science): http://en.wikipedia.org/wiki/Closure_%28computer_science%29,
2007-09-01
A Way to Create Reusable Tools: http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/
Michael Corvin replied: http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16367
What am I doing wrong here (OO in Matlab):
http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/a849d5ad999c8394/962b1cb5b7d99220
THIS (or SELF) object in matlab:
http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/29e1ccf18a00eea8/1b8583675b327e7e
Objects and Nested Functions in Matlab:
http://xtargets.com/cms/Tutorials/Matlab-Programming/Objects-Nested-Functions.html
NumPy etc: http://www.mathworks.cn/matlabcentral/newsreader/view_thread/134693#345566

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
class per isakson 17 Sep, 2007 14:03:55
oo per isakson 17 Sep, 2007 14:03:55
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com