Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

Summary

  • Fixed GetValidFileStreamOrDefault method in FileHelpers.cs to return null instead of throwing InvalidOperationException when file size is not aligned to element size
  • This ensures ReadFirstOrDefault<T> and ReadLastOrDefault<T> methods follow the OrDefault convention of returning default values in unexpected cases
  • Added comprehensive tests to verify correct behavior for misaligned files

Problem

The issue was in the GetValidFileStreamOrDefault method at lines 86-89, where an InvalidOperationException was thrown when the file size was not aligned to the element size. This violated the OrDefault convention where methods ending with OrDefault should return default values instead of throwing exceptions.

Solution

Changed the exception-throwing code to return null, which causes the calling OrDefault methods to return their default values as expected.

Test Plan

  • Existing tests continue to pass
  • Added ReadFirstOrDefaultShouldReturnDefaultForMisalignedFile test
  • Added ReadLastOrDefaultShouldReturnDefaultForMisalignedFile test
  • All tests pass successfully
  • Project builds without errors

🤖 Generated with Claude Code


Resolves #51

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #51
@konard konard self-assigned this Sep 13, 2025
…ceptions

Fixed GetValidFileStreamOrDefault to return null instead of throwing InvalidOperationException when file size is not aligned to element size. This ensures ReadFirstOrDefault and ReadLastOrDefault methods follow the OrDefault convention of returning default values in unexpected cases rather than throwing exceptions.

Added comprehensive tests to verify the correct behavior for misaligned files.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] This method does not corresponds to OrDefault methods convention Fix OrDefault methods to return default values instead of throwing exceptions Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 15:22
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.

This method does not corresponds to OrDefault methods convention

2 participants