I have the following class:
public class Person
{
public String Name { get; set; }
}
I have a method that takes in Person and a String as parameters:
public void ChangeName(Person p, String name)
{
p.Name = name;
}
Since Person was passed by reference, it should change the Name of the passed instance.
But is this method more readable than the one above?
public Person ChangeName(Person p, String name)
{
p.Name = name;
return p;
}
Is it more readable? No. In fact you may be doing more harm them good.
By having it return a Person object, it might lead you to believe that instead of modifying the Person parameter, it is actually creating a new Person based on p but with a different name and someone could incorrectly assume that p is never changed.
Either way, if you have a method that has no affect on the class it is apart of it should probably be static. That helps you know for sure that it doesn’t affect its class. Only have the method return a value if you need it to return a value.
So here is my recommendation for this method: