Skip to content

Conversation

@Gounlaf
Copy link
Contributor

@Gounlaf Gounlaf commented May 1, 2024

Description

👋

Theses method doesn't use the result of TryGetValue and expect a non-null result:

public IChannelMoments GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var moments);
return moments;
}

public IChannelPerceptualHash GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var perceptualHash);
return perceptualHash;
}

This one can - explicitly - return a null value:

public IChannelStatistics? GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var channelStatistics);
return channelStatistics;
}

This PR:

  • remove the TryGetValue for the first too method and use index access
  • tests are added accordingly
  • documentation is not (yet) updated (don't know if it's necessary)

If otherwise you prefer to not change the actual behavior (as it might be a breaking change, or just because the nullable returns is the wanted behavior), I will update the PR:

  • add tests
  • add nullable indicator to method return type

Regards.

Copy link
Owner

@dlemstra dlemstra left a comment

Choose a reason for hiding this comment

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

Wondering if we should not change the interface instead? Als a bit surprised this doesn't cause a warning?

/// </summary>
/// <param name="channel">The channel to get the moments for.</param>
/// <returns>The moments for the specified channel.</returns>
public IChannelMoments GetChannel(PixelChannel channel)
Copy link
Owner

Choose a reason for hiding this comment

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

Should we not change the interface here instead and do IChannelMoments?

public class TheGetChannelMethod
{
[Fact]
public void ShouldThrowExceptionWhenChannelDoesNotExist()
Copy link
Owner

Choose a reason for hiding this comment

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

This name is no longer correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup; renamed.

public class TheGetChannelMethod
{
[Fact]
public void ShouldThrowExceptionWhenChannelDoesNotExist()
Copy link
Owner

Choose a reason for hiding this comment

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

Same here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.


public partial class PerceptualHashTests
{
public class TestPerceptualHash : IPerceptualHash
Copy link
Owner

Choose a reason for hiding this comment

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

I will move this to a separate file after merging this PR. Not yet sure where I want to put this. And maybe we could use NSubstitute instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm used to anonymous object that can implements an Interface in PHP or Kotlin 😜

I don't know NSubstitute (this one?), but I could use it instead of this Test class.

{
public IChannelPerceptualHash GetChannel(PixelChannel channel)
{
return null;
Copy link
Owner

Choose a reason for hiding this comment

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

Please use => null instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

using var image = new MagickImage(Files.ImageMagickJPG);
var phash = image.PerceptualHash();

Assert.Throws<NotSupportedException>(() => phash.SumSquaredDistance(new TestPerceptualHash()));
Copy link
Owner

Choose a reason for hiding this comment

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

You can also check the message of the exception like we do in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

@Gounlaf
Copy link
Contributor Author

Gounlaf commented May 3, 2024

I can't rerun the pipeline; it seems stuck like "last time", not because of code, but for some other reasons

@dlemstra
Copy link
Owner

dlemstra commented May 4, 2024

You will probably need to rebase your PR?

@Gounlaf Gounlaf force-pushed the opti-trygetvalue branch from 0c5539c to 4e3cf39 Compare May 4, 2024 09:34
This was referenced Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants