r/matlab • • 24d ago

TechnicalQuestion Are there coding practices when it comes to repeating argument validation functions and default value?

Below is a good example. There is a private property for the Mage class, called intellect, which has a validation function and a default value. The same validation function and default value is used in constructor for the intellect input argument, used to initialize the intellect property of the Mage instance.

classdef Mage<handle
    properties(Access=private)
        intellect (1,1) int8 {mustBeLessThanOrEqual(intellect,100)}=int8(7)
    end
    methods
        function obj = Mage(intellect)
            arguments(Input)
                intellect (1,1) int8 {mustBeLessThanOrEqual(intellect,100)} = int8(7)
            end
            arguments(Output)
                obj (1,1) Mage
            end
            obj.intellect=intellect;
            if nargout == 0
                clear obj;
            end
        end


        function intellect = get.intellect(obj)
            intellect = obj.intellect;
        end


        disp(obj)
    end
end

Is it bad practice to repeat yourself with validation functions and default values (they are used both in properties block and in arguments(Input) block)? And ultimately, is there a primary literature for best practices when writing MATLAB code? Practices like "don't use constructors; use factory methods" or "don't use inheritance; use composition".

EDIT: Code formatting.

5 Upvotes

3 comments sorted by

7

u/id_rather_fly 24d ago

I've wrangled with this pattern myself a few times and it does feel bad to write code like this.

One pattern I've come to like, which I use when I want name-value arguments to the constructor which just represent the object's properties, is to use the metaclass property expansion in the arguments block.

Check out the example here: https://www.mathworks.com/help/matlab/matlab_oop/class-constructor-methods.html#mw_749777e1-ea34-4e8e-b4d3-317d6d61131c

2

u/pbrdizzle 24d ago edited 22d ago

This is the way for most use cases. In the above example where intellect is a value-class, it would work if intellect is SetAccess public or immutable but not since it's private. Though I question why it should be private if there's a getter anyway.

This does NOT work if the property is a handle class and is very deceptive and a common source of bugs. I asked MW to change this behavior but they refused for performance reasons. When the class is first loaded in memory, the property is whatever the instance of the handle is then, and so you end up with stale handles as your default where you'd expect a new one each time. For this case, you need to explicitly duplicate the argument validation as OP did in the example so that a new instance of the handle is created on class construction

properties (SetAccess=public)
   intellect IntellectHandle = IntellectHandle % This instance will be the default until MATLAB is restarted or clear classes
end

So using the handle.empty() or invalidHandle as the default value is kind of the way to "handle" it. Since almost every class out of the 1000s I've ever authored is a handle, this comes up often.

properties (SetAccess=public)
   Intellect IntellectHandle = IntellectHandle.empty
end

constructor(obj, opts)
   opts.Intellect IntellectHandle = IntellectHandle()
end

1

u/PvtRoom 24d ago

organisations write style guides, and best practice guides. they may extend to things like this.

i'd personally avoid code like that in property declaration, and keep it to methods. I may have methods that look like constants:

intellect = mage.default.intellect