andykorth / opentk

The Open Toolkit is a low-level C# library that wraps OpenGL, OpenCL and OpenAL. This fork has been superseded by https://github.com/thefiddler/opentk Please direct your attention to that official repository.
71 stars 27 forks source link

Replace IndexOutOfRangeException with ArgumentOutOfRangeException in vector indexers #26

Closed Robmaister closed 10 years ago

Robmaister commented 10 years ago

IndexOutOfRangeException is supposed to only be thrown internally for arrays. Everything else should be throwing ArgumentOutOfRangeExceptions. Even the BCL collections do this.

I copied the Vector3 class out to another project, VS2012's code analysis pointed this out as CA1065

andykorth commented 10 years ago

You might want to submit this to: https://github.com/opentk/opentk also!

Robmaister commented 10 years ago

Oh cool, didn't realize it officially moved to GitHub! I'll definitely submit it there too.

andykorth commented 10 years ago

Yup, it's official and is getting regular updates! Most of the issues I've fixed have already been moved into that branch.

On Nov 22, 2013, at 2:57 PM, Robert Rouhani notifications@github.com wrote:

Oh cool, didn't realize it officially moved to GitHub! I'll definitely submit it there too.

— Reply to this email directly or view it on GitHub.

BrainSlugs83 commented 10 years ago

I disagree with the legitimacy of CA1065 for indexers that take only a single integer as a parameter -- such a parameter IS an index; In such cases you should just suppress the warning. -- In all other cases, using ArgumentOutOfRangeException is the correct approach.

Robmaister commented 10 years ago

This is the approach taken by all the BCL collection classes that have indexers, including List<T>. IndexOutOfRangeException inherits from SystemException, which is provided as a means to differentiate between exceptions defined by the system versus exceptions defined by applications and is only thrown by 3 CIL instructions - ldelem, ldelema, and stelem.

If anything, making this change is just conforming to what users expect of C#'s standard library and other applications.

BrainSlugs83 commented 10 years ago

Yet, this is clearly not the recommended, or standard (even for Microsoft) practice.

Here's something that perhaps _we can_ agree on: In the XML comments for those indexers which now throw ArgumentOutOfRangeException, can you please add some /// <exception cref="" /> markups for the exception types they throw.


Footnotes:

Feel free to disagree with my assessment, but from where I'm standing, it seems like a matter of personal preference.

Frassle commented 10 years ago

Just to add a voice here, I think BrainSlugs84 is accurate with the assessment that it's a matter of personal preference. I can see where Robmaister is coming from, and I wouldn't put up a fight if the maintainers deemed ArgumentOutOfRange preferable over IndexOutOfRange, but as a personal preference I to expect indexer methods to throw IndexOutOfRange.

ArgumentException (and it's descendants such as ArgumentOutOfRangeException), NotImplementedException, and NotSupportedException all inherit from SystemException. Clearly the distinction between System and Application exception is in definition, not use.

Robmaister commented 10 years ago

Yes, more/better documentation is always better.

As for the sample code provided in specification, the actual BitArray class's indexer throws an ArgumentOutOfRangeException instead.

If you look at all the collections in the System.Collections.Generic namespace, in the case of an indexer with a single int as the index, they all throw ArgumentOutOfRangeException. I always try to keep my own code very similar to the BCL.

I do see the point in string's indexer throwing an IndexOutOfRangeException, as that's a data type and not a collection. Vector/Matrix classes can be either, depending on how you look at it.

As for what else I've read online, people seem split between using the two. I've already submitted the pull request to the opentk/opentk fork. If @thefiddler prefers IndexOutOfRangeException, I can either add another commit that changes it back, or delete the pull request and rewrite my branch's history to not change which exception is thrown but keep the other changes I made, then submit another pull request. I'm fine with it either way.

Frassle commented 10 years ago

"I can either add another commit that changes it back, or delete the pull request and rewrite my branch's history to not change which exception is thrown but keep the other changes I made, then submit another pull request. I'm fine with it either way."

I wouldn't spend time on this unless @thefiddler ask. You've already submitted a pull request with it in just leave it. If it's conclusively deemed problematic/confusing/wrong at some point in the future it can be changed. There seems to be consensus on it being personal preference, let's not argue the color of the garden shed over this.

Robmaister commented 10 years ago

Yeah, I'm not going to change it unless @thefiddler prefers IndexOutOfRangeException and asks for me to change it.