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

Refactoring request: nullable values to ?? syntax #1507

Open
martinandersen3d opened this issue Aug 30, 2024 · 1 comment
Open

Refactoring request: nullable values to ?? syntax #1507

martinandersen3d opened this issue Aug 30, 2024 · 1 comment

Comments

@martinandersen3d
Copy link

martinandersen3d commented Aug 30, 2024

I would love to have some refactor tool that could suggest non-nullable variables from my parameters or any variable. So when the cursor is on the parameters, it can suggest to insert code:

Actual Behavior:

MyMethod(int? intOrNull, string? strOrNull)
{
...
}

Expected Behavior:

MyMethod(int? intOrNull, string? strOrNull)
{

    // Suggested refactor: Convert null, to 0
    Int intOrNullParam = intOrNullParam ?? 0;
    
    // Suggested refactor: Convert null to empty string
    string strOrNullParam= strOrNull ?? "";
    
    ...
}

What would be the best solution here?

Should also handle variables:

bool? boolOrNull = null

// Suggested refactor: Convert bool to false
bool boolOrNullVar = boolOrNull ?? false

What would be the best solution here?


Similar issue:
#411

@BenjaminBrienen
Copy link
Contributor

Would this only affect primitives? Structs (which have default 0 values)? This refactor would be called something like " coerce default value" but "" isn't actually a default value and string.Empty is idiomatic anyway.

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

No branches or pull requests

2 participants