In-place or operator methods?
Written by Ian Elliot   
Article Index
In-place or operator methods?
Solution

Solution

This is a very silly problem. It all comes down to a careful interpretation of what the Add method actually does. The programmer who had made the mistake had in his head:

"The Add method adds 12 hours to the date stored in t1"

And it clearly does because the test

if(t1.Add(new TimeSpan(12,0,0))>EndDate){

works perfectly.

In fact what he should have had in his head was:

"The Add method adds 12 hours to the date stored in t1 and then returns a brand new TimeDate object"

Now it all makes sense.

When you do

if(t1.Add(new TimeSpan(12,0,0))>EndDate){

it is the temporary object that is created by the Add method that is used to compare with EndDate and so it all works. However when you do:

t1.Add(new TimeSpan(12, 0, 0));
if(t1>EndDate){
Do something as the task nearly over
}

the new object created by Add is just thrown away and t1 doesn't change.

You can easily see that this is the case by writing:

t1.Add(new TimeSpan(12, 0, 0));
Console.WriteLine(t1);

and noticing that t1 hasn't changed.

It becomes even more obvious when you consider the operation written in terms of the overloaded "+" operator. For example, you would never write:

TimeSpan d1=new TimeSpan(12, 0, 0);
t1 + d1;

as it is obvious that the + operator doesn't modify t1 and you are clearly throwing away the result of an expression - something that C# doesn't allow so you will also see an error message.

The solution to the problem is obvious - just be aware that the Add method doesn't modify the object it is a member of.

Pattern

It is all to easy too write this problem off as trivial. In fact it reveals a lot about the way we think of things in procedural programming and in object-oriented programming. For example, when you first start writing code you quickly learn:

data=data+1;

and understand the idea of assignment in action. Notice that a mathematical interpretation of the above results in a deduction: i.e. if x=x+1 then 0=1 which is of course nonsense and here you have the distinction between math and programming.

So far so good but then, if you move on to the right programming language, you learn:

 data++;

or

++data;

both of which can be interpreted as telling data to add one to itself.

Moving on to object-oriented programming you then start to think about methods that do things to objects. For example you might invent a Sort method for an array that sorts it into order. For simple efficiency reasons it might never occur to you that the Sort method should return a new copy of the array and leave the original untouched.That is the Sort method actually sorts the original array - this is generally called an "In place Sort" or an in place method.

In-place methods may seem natural when you first start programming but the "always generate a new object" approach is becoming the norm today - partly because we believe that immutability is a good idea and partly to fit in with the operator semantics i.e. methods should be usable as operators.

As already pointed out just as methods can adopt operator semantics by creating a new object as a result so too can operators take on in-place semantics, i.e. ++data and data++

Whatever the reason we do have two conventions - in-place methods and operator methods and it important that you always know which is which.

Banner

More Puzzles

Sharpen Your Coding Skills
Towers Of Hanoi Mutants

Towers of Hanoi is a classic puzzle and is often used to illustrate the idea of recursion. Here Melvin Frammis challenges you to attempt to find solutions to some variations - but first he explains th [ ... ]


Sharpen Your Coding Skills
The Post Production Problem

Joe Celko has posed another puzzle that requires you to think like a programmer. This one is all about Post tag machines, which have absolutely nothing to do with mail of any type but a lot to do with [ ... ]


Sharpen Your Coding Skills
Vertex Coverings And The Cool Kids Problem

Joe Celko has posed another puzzle that requires you to think like a programmer. This one asks us to find the cool kids in a social network - the ones who taken together know everyone else. This is al [ ... ]


Other Articles

<ASIN:0321637003>

<ASIN:0596800959>

<ASIN:047043452X>

<ASIN:0123745144>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.