y = 
Argument validation for class methods
Show older comments
Is there a best practice for validating (or not validating) the 'self' arguments in class method definitions?
Consider:
classdef my_class < handle
properties
A;
end
methods
function obj = my_class
obj.A = 0;
end
function add_to_obj(obj, B)
arguments
obj my_class; %This seems unnecessary
% obj; %Alternately we could omit the 'class' specifier
B (1,1) double;
end
obj.A = obj.A + B;
end
end
end
The class specification of the obj argument seems superflous and should be self-evidently true. Are there performance implications for leaving it in there (i.e., if I'm calling add_to_obj a lot)? Should I just leave out the specifier (essentially bypassing the argument validation)?
Accepted Answer
More Answers (0)
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!