Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool

October 2007 - Posts

  • See you at Chicago MOSSCamp 2007

    On Friday, November 9th there is going to be a one day MOSSCamp in downtown Chicago.  This is the equivalent to the CodeCamps that are run across the country, but is specific to MOSS. 

    Since I have been diving in to MOSS, I thought it would be nice to drop by to see what I could learn.  If anyone else from the Chicago area has plans on attending let me know, it would be nice to meet up with fellow readers.

    MOSSCamp homepage - Here

    Till next time,

  • New Development Methodology: Change and Pray Development (CPD)

    A while back there was a posting out there about all the different software development methodologies (this was a parity posting).  Yesterday I was chatting with a buddy and we coined a new development methodology.... Change and Pray Development (CPD).

    Here is how you can tell if you or your team/company follow this methodology.

    • You don't have any unit tests.
    • Your system is so intertwined that you cannot tell the difference between the UI and the data layer.
    • Making a single change to a single class has ramifications beyond your wildest dreams.
    • Your development team is afraid to make changes because the only person that knew/understood that code has left for greener pastures.
    • It can take you weeks worth of tracking down a 'simple' bug before you know enough to even know where the bug originates. 

    Here is the typical development pattern for someone in that follows this methodology

    • Decide to make a change to the code
    • Find the place you 'Think' needs the change
    • Make your change
    • Compile your code to hope is even compiles
    • Run the application to ensure you changes worked
    • PRAY that you did not screw up anything else

    If you find yourself in a Change and Pray shop run, don't look back, don't try to fix it, don't hope it will get better just RUN......

    Till next time,

  • Object Construction - Constructing for Success - Part Duex

    A few months ago I posted about my thoughts on Object Construction (found here).  In the post I talk about constructing object with ALL the required values to make the object valid. 

    This post is just a quick follow up to that.  The other day I was refactoring some code and needed to add a new property to it.  The new property was a required one and the object would not be considered valid without it being populated.  Because I have my constructor require all needed properties, I simply added the parameter to the constructor's signature and re-compiled. 

    Because this is now part of my constructor I instantly knew where I needed to update my code to add this call (yes I know I could have found this other ways, but this is the simplest if you ask me).  Had my constructor NOT required the values, I would have had to search for every use of the object and perform a set on my new property.  This is doable, but is more error prone.  With my luck I would have missed something.

    Anyway, the point of this was that if you put requirements on your objects it will make for simpler and easier refactoring later.

    Till next time,

  • Mocking Voids with LastCall and RhinoMocks

    When following TDD, using mocking tools like RhinoMocks (here) or NMock (here) is a critical part of the process.  By utilizing mocking tools you can better isolate your tests to only test the code that you have direct control over.  This can prove to be critical component to building out your test suite.

    One thing that I have found fairly poorly documented about mocking is how to mock void methods.  Today I thought I would give a quick how-to on how to mock voids.  My mocking tool of choice is RhinoMocks, so the code below will be specific to that mocking framework.

    Mocking voids is really not that much different then mocking methods with return values.  Except for one slight thing, you actually have to make a call to your method on your mock, then setup the expectation on that call.  Below I will put the code to mock non-void methods, then to mock void methods just to show the differences.

    Mocking non-void methods

    1. MockRepository mocker = new MockRepository();
    2. ISomeObject someObjectMock = mocker.CreateMock<ISomeObject>();
    3. Expect.On( someObjectMock).Call( request.IsRequestValid() ).Return( true );
    4. mocker.ReplayAll();
    5. .... Make some call that will exercise your mocks
    6. mocker.VerifyAll();

    Mocking void methods

    1. MockRepository mocker = new MockRepository();
    2. ISomeObject someObjectMock = mocker.CreateMock<ISomeObject>();
    3. someObjectMock.SomeVoidMethod(0, "", "" );
    4. LastCall.IgnoreArguments();
    5. mocker.ReplayAll();
    6. .... Make some call that will exercise your mocks
    7. mocker.VerifyAll();

    You will notice that the two different methods are pretty much the same EXCEPT for lines 3 & 4 in the void methods.  When mocking void method you need to actually make the call on the mock object for the method you expect to call later in your test.  This will setup the call inside the mocking framework.  The next step is to use the 'LastCall' (some info here) object.  The LastCall object is used ONLY for mocking void methods.  Unlike the 'Expect' object you do NOT need to provide the object that the void method is on, you only need to setup the expectations for the call.  This includes things like the number of calls or the types of arguments for the call.

    Mocking voids is no more complicated then non-voids, but it does require a little more work and the use of different objects.

    I hope this helps.

    Till next time,

     

    1. Using Anonymous Delegates to handle Async Web Service Calls

      I just thought I would share a little trick I used today to back in Async web service calls into existing code using anonymous delegates.

      Here was my problem:
      I have a library that makes use of an existing web service that I do not control (so I cannot change/up the timeout on this).  I have been using this code now for a few weeks with no issues, however late last week I noticed that I was experiencing timeouts when making certain requests via the service.  My original code was making use of the synchronous methods on the web service.  In order to get around the time out issue I wanted to make the calls asynchronous, but I did not want to have to put in a ton of time or effort to retrofit my code. 

      Here is my solution:
      Create an anonymous delegate to handle my Async call and move on.

      Here is my original code

      Here is the new code using the delegates

      As you can see, this code is not 'slick' and 'cool' but it does do the job.  By tweaking this code I was able to not have to re-write any other outside logic to handle the new async way of making this call.

      Till next time, 

    2. SoapException when performing CheckInFile via WSS 3.0

      This is just an FYI. 

      If you are ever calling the CheckInFile method that is on the Lists.asmx web service in WSS 3.0 (MOSS 2007) and you get the following exception System.Web.Services.Protocols.SoapException. It is highly likely that you are trying to check in a file that does not have all the required properties filled out and is violating the check-in rules on SharePoint.

      You will either need to perform an update on the meta data on the document prior to the check in or remove the constraints for the document library.

      Hope this helps someone....

      Till next time,

    3. Backing in Unit Tests into an existing project

      Recently I was asked about backing in unit tests into an existing project.  In particular I was asked A) was possible and B) is it worth it.  In short my answers to both these are Yes and Yes.  However, backing in unit tests is not without its challenges.  I thought today I would go express my experiences with doing exactly this and what types of hurdles I ran into.

      Start off by only testing new code
      When backing in tests, it is very important to accept the fact that a large percentage of your application is going to be untested.  Accepting this fact will help you move forward.  I would suggest that as you write new code (be it new modules, features or whatever) you write them following the TDD practice.  This will be the simplest way to get the ball rolling. 

      Doing this is also an easy way to get other team members on board with the testing idea.  I know from personal experience that when we wanted to started following TDD on a project that was 2 years in, many of the other developers thought it was a waste because we have so much that is untested.  They thought it would take too much time/effort to write test for existing code.  I agree with this.  So only test going forward

      Add tests to existing code only when changes are needed
      I know I just said above to only test new code, and this is true.  However, when you are making changes to existing code test those changes.  Again, you don't have to try to test all the existing code, only the path ways you are adding/changing. 

      If you follow this, over time you will start to build a nice suite of tests that tests not only new code, but some existing code as well.

      Mock as much as possible
      Mocking is a great tool.  I like to mock code that I don't have control over, or when I have no faith the code will work reliably.  This is especially important in a project that does not have tests. 

      An example would be this.  Lets say you are adding a new feature to an application that needs to call into some sort of calculation engine.  Since this calculation engine is not tested and you have no real faith that it will work reliably, mock it.   You should know that given a valid set of inputs you expect to receive a given response.  By mocking this you remove all risk of the calculation engine failing thus causing your tests to fail. 

      *** I can hear you now, you want your test to fail when the calculation engine fails.... No you don't.  Unless your test is actually testing the calculation engine, you don't care if it passes or fails.  You just need to call it. ***

      Take this as a learning experience
      Take this time as a way to learn to write good solid tests.  This will be of great benefit later in your next project.  Also, you will also learn how to write better code that is more acceptable to testing (hum... sounds like another good post).  Remember, in order to become good at something you must first be bad at it.  But with practice you will become better and you will be a better developer because of it.

      Be patient
      Testing is painful at first.  It is a shift in your development mind set.  Be patient, it gets better.  This is no different then learning anything else.  It will take time, but eventually you will become comfortable with TDD and it will become second nature.  You never know, you may end up like me and find it odd when you are NOT writing tests.

      I hope this helps.

      Till next time,

    4. MS is releasing the .Net Source with VS 2008

      Today Scott Gu announced on his blog that MS will be releasing the source for the .net framework with the VS 2008 release. 

      You can read his post here.

      Till next time, 

    5. Great post on how to enable anonymous access in SharePoint 2007

      I came across this great post on how to enable anonymous access in Sharepoint (MOSS) 2007 tonight.  This was very helpful because I was not able to get it working correctly for me, because I missed a step that this post stated.

      Post can be found here

    6. Something odd with my unit tests running in a VM

      Today I noticed something very odd when doing some SharePoint development. 

      I have a VM setup with Win2003 on it in order to do my coding.  I was able to run my unit tests (via Resharper's Unit Test Runner UI) and was coding away.  I decided to map a share to my host PC and move the source over to the host (thought it was better to keep the source on the host then the vm).  After I moved the source to the host I opened the solution again, but this time from the hosts' file share.  When I tried to run the same unit tests everything appeared to be working fine however the unit tests would not run.  The test IDE just said 'tests did not execute'.  I did not get any errors.

      I thought this was odd, so I shut down the IDE and tried it again.  Same thing.  Just to see if it was caused by the files being on a share I re-opened the solution that was inside the VM.  This time all was well.

      I don't know if this only happens when using the Resharper UI or if it would happen using NUnit's UI.  I will have to check and see.

      Has anyone else noticed this before?

      Till next time,

    7. Why I believe IN and WRITE unit tests

      In my last post I think I struck a cord with some people in my post 'Unit tests taking too much time'.  My intent was NOT to sound like an elitist Agilist or any else of that nature.  My intent was simply to put a post out there about the misperception (in my opinion) about how writing unit tests take too much time.

      I thought I would put my 'retort' on why I believe in and write unit tests.

      Allows ME to write better code

      I believe that when I truly follow TDD practices my code comes out simpler.  I have also found, that my code tends to follow YAGNI more so then when I don't follow TDD practices.

      Here is an example from my past experience.  On a prior project I needed to build a 'search engine' into our app (this was not an actual search engine, more like a hunt path engine).  When I first started out writing code I started out by creating all these classes and logic that I thought I would need.  After about 3-4 hours of coding I was dead in the water.  I was not able to solve my problem in a simple manner and I was frustrated.  I decided to take a step back and start over. 

      This time though I started out by writing test to mimic what I actually needed.  I continued to follow the principles of TDD (btw, TDD is more then just about writing tests), within an hour or so I had my solution pretty much good to go.  All those classes and methods I THOUGHT I would need I did NOT.  I was able to scrap about 50% of the code/classes that I thought I needed.  And guess what, now I have a test suit to test my new code.

      Allows ME to find more bugs during the development process

      It has been my experience that I find more bugs while writing tests.  This is due to the fact that I not only follow TDD, but I also do my best to implement the Fail Fast Principle.  When I am writing my tests I have the ability to create tests that easily walk though the different pathways of my code (I attempt to hit 100% code coverage as well).  If I were not writing tests I would have to do this via the application, and this is much more painful and it less likely that I would actually do it.

      By writing my tests and testing my code during the development process I know that I have reduced my bug count significantly.

      Makes ME feel more comfortable making changes

      This is the beauty of TDD, I am not afraid to make any critical changes to my code because I KNOW that I have tests to verify if I screwed anything up. 

      Have you ever run across some code that 'you were scared' to change?  If you had tests for that code I bet you would a WHOLE LOT LESS scared to make any changes.  I know that I am.

      A case in point for this is as follows.  On a prior project a consultant I was working with needed to make changes to some 'smelly' logic.  This just happened to be a week or so before a scheduled release.  Beacuse he had NO confidence that the changes he would make would NOT break the application, he convinced the business to NOT make the changes.  I bet you a dollar that had there been unit tests he would have been much more willing to make the changes.  In this case the fact of NOT having tests may have caused the business to lose out on features it needed.

      Improves MY code

      Because following TDD practices forces you to write your failing code before your passing code, I tend to implement my intent much cleaner and simpler.  I also know that because I am writing tests along the way my code is solid.

      Allows ME to better convey intent

      I know that tests are not a substitution for quality comments that convey intent, but they sure are the next best thing.  It is really nice to be able to point someone towards a suite of tests to 'demo' how to use a component.

      A case in point for this is as follows.  A few weeks ago at work I wrote an API to allow the querying of some data via our imaging system at work.  Turns out that an outside party needed to use this API for integration into on of our 3rd party applications.  When he came to me asking for 'how do I use this', I simply emailed him my suite of tests.  In this suite I had pretty much any conceivable use for the API.  To date the vendor has not asked me how to implement the API.

      Allows MY tests to be automated and rerunable

      This is a nice side effect of having tests.  Because we use both NUnit and CruiseControl I can run my tests on every check in.  This is cool because now I (and the team) know if changes I made breaks some other part of the system.  I would rather know now then before QA hits it. 

      The above are simply my opinion, if you don't share the same opinions, let me know.

      Till next time,

    More Posts

    Our Sponsors

    Proudly Partnered With


    This Blog

    Syndication

    News