Argument validation for class methods (2024)

Steven Lord on 11 Jul 2024 at 15:51

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#answer_1484428

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#answer_1484428

Open in MATLAB Online

The class specification of the obj argument seems superflous and should be self-evidently true.

Not necessarily. The first input argument to a class method is not always required to be an instance of that class! There are two common patterns where this may not be the case. Let's take a sym object as an example.

syms x

This command calls the plus method for the sym class, as we can tell using the which function.

This also calls the plus method for the sym class, even though 1 is not a sym. It doesn't call the plus defined for double arrays.

z = 1 + x

z=Argument validation for class methods (3)

which('plus(1, x)')

/MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % sym method

which('plus(1, 2)')

built-in (/MATLAB/toolbox/matlab/ops/@double/plus) % double method

Another common pattern is when class precedence is involved. [I suppose the example above simplifies down to this as well, since as stated on that documentation page double is always inferior to classes defined using classdef, which is the case for the sym class.] The graph object in MATLAB has an overloaded plot method that can accept an axes handle as its first input.

g = graph(bucky);

ax = axes;

plot(ax, g)

Argument validation for class methods (4)

Despite the first input to plot being an axes object, MATLAB calls the graph plot method.

which('plot(ax, g)')

/MATLAB/toolbox/matlab/graphics/math/@graph/plot.m % graph method

This is because the graph class states that the axes class (technically matlab.graphics.axis.Axes) is one of its InferiorClasses (as listed in the metaclass information.) So if both a graph object and an axes object are in a method's argument list, regardless of the order in which they're listed in that argument list, we call graph's method.

q = ?graph;

{q.InferiorClasses.Name}.'

ans = 2x1 cell array

{'matlab.graphics.axis.Axes'} {'matlab.ui.control.UIAxes' }

class(ax)

ans = 'matlab.graphics.axis.Axes'

But to get back to your question about performance, measure it and find out! You'll probably want to use timeit or the Profiler as I suspect the impact with and without the validation is going to be so small tic and toc may not capture it. I strongly suspect that unless your method is very fast (returning a constant value) this probably isn't going to be your bottleneck.

3 Comments

Show 1 older commentHide 1 older comment

John on 11 Jul 2024 at 17:36

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#comment_3209068

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#comment_3209068

Open in MATLAB Online

Thanks for your reply and the thorough response. But to play Devil's Advocate, you've partially sidestepped the original question. All of the examples (plus, plot, etc.) do not use arguments declarations for validation. If the method in question is written to support varargin inputs, or like plus are designed with internal calls design to sort out how to manage the input arguments, you're probably not using an arguments declaration anyhow.

I'll also grant that the overhead from doing the validation will be minimal in most applications. It's probably safer to include the validation if you expect users to invoke the method with

foo = my_class;

add_to_obj(foo, 5);

That way you'll get an error thrown during validation rather than a problem further down the line.

As an aside, I really like arguments as I find it much cleaner and succinct than working with inputParser in many situations. It would be useful to have an additional builtin validator, like mustBeEmptyOrA (e.g., allows an empty default, but if it isn't empty is has to satisfy mustBeA). This comes up often enough that it creates a dependency issue when distributing code.

Steven Lord on 11 Jul 2024 at 19:21

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#comment_3209153

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#comment_3209153

I used the plus function / method in my example, but your add_to_obj(obj, B) method looks like it is adding together obj and B, doesn't it? So it could be called plus, couldn't it?

Please contact Technical Support directly using this link to submit an enhancement request for mustBeEmptyOrA. When you do, please describe your use case (especially how the lack of it proves to be a problem when you distribute your code.) The developers benefit greatly when designing new features if they have real-world usage stories to help them consider design trade-offs.

John on 11 Jul 2024 at 19:41

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#comment_3209178

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2136563-argument-validation-for-class-methods#comment_3209178

My example was really just intended as a minimal demonstration. The real applications were quite different. But thanks nontheless. I'll put in a enhancement request, as you suggest.

Sign in to comment.

Argument validation for class methods (2024)

References

Top Articles
Jazmen Jafar Linkedin
Dairy Queen 162 Cake Design
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
Which aspects are important in sales |#1 Prospection
Detroit Lions 50 50
18443168434
Newgate Honda
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Grace Caroline Deepfake
978-0137606801
Nwi Arrests Lake County
Justified Official Series Trailer
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Free Online Games on CrazyGames | Play Now!
Sizewise Stat Login
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Jet Ski Rental Conneaut Lake Pa
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Ups Print Store Near Me
C&T Wok Menu - Morrisville, NC Restaurant
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
University Of Michigan Paging System
Dashboard Unt
Access a Shared Resource | Computing for Arts + Sciences
Speechwire Login
Healthy Kaiserpermanente Org Sign On
Restored Republic
3473372961
Craigslist Gigs Norfolk
Moxfield Deck Builder
Senior Houses For Sale Near Me
Whitehall Preparatory And Fitness Academy Calendar
Jail View Sumter
Nancy Pazelt Obituary
Birmingham City Schools Clever Login
Thotsbook Com
Funkin' on the Heights
Vci Classified Paducah
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 5917

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.