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

MAINT: Use primary constructor feature to enhance code readability #13268

Closed
wants to merge 4 commits into from

Conversation

asilverman
Copy link
Contributor

@asilverman asilverman commented Feb 7, 2024

Description

This pull request introduces a new rule to the .editorconfig file of the Bicep project: dotnet_diagnostic.IDE0290.severity = suggestion. This rule enforces the use of primary constructors where applicable, a feature introduced in C# 9.0 that allows for more concise and readable class definitions by combining the constructor with the declaration of properties.

To make this PR easier to review I broke it down into 3 separate commits:

  • 7841443 Makes the changes to .editorconfig and formatter github workflow
  • fc5338a Changes resulting from running dotnet format style --diagnostics IDE0005 IDE0290 --severity info
  • c734b2e Manual changes to fix dotnet build. These are necessary because as a result of applying the new rule the compiler is detecting unused and/or unreferenced variables.

Rationale

The adoption of primary constructors aligns with modern C# practices and can significantly improve the readability and maintainability of our codebase. Primary constructors reduce boilerplate by eliminating the need for explicit property assignments in the constructor body, making classes more succinct and focusing on the essential aspects of their design.

Benefits

  • Improved Readability: By reducing the amount of boilerplate code, the intent of a class becomes clearer. This simplification makes it easier for new contributors to understand the codebase and for existing contributors to maintain it.
  • Enhanced Productivity: Developers can define properties and constructors in a single line, speeding up the coding process and reducing the potential for errors in property assignments.
  • Consistency with Modern C# Practices: As the Bicep project evolves, embracing modern language features ensures our code remains clean, efficient, and up-to-date with current development standards.
Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

github-actions bot commented Feb 7, 2024

Test this change out locally with the following install scripts (Action run 7822314977)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 7822314977
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 7822314977"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 7822314977
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 7822314977"

Copy link
Contributor

github-actions bot commented Feb 7, 2024

Test Results

    66 files   -     33      66 suites   - 33   25m 23s ⏱️ - 21m 18s
11 007 tests  -     20  11 007 ✅  -     20  0 💤 ±0  0 ❌ ±0 
25 904 runs   - 12 948  25 904 ✅  - 12 948  0 💤 ±0  0 ❌ ±0 

Results for commit 75a4b73. ± Comparison against base commit c85ef40.

dotnet_diagnostic.IDE0005.severity = suggestion
# IDE0290: [Use primary constructor](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0290)
dotnet_diagnostic.IDE0290.severity = suggestion
Copy link
Member

Choose a reason for hiding this comment

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

This isn't a convention we currently follow, and I think it warrants a discussion with the team before enforcing it.

I personally find this less readable, and don't see the value in enforcing it, but would be happy to hear opinions from others.

@asilverman asilverman added the do not merge Do not merge this pull request yet. label Feb 9, 2024
@asilverman
Copy link
Contributor Author

During Bicep Discussion the team decided to not adopt this rule since we don't have confidence that the Roslyn compiler will not be breaking the code in the case of constructors that rely on some internal ordering of statements/expressions.
Use of primary constructors for classes that use dependency injections and simple initialization logic are ok to sporadically be used in our codebase

@asilverman asilverman closed this Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge Do not merge this pull request yet.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants