Calanus
Calanus

Reputation: 26277

If a method performs operations on a reference type is there any need to return it?

If I have a method that operates exclusively on a reference type variable (e.g. a datatable) and modifies it, is there any need to return that variable from the method??

For example, the following method goes through a datatable and trims all the values within it and then returns the datatable to the caller:

        private DataTable TrimAllDataInDataTable(DataTable dt)
        {
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    row[dc] = row[dc].ToString().Trim();
                }
            }

            return dt;
        }

Would it be better if this method returns void? Seems pretty pointless to return it (?), but do you think that it reads better if it returns the object after operating on it (like it does at the moment)??

Upvotes: 2

Views: 230

Answers (9)

teabot
teabot

Reputation: 15444

Arguments as outputs could be considered confusing as more often that not in OO programming we'd expect arguments to be used as inputs not outputs. Another approach suggested by Clean Code would be to make the variable being modified a member of a class:

class DataTableTrimmer {

    private DataTable dt;

    public DataTableTrimmer(DataTable pDt) {
        dt = pDt;
    }

    public void TrimAllDataInDataTable()
    {
        foreach (DataRow row in dt.Rows)
        {
            foreach (DataColumn dc in dt.Columns)
            {
                row[dc] = row[dc].ToString().Trim();
            }
        }
    }

    ...
}

Upvotes: 0

Mark
Mark

Reputation: 2432

Personally, I'd return void. The method name lets developers know that you are going to modify values in the DataTable, and returning the DataTable doesn't give any benefit whilst it may introduce the ambiguity that you are cloning the table before processing it.

Upvotes: 5

Andreas Tasoulas
Andreas Tasoulas

Reputation: 453

Chaining comes to mind. For instance, suppose you have three methods A, B and C. Instead of calling:

object.A(object);

object.B(object);

object.C(object);

you could go for the terse:

object.A(object).B(object).C(object);

You could use the null return value to indicate that something went wrong and catch a NullPointerException (or equivalent, I am not familiar with C#) for the above "chained" statement.

Upvotes: 0

Steef
Steef

Reputation: 34665

To me, when a method returns a reference type, I expect it to be a new object. I'd pick making it return void.

A question: is there any reason for this method to not be a member of the DataTable class? I think that DataTable.TrimAllData() would work even better

Upvotes: 9

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

The extra value that can be added by returning the object is that you can chain calls in different ways:

DataTable dt = GetDataTable();
int rowCount = TrimAllDataInDataTable(dt).Rows.Count; // OK, perhaps not the  
                                                      // best example, but it  
                                                      // shows the idea

You can also nest method calls with it:

SomeMethodThatTakesADataTable(TrimAllDataInDataTable(dt));

Upvotes: 0

Timothy Carter
Timothy Carter

Reputation: 15785

A reason to this is it allows you to string methods together:

TrimAllDataInDataTable(dt).WriteXml(...);

Not always useful, or even appropriate, but sometimes can be nice.

Upvotes: 2

Jeremy Smyth
Jeremy Smyth

Reputation: 23493

It's redundant.

There are two approaches to doing this - the classic way (caller chooses what to manipulate/change), and callee changes (which is what you have:

  • If the caller chooses what to manipulate, then your method will operate on a copy of the dataset, and return that copy, leaving the original untouched. Then the caller may overwrite the original, if desired.
  • The alternative lets the called method do the in-place operation, in which case the return value is more typically a boolean ("Yes, I've succeeded!") or an int containing the number of values affected.

Upvotes: 1

John Fisher
John Fisher

Reputation: 22719

For purposes of the DataTable or the function themselves, there is no value to returning the changed reference variable from the function.

However, if you like the way that functional programming works (like the Linq extension methods), then returning the DataTable is a great idea. It lets you chain calls to the variable and provides options for more expressive code.

Upvotes: 0

Chris Tonkinson
Chris Tonkinson

Reputation: 14459

If you're not using the return value, I'd lose it. No use in wasting the cycles copying the reference back to the calling frame if it's going to be ignored. But perhaps there's some error checking you can do within this function? In that case, you could be returning a True/False "success" code.

Upvotes: 4

Related Questions