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

TaskExtensions: Rename AndForget() to CatchAndLog() #8950

Merged
merged 3 commits into from May 14, 2024

Conversation

henon
Copy link
Collaborator

@henon henon commented May 11, 2024

Description

The name Task.AndForget() implies discarding the task. However, AndForget() does something very important, it observes the task in the background and catches any exceptions which are then forwarded to the global unhandled exception handler in MudGlobal. If we can not await a task we should never simply discard it like this: _ = SomeMethodAsync();. Instead we should do SomeMethodAsync().AndForget();. To make the significance of not discarding more evident I renamed the method AndForget to CatchAndLog ...

SomeMethodAsync().CatchAndLog(); ... discards the task in a safe manner.

Type of Changes

Refactor

@henon henon added the refactor Refactoring of code. No bug fixes or new features label May 11, 2024
Copy link

codecov bot commented May 11, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 90.45%. Comparing base (28bc599) to head (fecb963).
Report is 190 commits behind head on dev.

Files Patch % Lines
...MudBlazor/Components/DataGrid/MudDataGrid.razor.cs 0.00% 1 Missing ⚠️
...rc/MudBlazor/Components/Input/MudDebouncedInput.cs 50.00% 1 Missing ⚠️
src/MudBlazor/Components/Mask/MudMask.razor.cs 50.00% 1 Missing ⚠️
...ontentNavigation/MudPageContentNavigation.razor.cs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #8950      +/-   ##
==========================================
+ Coverage   89.82%   90.45%   +0.62%     
==========================================
  Files         412      419       +7     
  Lines       11878    12196     +318     
  Branches     2364     2381      +17     
==========================================
+ Hits        10670    11032     +362     
+ Misses        681      629      -52     
- Partials      527      535       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/MudBlazor/Extensions/TaskExtensions.cs Outdated Show resolved Hide resolved
src/MudBlazor/Extensions/TaskExtensions.cs Outdated Show resolved Hide resolved
src/MudBlazor/Extensions/TaskExtensions.cs Outdated Show resolved Hide resolved
@danielchalmers
Copy link
Contributor

Name is not obvious about its intent to me at least. Something like FireAndForwardExceptions is longer but more self-explanatory and this method should be used sparsely anyway

@henon
Copy link
Collaborator Author

henon commented May 13, 2024

Catch is pretty self explanatory, no need to explain it. Log as well, it expresses that exceptions are not discarded (as oposed to Forget which hints at discarding) and I think Log it is better or at least as good as Forward. If we want maximum self-explanation we'd have to do DontAwaitButExecutInBackgroundAndCatchExceptionsAndForwardToGlobalExceptionHandler but I rather prefer a short and sexy name such as CatchAndLog which includes the most important details. It is self-evident from the code that we don't await, no need to express this in the name (that's also why I don't like AndForget which hints at the phrase fire-and-forget).

@henon
Copy link
Collaborator Author

henon commented May 13, 2024

ObserveExceptions() would also be a good one

@henon
Copy link
Collaborator Author

henon commented May 13, 2024

Or DiscardAndObserve()

@henon henon merged commit 12d5de2 into MudBlazor:dev May 14, 2024
4 checks passed
@henon henon deleted the rename-andforget branch May 14, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactoring of code. No bug fixes or new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants