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

roundcrisis picture roundcrisis · Dec 28, 2012 · Viewed 7.6k times · Source

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

Answer

Oded picture Oded · Dec 28, 2012

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.