9

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;
}
Ian
  • 5,625
  • 11
  • 57
  • 93
  • 1
    Technically, `p` is not passed by reference in any of those examples. – Amber Mar 11 '11 at 06:07
  • @Amber How so? I'm pretty sure that is entirely incorrect. – Corey Sunwold Mar 11 '11 at 06:07
  • 3
    `p` *contains* and *passes* a reference, but is not *passed by reference* - there is a distinct difference between the two. Pass by reference (by declaring the arguments as `ChangeName(ref Person p, ...)` would allow something like `p = foo` to then completely change which Person the caller's variable was pointing at, not just the contents of the currently pointed-to Person. – Amber Mar 11 '11 at 06:10
  • For more details, see http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx examples #4 and #5. – Amber Mar 11 '11 at 06:12
  • @Amber - Right. Right. I screwed that up. – Ritch Melton Mar 11 '11 at 06:30

8 Answers8

12

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:

public static void ChangeName(Person p, String name)
{
    p.Name = name;
}
Corey Sunwold
  • 10,194
  • 6
  • 51
  • 55
1

There isn't anything right/wrong with either approach. Depends on what your program needs.

Returning the parameter passed into a method is rarely needed as it is always possible for the user to just use the variable passed as argument instead.

It, however, gives you the flexibility of eventually overriding this implementation, or passing this implementation into another function which accepts delegates with similar signatures. Then you can pass in other implementations that does not return the same Person object.

Do it only if you really need the flexibility.

Stephen Chung
  • 14,497
  • 1
  • 35
  • 48
1

I would suggest you use one of the following for best readability:

public static void ChangeName(Person p, String name)
{
    p.Name = name;
}

public static Person WithName(Person p, String name)
{
    return new Person(p) { Name = name };    
}

The second one treats the Person object as immutable and does not change the state of the object. The ChangeName function explicitly changes the state of the input object. I think it's important to make a clear distinction between the two types of methods. A good rule of thumb to follow is that a method should not change the state of an object AND return one at the same time.

Can Gencer
  • 8,822
  • 5
  • 33
  • 52
1

first of all p is not being passed by reference in the first example. Your second method makes one believe that it is returning a new reference which it is not. So I don't think the second one is any clearer than the first one.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
0

In the case you've described, I would say neither. Its not really clear what you are trying to do with this method. Just use the object and set the property. Inserting a method into the execution path just complicates understanding and creates another dependency on the Person object and its underlying value.

If you are asking a meta question that involves some design over and above the code you have posted, then I am missing it.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
0

The first one is better, because of that the second one might lead you to believe that p is immutable. But, the whole method is useless since it just calls the setter. Why not just call the setter directly?

Teo Klestrup Röijezon
  • 5,097
  • 3
  • 29
  • 37
0

I beleve that your 2nd approach is not more readable YAGNI. But if you change it like this

public static class PersonExtensions 
{
public static Person ChangeName(this Person p, String name)
{
 p.Name = name;
 return p;
}

you will have an extensionmethod for a fluent interface a la

new Person().ChangeName("Peter Smith").SendEmail().Subject("Test Mail").Receiver("....)
k3b
  • 14,517
  • 7
  • 53
  • 85
0

Here is the definitive reference to understand passing parameters by value/reference.

Looking at the code, why don't you use property?

public string Name
{
   set {name = value;}
   get { return name; }
}

EDIT: Auto implemented properties

public string Name
{
   set;
   get;
}
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88