To check arguments or not to check arguments?
Forum » Nvigorate Design / Specific Design » To check arguments or not to check arguments?
Started by: alexexmachinaalexexmachina
On: 1174715791|%e %b %Y, %H:%M %Z|agohover
Number of posts: 5
rss icon RSS: New posts
Summary:
I've reached a point of indecision regarding checking function arguments for null/invalid values.
To check arguments or not to check arguments?
alexexmachinaalexexmachina 1174715791|%e %b %Y, %H:%M %Z|agohover

I've been struggling lately with whether or not to check all the arguments for null or empty string. While many say that it's best practice, I've yet to actually see this done. While it might definitely make the entire framework far more debugging friendly, I wonder if it's worth the extra effort and expense.

Scott Wagner, a .Net Solutions Architect has come up with (what I believe) is a simple enough solution. An argument checker class provides a way to tie delegates to the type of argument to check. Checking the argument is accomplished by calling a static method and providing the argument name and error message in the event that the argument does not pass the check.

For the time though, I will not be trying to incorporate any approach until I've gotten further along in the development of the basic classes. This is partly because at this point, I'm trying to get a good deal of code up to standard and make sure it's passing general unit tests.

I would like to hear some feedback on this matter so please chime in.

unfold To check arguments or not to check arguments? by alexexmachinaalexexmachina, 1174715791|%e %b %Y, %H:%M %Z|agohover
Re: To check arguments or not to check arguments?
mechheadmechhead 1174902861|%e %b %Y, %H:%M %Z|agohover

Yep, argument checking is a big point of argument (whaha, do I get points for bad puns?). From a best practices point of view I'd agree completely, just for the sake of full and utter completeness it would be a good thing.
But from a logical point of view, its just superfluous, in most cases its very logical if an argument is nullable or not. You know, consider a method such as UpdateUserAccount(Account accountObj). I think its rather obvious that you need to pass in a valid object here.

But then again, you are building a FOUNDATIONAL library here, so in NVigorate's case I'm actually in favor of throwing exceptions that cleanly explain why certain arguments may not be null. And by throwing custom exceptions the user of the library is also able to catch these exceptions and act accordingly.
A global exception mechanism for the entire library might be in place here, I'd recommend working with global error indentifiers that refer to string representations in a resource file. That gives you even the opportunity to raise errors in different lannguages :)

As far as implementation goes, maybe its a good idea to create a method Attribute class for this? I have zero knowledge of the whole Attribute side of things, but to me this sounds like a logical route to take in this case.
Does anyone around here have sound experience in creating custom attributes? I think I'll start reading up on that aspect of .NET, should be interesting…

last edited on 1174940250|%e %b %Y, %H:%M %Z|agohover by mechhead + show more
unfold Re: To check arguments or not to check arguments? by mechheadmechhead, 1174902861|%e %b %Y, %H:%M %Z|agohover
Re: To check arguments or not to check arguments?
alexexmachinaalexexmachina 1174953492|%e %b %Y, %H:%M %Z|agohover

Thanks for participating :)

I agree. And actually, I was thinking about globalized error messages today. I agree that it's something we should try to plan for. I've had quite a bit of experience with custom attributes :) Actually, I need to put the latest chunk of code up for Nvigorate. I've finished most of the Reflector stuff I just lack the rest of the NUnit tests.

I'm currently working on 2 things. 1) Finishing up the DataInterface class and testing it to make sure it's at least mostly operational 2) Stubbing out interfaces and the basic implementations for the Relational (used to be Binding) namespace.

Hopefully I can get organized better soon so that anyone wanting to contribute code will actually know what the design is like. I'll post a line on the news page once I get everything uploaded this evening.

unfold Re: To check arguments or not to check arguments? by alexexmachinaalexexmachina, 1174953492|%e %b %Y, %H:%M %Z|agohover
Re: To check arguments or not to check arguments?
mechheadmechhead 1174995401|%e %b %Y, %H:%M %Z|agohover

Hey Alex, looking forward to seeing the new drop of your code.
So do you think a custom attribute could be used for argument checking? I've been reading up a little and must say I'm still a little stumped on what the implementation would look like…
Good luck getting your release out the door!

cheers,

Roland

unfold Re: To check arguments or not to check arguments? by mechheadmechhead, 1174995401|%e %b %Y, %H:%M %Z|agohover
Re: To check arguments or not to check arguments?
alexexmachinaalexexmachina 1175010173|%e %b %Y, %H:%M %Z|agohover

The new code is up (finally). It's nothing near completetion but you can take a look at the parts of the Reflector class that work specifically with custom attributes. Basically, there are ways to combine custom arguments with proxies or when performing call interception to give the proxy/intercepting code a way to know what to check for. However, from what I understand both of these methods are computationally expensive. Unfortunately, I've yet to find anyone who's done comparisons or respectable analysis of the cost, so it may still be feasible? That's something I'd like to look into or have someone else look at (are you interested?) so at least whatever we go with will be the best reasonable solution.

If you're interested in officially joining the project, I've posted openings on the sourceforge.net site. If you can't find them just shoot me an e-mail with your account name to moc.liamg|anihcamxexela#moc.liamg|anihcamxexela. I can add you to the project that way. And if you decide to take on researching something like that I can add some tasks to the plan to track against.

unfold Re: To check arguments or not to check arguments? by alexexmachinaalexexmachina, 1175010173|%e %b %Y, %H:%M %Z|agohover
New post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.