|
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.
|