Skip to content

Object oriented programming vs. class oriented programming

In the last post, the code was pretty clean. Our resident Rhino.Mocks guru at work, Sean, left a comment saying that the new code was much better than the Do-Func stuff I had before. Sean was the one that pointed me to the Repeat.Times methods in Rhino.Mocks. I thought I'd post the old code that I had cobbled together from a StackOverflow answer.

CODE:
  1. IDataReader reader = MockRepository.GenerateStub<IDataReader>();
  2.  
  3.             reader.Stub(x => x.Read()).Do((Func<bool>) delegate()
  4.  
  5.                                                            {
  6.  
  7.                                                                m_NumberOfTimesIDataReaderHasBeenCalled++;
  8.  
  9.                                                                return
  10.  
  11.                                                                    (m_NumberOfTimesIDataReaderHasBeenCalled%2 != 0);
  12.  
  13.                                                            });
  14.  
  15.             reader.Stub(x => x["ID"]).Return(Guid.Empty);
  16.  
  17.             reader.Stub(x => x["FullName"]).Return("Test User");
  18.  
  19.  
  20.  
  21.             List<UserDTO> list = SearchProvider.ParseUserData(reader);
  22.  
  23.             Assert.IsNotNull(list);

The re-factored code using the Repeat.Times methods.

CODE:
  1. IDataReader reader = MockRepository.GenerateStub<IDataReader>();
  2.  
  3.             reader.Stub(x => x.Read()).Return(true).Repeat.Times(1);
  4.  
  5.             reader.Stub(x => x.Read()).Return(false);
  6.  
  7.             reader.Stub(x => x["ID"]).Return(Guid.Empty);
  8.  
  9.             reader.Stub(x => x["FullName"]).Return("Test User");
  10.  
  11.  
  12.  
  13.             List<UserDTO> list = SearchProvider.ParseUserData(reader);
  14.  
  15.             Assert.IsNotNull(list);

You can see how the second code sample is much cleaner than the first. A lot of the messiness of the first code sample comes from talking to the compiler instead of talking to objects. What do I mean by that? Well, in the first example we have to tell the compiler what the delegate should return

CODE:
  1. Func<bool>

You'll also notice some ugliness inside of the delegate body.

CODE:
  1. m_NumberOfTimesIDataReaderHasBeenCalled++;
  2. return (m_NumberOfTimesIDataReaderHasBeenCalled%2 != 0);

Here, I was incrementing a class member and checking to see if it was odd or even. If it was even, I'd return true, otherwise I'd return false. This allowed me to control the number of times the IDataReader.Read() method would return true. In this case, it would return true once, then the variable would be incremented to an odd number and the Read method would return false.

That's all part of me telling the compiler what to expect, when what I really want to do is just tell my objects what to do. This episode of the Alt.net podcast also talks a little bit about class-based programming versus object-oriented programming.

Share and Enjoy:
  • del.icio.us
  • DotNetKicks
  • DZone
  • Reddit
  • Digg
  • StumbleUpon
  • LinkedIn
  • Facebook
  • FriendFeed
  • HackerNews
  • Netvibes
  • Posterous
  • Tumblr
  • Twitter

2 Trackbacks/Pingbacks

  1. [...] Object Oriented Programming vs. Class Oriented Programming (Scott Koon) [...]

  2. Guru Programming Tools. | 7Wins.eu on 04-Oct-09 at 12:12 am

    [...] jQuery: JavaScript Library of the FutureModern Programming: Anti-Evolution? | Programming By A ToolHow To Set Up A Web Site For Dummies – Guru Consulting Servicesearticle GURU » Decoding success with Dynamic Website DesignObject oriented programming vs. class oriented programming | Lazycoder [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes