Menu

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

#matlab#redditmatlab#intellect#mage#function
Reading 0:00
15s threshold

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?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.