Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Who, what, why, but not this

Dave Foti, who manages development of object oriented programming features in the MATLAB language, is back with a discussion of object parameters in methods.

A fairly frequent question from my colleagues and some of you is why MATLAB classes don’t have a keyword like “this” inside methods and support for the other behaviors found in languages with a “this” keyword. Having to declare the name of every object parameter to a method may be a minor inconvenience, but having to prefix properties with that object parameter is much more cumbersome. For example, consider the following Java method:

public int getArea() {
    return Width * Height;
}

In the MATLAB language, such a function would be written as something like:

type Rect/getArea
function A = getArea(R)
    A = R.Width .* R.Height;
end

While the above example looks simpler in Java, there are situations where implicit object arguments get in the way. Many functions such as binary operators work on multiple objects. When a function needs to work on data from multiple objects, languages with implicit object parameters can lead to an asymmetry in handling the different objects. For example, consider a Java intersect method to return the intersection of two input rectangles:

public Rect intersect(Rect B) {
    Rect C = new Rect();
    C.X = max(X, B.X);
    C.Y = max(Y, B.Y);
    C.Width = min(X+Width, B.X + B.Width) – C.X;
    C.Height = min(Y+Height, B.Y + B.Height) – C.Y;
    return C;
}

To restore some symmetry, sometimes I see the following pattern:

public Rect intersect(Rect that) {
    Rect other = new Rect();
    other.X = max(this.X, that.X);
    other.Y = max(this.Y, that.Y);
    other.Width = min(this.X+Width, that.X + that.Width) – other.X;
    other.Height = min(this.Y+Height, that.Y + that.Height) – other.Y;
    return other;
}

However, a MATLAB method naturally accesses all rectangles in the same manner:

type Rect/intersect
function C = intersect(A, B) 
    C.X = max(A.X, B.X);
    C.Y = max(A.Y, B.Y);
    C.Width = min(A.X+A.Width, B.X+B.Width) - C.X;
    C.Height = min(A.Y+A.Height, B.Y+B.Height) - C.Y;
end

Another important consideration for the MATLAB language is operators that work on objects and numbers or different kinds of objects. For example, a Rational number class may provide arithmetic operators that work on any combination of rational values and built-in numeric values.

x = Rational(5,7)
x = 
5/7

y = 3 .* x
y = 
15/7

z = x .* 3
z = 
15/7

t = y .* x
t = 
75/49

The Rational function times is called in all three multiplications above:

type Rational/times
function R = times(X,Y)
    if isa(X, 'Rational')
        if isa(Y, 'Rational')
            R= Rational(X.Numerator .* Y.Numerator, ...
                        X.Denominator .* Y.Denominator);
        else
            if ~isnumeric(Y)
                error('Y must be a number.');
            end
            R = Rational(X.Numerator .* Y, X.Denominator);
        end
    else
        if ~isnumeric(X)
            error('X must be a number.');
        end
        R = Rational(X .* Y.Numerator, Y.Denominator);
    end
end

In this example we can see the value of making all arguments explicit. It allows operators to work on a first double argument and a second Rational argument. It also reinforces the fact that functions inside the class can access private data and methods on any instance of the class. The times method is a method of the Rational class that works on one or more instances of Rational numbers. When it works on two Rational numbers to produce a new Rational result, there is nothing special about any one of these three instances.

MATLAB functions, methods, and variables share the same namespace. Object properties are scoped to the class of the object and their names can be the same as MATLAB keywords. While it would sometimes be more convenient, implicit references to an object parameter would also mean additional restrictions on property names and potential conflicts with function names.

I would be interested in hearing your thoughts on this subject. In your code, what is the relative frequency of methods that work on a single object versus multiple objects? How important is the convenience of implicit object access to you? Would you like a way to declare that a block of methods define access operations on an object and make the object implicit within these methods?




Published with MATLAB® 7.14


  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.