Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception in ViewportAdapter constructor if the GraphicsDevice is null #838

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

safoster88
Copy link
Contributor

@safoster88 safoster88 commented Feb 17, 2024

If the viewport adapter is initialized in the constructor of game, GraphicsDevice will be null.

There is no check for this, so the null ref only occurs when trying to call GetViewMatrix().

This PR aims to improve the public API.

@Gandifil Gandifil self-requested a review April 7, 2024 20:21
@Gandifil Gandifil self-assigned this Apr 7, 2024
@Gandifil
Copy link
Collaborator

Gandifil commented Apr 7, 2024

Firstly, thank you for attention!

It's a good idea, but why do you use the custom exception? There is ArgumentNullException.
It can be simply:
if (graphicsDevice is null) throw new ArgumentNullException(nameof(graphicsDevice));

People often use this way, so more easy to understand.

Copy link
Collaborator

@AristurtleDev AristurtleDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @safoster88
Please make the requested changes and then it can be merged.

public abstract class ViewportAdapter : IDisposable
{
protected ViewportAdapter(GraphicsDevice graphicsDevice)
{
if (graphicsDevice is null)
{
throw new NullGraphicsDeviceException($"Attempted to construct {nameof(ViewportAdapter)} but {nameof(GraphicsDevice)} is null");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just throw an ArgumentNullException since the issue here is that the argument is null, not that the graphics device itself is null.

Please use the new method for checking

ArgumentNullException.ThrowIfNull(graphicsDevice);

{
var act = () => new TestViewportAdapter(null);

Assert.Throws<NullGraphicsDeviceException>(act);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After making the suggested change of using ArgumentNullException this should be updated to check for that exception type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This custom exception is not needed. The problem being solved is that the graphics device argument for the method is null, not that the GraphicsDevice itself is null. This can be removed and the exception changed to use ArgumentNullException to better communicate to consumers that the argument being null is the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants