Search code examples
c#anti-patterns

Change value of parameter inside method, is this an anti-pattern?


So something like this

public void MyMethod(object parameter)
//....
    BuildSomething(parameter);
    BuildLayers(parameter);
    BuildOtherStuff(parameter);
}

public void BuildSomething(object parameter)
{
//...
    parameter.SomeProperty = "sadsd";
//...
}

If this is an anti pattern, what is it called? The problem (possibly) is that you are implicitly changing parameter and using the changed value.
I just want to know what is this anti-pattern know as

Thanks


Solution

  • It is a side effect.

    These are normally not good and considered to be a code smell as it makes it difficult to reason about and understand code.

    However, this pattern is sometimes useful.

    C# codified the ref and out keywords specifically to show that a method is expected to have side effects.