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

Removed nesting levels through block-scoped usings #90

Merged
merged 1 commit into from
Aug 22, 2023
Merged

Removed nesting levels through block-scoped usings #90

merged 1 commit into from
Aug 22, 2023

Conversation

Lehonti
Copy link

@Lehonti Lehonti commented Aug 3, 2023

Background

.NET allows to initialize and properly dispose of IDisposable objects (usually objectd that handle external system resources whose structure is foreign to the runtime and have to be disposed of explicitly) through using statements. This means that they will be disposed of when exiting the block, regardless of how it happens (be it through normal control flow or an exception being thrown).

Formerly, the best, cleanest code we were able to write was something like this:

{
    using (var fileHandle = File.OpenRead("log.txt"))
    {
        // Do operations with fileHandle
        // Files have to be closed after we are done using them
    }
}

In newer versions of .NET, though, we can do the following:

{
    using var fileHandle = File.OpenRead("log.txt");
    // Do operations with fileHandle
    // Files have to be closed after we are using them
}

This looks closer to regular variable initialization, which allows us to keep the statement more readable, whist preserving the benefits of handling our IDisposable objects properly.

Changes proposed in this pull request

In this pull request, I've aimed to improve code readability by translating using statements from the former syntax to the newer syntax in several places throughout the code. This has helped me reduce block nesting levels, which are one of the things that can make it harder to understand what is happening.

@DamianSuess
Copy link
Collaborator

Looks decent, @Lehonti.
Please provide a full description of what this PR aims to improve/accomplish. This helps to maintain a clean history for others reviewing changes over time.

@Lehonti
Copy link
Author

Lehonti commented Aug 14, 2023

Hi @DamianSuess, I've added an explanation on the original post.

@DamianSuess
Copy link
Collaborator

Sorry for the delay, I'm a bit tied up this week and will give this branch a one-over as soon as I can.

Thank you again, @Lehonti for the contribution!

@DamianSuess DamianSuess merged commit 47b2b50 into AvaloniaCommunity:develop Aug 22, 2023
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.

None yet

2 participants