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

Do not assign property multiple times inside method #470

Open
RalfKoban opened this issue Feb 17, 2022 · 0 comments
Open

Do not assign property multiple times inside method #470

RalfKoban opened this issue Feb 17, 2022 · 0 comments

Comments

@RalfKoban
Copy link
Owner

It may happen that programmers write code as following:

public string SomeProperty { get; set; }

public void DoSomething(int value)
{
    if (value == 1)
    {
        SomeProperty = "something";
        return;
    }

    if (value == 2)
    {
        SomeProperty = "something else";
        return;
    }

    if (value == 3)
    {
        SomeProperty = "something different";
        return;
    }

    SomeProperty = string.Empty;
}

They evaluate a value and - dependent on the result - assign a value to some property.

It would be much better if they would separate the evaluation and the assignment into different methods, as in following code:

public string SomeProperty { get; set; }

public void DoSomething(int value)
{
    SomeProperty = Evaluate(value);
}

private string Evaluate(int value)
{
    if (value == 1)
    {
        return "something";
    }

    if (value == 2)
    {
        return "something else";
    }

    if (value == 3)
    {
        return "something different";
    }

   return string.Empty;
}

That would lead to less side effects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

1 participant