• Post Calendar

    May 2012
    S M T W T F S
    « Apr    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

My Dell U3011 Monitor Won’t Display 2560×1600

HEADLINE: Try using the DisplayPort cable that shipped with it.

I got the new Dell U3011 Monitor today – 30” monitor capable of up to 2560×1600 resolution. It ships with three cables – DVI (Dual Link), VGA, and DisplayPort. The DVI is attached and all the instructions show that you can use the 2560×1600 resolution with the DVI… but I couldn’t get it to work.

After about an hour talking to Lance at Dell he suggested trying the DisplayPort cable and viola – I now get 2560×1600!

I use a 55” monitor with 1920×1080 resolution at work. With that I can see 38 lines of code in Visual Studio. Here I can see 62 lines of code! That’s actually going to make my 55” monitor look small now.

image

I hope this helps other that might be having problems. I saw lots of posts, but no solutions. It seems that for some reason the feedback loop from the monitor to the computer isn’t working with the dual link DVI cables so it doesn’t think the resolution can be higher, but DisplayPort works fine.

Unit Testing Your JavaScript

This falls into the category of something I want to remember… so I’m putting it here… Unit Test 101: Are You Testing Your JavaScript?

As Microsoft seems to have been blocked on their XAML everywhere strategy as a cross-platform strategy they seem to be looking now to HTML5 and JavaScript. I was enthusiastic about the use of Silverlight and Windows Presentation Foundation (WPF) as two presentation tiers that could relatively easily share a common logic/data tier. But when Apple banned both Flash and Silverlight from the devices that created some issues. I was hoping that consumers would complain loudly about not having it and that Apple would listen to their customers. Not the case.

So now what… I see continued references to HTML5 and JavaScript as the way to achieve the cross-platform support. But I’ve grown very accustomed to high quality software made possible by a strong unit testing strategy (ideally test driven development or TDD). So this post is a pointer to an excellent article that is at least a start for doing that in a JavaScript world.

I hope Microsoft really pushes the envelope on their testability of JavaScript in their next release of Visual Studio.

Thanks Christian Johansen!

Changing the Patch on My Uniform

I’ve decided to pursue my passion for developing great software (for the oil and gas industry) at a new company.

The Military Perspective

When I was in the U.S. Army Reserves for 10 years I had occasion to change units – or companies – several times. For example, when I moved from Denver to Houston I switched from Headquarters Company in the 244th Engineer Battalion to D Company with the 871st Engineer Battalion. I really enjoyed working with individuals in the 244th and ran into someone from my old unit in the Denver airport several years ago. There is a true sense of camaraderie. We all worked toward the common goal to “defend the Constitution against all enemies, foreign and domestic”.

People Matter

I learned years ago that what really matters are people. Treat people with respect and look out for their best interest as you would your own child or someone else you love and all will work out well. I have a passion for excellence (hence one of my site’s name “JoyOfExcellence.com”). Along the way I’ve discovered that making my passion for excellence known attracts others with a similar passion. I know I’ve learned much from some talented individuals and I hope that some have learned from me as well. The challenge, learning, and smiling customers is what make software development a “pursuit of happiness” – take those away and it’s drudgery.

My Previous Company

I’ve really enjoyed work at SMT (Seismic Micro-Technology). As a matter of fact, to this day there is not an environment I’ve enjoyed more! I enjoy the loyal customers, my fellow developers and other colleagues, and the product (for the most part). There are most definitely colleagues I’ve worked with that I would greatly enjoy working with again. They share my passion for excellence.  I felt there was an exciting environment there and I’d like to think I had at least some contribution to that.

My Next Company

I fully expect to enjoy the time with my new company, LMKR. Their development presence is global and I’ll be operating out of the Denver office.

Through the years I have discovered that I have a true passion for commercial software development. But I was trained as a geophysicist. While the passion for commercial software development exists, my understanding of the oil & gas industry means that my greatest contribution is commercial software development in the oil & gas industry.

Of course my post on Parable of Two Villages paints my view.

Where Do I Want To Go?

If you follow my blog you’ll understand what I believe to be the “relatively” new way to develop software (at least in our industry).  It starts with unit testing and continues with agile development practices (for which I believe unit testing is a prerequisite… see previous posts for more detail). I believe that on occasion you can achieve a level above and beyond agile development – Super-sonic Software Development. There is a right time and place for each practice with Agile being the more prevalent.

I hope that I can work with both new and familiar faces to greatly enhance software to help find oil and gas.

Summary

So now I am changing companies – from SMT (Seismic Micro-Technology), to IHS, then to LMKR. I’m merely changing the patch on my uniform. I remain dedicated to great commercial software for the oil and gas industry. I hope the sense of camaraderie I’ve experience in both the military and the oil & gas software industry will continue.

Parable of Two Villages

I can’t seem to find the version I remember my wife reading me sometime back, so I’m going to work from memory on this one… Here is a link to a slightly different version.

A couple was travelling from one village to the next when they came upon a farmer working in his field. They stopped to ask him what the people in the next village were like. The farmer asked, “What were the people like in the last village?” The couple replied, “They were rude, unfriendly, dishonest people.” “You’ll find the people in the next village are the same.” said the farmer.

A second couple was also travelling between the same two villages and came upon the farmer. They too stopped to ask what people in the next village would be like. Again the farmer asked, “What were the people like in the last village?” The second couple replied, “They were kind, friendly, generous, great people!” “You’ll find the people in the next village are the same.” said the farmer.

My wife only travels to the villages the second couple visited. I like having her as a travel companion! I hope I visit those too.

Drive

A friend recently recommended a book to me called “Drive: The Surprising Truth About What Motivates Us” by Daniel Pink. I’m glad I read it. There was much about the book that resonated with me. While I believe that Maslow’s Hierarchy of Needs explains the underlying principles for much of what is presented in the book it was eye-opening to read how much science goes behind the notions of focusing on higher planes. Now that many of us seldom worry about where our next meal will come from we can move to new levels.

I’d noted from many other readings that society tends to focus on the higher levels when there are forms of slavery – take the Romans or the Greeks as early examples. Slavery was a means of taking mundane tasks and having others perform them. Today we have computerization. This is an apparently “humane” form of slavery… I say “apparently” since much of the computerization replaces work that humans might otherwise perform.

The key message that I took away as someone that leads software development teams is that motivating individuals is not the problem. Finding ways to NOT de-motivate them is the real challenge. For better or worse, many individuals have an impact on the motivation of software developers that I work with. A lack of understanding of the principles in this book make it all too easy to demotivate high performers.

motivating individuals is not the problem. Finding ways to NOT de-motivate them is the real challenge.

I hope that my Drive for excellence will motivate others around me. I hope that people that work with me will say, “I’m better for working with him than I was before.”

Summary

If you are intrinsically motivated I highly recommend that you read Drive. I found it uplifting and a read that reinforces my beliefs. If you are extrinsically motivated you should probably still read the book so you know how to motivate and “leverage” intrinsically motivated people. Or at least figure out how to not demotivate them. However, I’ve found that extrinsically motivated people don’t understand the information and thus to not take to heart the actions that demotivate (and probably didn’t make it this far in my post anyway.)

I hope you enjoy reading Drive.

Super-sonic Software Development

What happens when you can develop software faster than you can describe what you want it to do? I think the best analogy is a sonic boom or moving faster than the speed of sound.

Agile Development Practices

I’m a big believer in agile software development practices. For that matter, not restricted to software… agile practices in general where you have tight feedback loops. However, I’ve observed some circumstances where even agile practices seemed overkill and to slow things down. Don’t get me wrong, this is an anomaly, but it was more than a one time occurrence.

Beyond Agile

imageI was talking with a colleague the other day about agile software development practices. However, we’d recently had experiences with projects that were small to medium sized but that seemed somewhat frustrating when using Agile practices.

We started to describe some of the characteristics of these projects…

  • The model was quite clear or simple
  • Unit tests were easy to write
  • We could code and deliver results almost as fast as we could write down the stories
  • Coding patterns such as Model-View-ViewModel were well established
  • The team had access to all the right people when they needed them
  • Focus
  • And most importantly, the people on the project were very sharp

The user stories were still valuable… but they were quite short and well formed – particularly the “so that” piece in the “As a [role] I want [feature] so that [outcome].”

Using Team Foundation Server (TFS) made putting the stories there useful from a traceability perspective. And this is not some sort of “free-for-all”. It’s clarity of vision. It’s very high code coverage. It’s tight and easy to understand code.

The Analogy – “Faster than the Speed of Stories”

As we continued to talk we described writing software as fast as we could talk about it. So rather than the “speed of sound”, we have the “speed of stories” – the speed at which a product owner can write and rank the stories and the team can read, discuss, develop acceptance criteria, story point, then place into an iteration. That’s actually pretty fast compared to traditional waterfall documentation cycles. Then when development occurs at a speed that is faster than the “speed of stories” we have Super-sonic Software Development (SSD).

This was a very exhilarating! But how could we sustain it?

That does pose a challenge. I think one trick is to develop software with consistent patterns and leverage the code reuse that’s been promised for so many decades. Another trick is to keep projects small. The unit tests let you refactor so that you can achieve the reuse.

Just as with super-sonic air travel, the conditions must be right – the right equipment and tools, the right people, and the right environment. It is not always possible. But what if we could increase the times that occurs? And somehow develop a process that would support and not hinder it?

I’ll Try to Nurture SSD…

This is a pattern that I’ve only recently started to see clearly, and while it will be a challenge I will do what I can to provide an environment that fosters such “Super-sonic Software Development”.

Configuring SMTP Mail for WordPress

Headline: I put in all the e-mail information for my WordPress site, but I’m not getting the notifications for new users or comments. You need to configure the SMTP and the best way is to install a plugin to do that. I chose Configure SMTP.

Sometime back I moved my blog and recently I see that I’ve ignored quite a few people – real people, not just the usual spam. So I started to investigate why I was not getting the notifications I expected with the help of someone at my site provider.

It turns out that out of the box WordPress does not support SMTP mail by default. This is a great response on the WordPress Forums that pointed me in the right direction: http://wordpress.org/support/topic/where-to-setup-email-system 

I chose Configure SMTP because 1) it seemed to have a long history (track record), 2) it was still up to date with the latest WordPress (maintained), and 3) it had a lot of stars. There are others out there, but I didn’t spend too much time. I’m quite pleased with my choice.

But I Only Want One File!

Technorati Tags: ,

Use ILMerge to combine a single exe and multiple dlls into a single new exe. You can also combine many dlls into a single dll.

Look for ILMerge from Microsoft Research on the web. You’ll need to download their tool to do this.

One .EXE

This was originally driven by a personal need where I wanted to have a single .exe file I could send someone. However, I had my .exe, one of my .dlls, and a third party dll. I could have easily copied my code from my .dll into my .exe, but what about the third party? I was still stuck with 2 files.

Here is the before picture:

image

By running this command I was able to combine them.

ilmerge /out:bin\Release\KarlZSecureFile.exe bin\Release\FileZSecure.exe bin\Release\FileZ.dll bin\Release\Ionic.Zip.dll

And the after:

image

So now I can just give the person the one KarlZSecureFile.exe program as a single file with no install!

I thought it was pretty cool.

TDD Saves Time When You Need It Most

In the life of a project when is time most precious? The first day? Or the last day?

Writing unit tests makes sense, but are you disciplined enough to write them? I understood the value of writing unit tests and had used them on numerous projects. I knew you should write them but I had a hard time articulating their value. I no longer have that problem. Here is an experience that will forever have me not just writing unit tests, but using Test Driven Development (TDD).

Then it happened – a moment that still causes my heart to race when I think about it. A serious, show stopping performance issue was uncovered.

The project had been running for months and now it was the Thursday before Thanksgiving. The scheduled release for the web site was the 1st of December, just days away. A release before the end of the year was required as hundreds of thousands of marketing dollars were committed for that timeframe. Then it happened – a moment that still causes my heart to race when I think about it. A serious, show stopping performance issue was uncovered. The project manager said he would have to tell senior management that we’d have to slip the release until after the middle of January. The performance issue appeared to be in the data access layer. Touching that layer would invalidate over a month of exhaustive testing. Merely performing all the tests again after a fix, assuming we could fix it, would take the project beyond the end of the year. I pleaded with the project manager to give us a few days to analyze the problem. He agreed to give us until the following Wednesday – the day before Thanksgiving. The work began…

The performance problem was perplexing. We ran tests early in the project to prove the approach was viable. But we clearly saw performance concerns when running significant load tests. We went to work with profilers to identify the bottlenecks. After days we finally thought we had a fix. We made the change and ran our unit tests. At this time we had over 94% code coverage. After the first change over 80 percent of the tests failed. But we discovered this in less than 10 minutes! After two more changes we felt we had the issue solved. Before the deadline we were able to show significant performance improvements. But what about all the testing that needed to take place? I reviewed our unit testing strategy with the project manager. That boosted confidence in the changes. But how could we be sure. After many discussions we agreed on a plan. We would have as many testers as we could spare hit the site and test for two days. If we encountered any issues we would then tell senior management that we would need to slip the release past the end of the year. Not a message I wanted to deliver.

After two full days of exhaustive testing by dozens of people the results were in… Not one bug was found!

After two full days of exhaustive testing by dozens of people the results were in… Not one bug was found! Due to some other changes the release was pushed until December 15th, but on that day the site went live. (Sidebar: By the way… The site was actually fast enough before we discovered the issue. As a matter of fact, the site was so fast that the load testing software caused wild fluctuations on the server that are impossible to achieve in real life scenarios. However we made the site even faster and improved memory usage on the server.)

This series of events convinced me. I was glad I had taken unit testing on faith. I know with certainty that if we did not have those unit tests we would not have shipped the product on time. Since then I’ve taken unit testing much more seriously. I now use TDD. I now target 100% code coverage (for the non-presentation tier) and I work to minimize code required for the presentation tier.

So now back to the question… When is time most precious? It was clear in my example that when we got down to the final days of the project we did not have time to spare. But my answer now will surprise you. With test driven development I no longer fear the final days of a project, but instead approach the end with great confidence. Now EVERY day of the project is precious and moves me one day closer to a successful conclusion. An effective test strategy allows me to equalize the days.

A Strategic Case for Unit Testing

There is a certain level of velocity and quality that you will never reach without effective unit testing.

I’ve had an unusual number of queries about unit testing during the past few days. As a result I thought I would put a more complete description together.

Definition

I’m not going to spend a great deal of time to define a unit test. I’ve heard some say, “The smallest piece of code that does meaningful work.” But since I frequently use Test First or TDD (Test Driven Design as one of my friends states) I frequently write a test that doesn’t yet know about the size of the work to be done.

So for my discussion here, it means the use of a “unit test framework” such as Visual Studio 2010 Test or nUnit to write tests that verify your code works properly.

Exercise Your Code to Keep It Lean

Some Characteristics of a Quality Code Base

Years ago on a Channel 9 ArcCast I discussed quality code and the notion of Quality, Fast, and Simple. As part of that discussion with Ron Jacobs I discussed the following notion – that code complexity is a function of (among other things) Lines of Code (LOC). I described (since this was an audio-only cast) the following diagram:

image

When I say, “Complex” I mean difficult to understand and thus, maintain. We’ve all seen code that was so terse that it was difficult to understand. But the more usual case is that a code base grows and grows. One code base I worked on prior to that ArcCast had over 18,000 lines of code. That’s not really a lot, given what it did, but after the release of .NET 2.0 I was able to shrink that code base to 3,800 lines of code!

I firmly believe that when it comes to code bases, “More is NOT better”. When you maintain it, you have more code to read. New developers have more code to read. More code usually has more bugs. More code usually runs slower.

When did you last delete code?

In large code bases with many developers deleting code seems a rare occasion. How do you know the scope of effect? If you have unit tests, you can actually delete code with confidence – even celebration! Unit tests are a safety net that allow you to understand what code is affected by removing the other code.

image

My observations have been that if you do not have an effective Unit Testing Strategy, you will continually grow. Many code bases are crushed by their own weight. A strong unit testing strategy will allow you to continually exercise your code and keep it lean.

Understanding Unit Tests

What better way to understand unit testing than to write unit tests about unit testing? Many years ago that’s what I did. Here’s what my project looks like:

image

So lets start with a look at a class diagram.

image

Since you really can’t understand unit tests without also understanding its relationship with code and the nature of that code I’ve included code models in addition to test models.

I’ll start with code…

Code

Let me start with the base class – Code. It’s really quite simple. You’ll notice the test was listed first.

Code
  1. public abstract class Code
  2. {
  3.     public abstract UnitTest UnitTest { get; set; }
  4.     public abstract string CodeText { get; set; }
  5. }

Legacy Code

I’ve found Michael C. Feathers book, “Working Effectively With Legacy Code” very useful, particularly his definition of Legacy Code. From memory it was “Any code without automated tests.”

Let’s take a look at the LegacyCode class:

Legacy Code
  1. public class LegacyCode : Code
  2. {
  3.     /// <summary>
  4.     /// Tests? We don't need no stinking tests…
  5.     /// </summary>
  6.     public override UnitTest UnitTest
  7.     {
  8.         get { return null; }
  9.         set
  10.         {
  11.             var message = "Get lost – by definition "
  12.                 + "(see Michael Feathers "
  13.                 + "'Working Effectively with Legacy Code')"
  14.                 + "I do NOT have unit tests.";
  15.             throw new NotSupportedException(message);
  16.         }
  17.     }
  18.  
  19.     /// <summary>
  20.     /// No logic required… We're just going to write some code.
  21.     /// </summary>
  22.     public override string CodeText { get; set; }
  23.  
  24. }

You can see that my class throws a NotSupportedException because Legacy Code does not have unit tests.

DO NOT write legacy code.

Moving on to UI (User Interface)…

User Interface Code

User interface code derives from Legacy Code because it seldom has automated tests. Since this exercise assumed that unit tests were your automated tests, it would be legacy code. If you used Coded UI Tests then you could have non-legacy UI Code.

User interface code is hard to unit test. The key here is to limit the amount of code in your presentation tier. When I say presentation tier, I mean the final layer. When using Silverlight or WPF I use the MVVM (Model-View-View Model) pattern and consider the View to be the presentation tier, but both the Model and the View Model can be easily unit tested.

DO minimize the code in your presentation tier.

Here is the UICode class:

UICode
  1. public class UICode : LegacyCode
  2. {
  3.     public override UnitTest UnitTest
  4.     {
  5.         get { return null; }
  6.         set
  7.         {
  8.             var message = "You still need to test, "
  9.                 + "but another strategy is probably best. "
  10.                 + "Minimize this code and perhaps use Coded UI Tests.";
  11.             throw new NotSupportedException(message);
  12.         }
  13.     }
  14. }

Agile Code

The next class is the Agile Code – simply code that does have unit test.

There are several characteristics you’ll see for the AgileCode such as it is impossible for the UnitTest to be null. This would be the approach you are using if you write your code first, then your test.

DO write unit tests.

Agile Code
  1. /// <summary>
  2. /// Represents Agile – aka Non-Legacy code.
  3. /// </summary>
  4. public class AgileCode : Code
  5. {
  6.     private UnitTest _unitTest;
  7.     public override UnitTest UnitTest
  8.     {
  9.         // Uses the null coalescing operator since this cannot be null
  10.         // for AgileCode.
  11.         get { return _unitTest ?? (_unitTest = new UnitTest { IsUICode = false }); }
  12.         set
  13.         {
  14.             if (ReferenceEquals(value, null))
  15.                 throw new InvalidOperationException("Agile code, i.e., non-legacy code, really needs unit test unless it is UI code.");
  16.             _unitTest = value;
  17.         }
  18.     }
  19.  
  20.     private string _codeText;
  21.     public override string CodeText
  22.     {
  23.         get { return _codeText; }
  24.         set
  25.         {
  26.             _codeText = value;
  27.             if (!UnitTest.IsCompleted)
  28.                 RunUnitTestWizard();
  29.         }
  30.     }
  31.  
  32.     private void RunUnitTestWizard()
  33.     {
  34.         // If you're here, then you wrote your code first…
  35.         // Now write your unit test.
  36.     }
  37. }

Why the name AgileTest?

I could have called this code, TestedCode or UnitTestedCode, but I wanted to make a point. Here are some questions to consider:

  • Can you have Agile software development if it takes you longer to fully test your code than the length of your iteration?
  • Can you ever have an Agile development project without an effective unit testing strategy?

I contend that without a unit testing strategy you cannot truly have an Agile Software Development Process.

DO use unit testing if you plan to use Agile practices.

Test First Code

The final Code class is the TestFirstCode. As the name implies the test is written before the code. This is the “Socratic Method” for writing code. Don’t just jump to the answer, ask questions that lead you to the answer. I’m continually amazed at the clarity in requirements that I gain from this approach. If I have ANY doubt about the requirements, this is the approach I use. (And guess what? I seldom have clear requirements, even though I’m the person providing them for the code you see on this site. Requirements are hard…)

Notice that this class does not have an empty constructor. You must have a test before you instantiate the TestFirstClass.

TestFirstCode
  1. public class TestFirstCode : AgileCode
  2. {
  3.     /// <summary>
  4.     /// Use this to create Test First Code. Note there
  5.     /// is not empty constructor.
  6.     /// </summary>
  7.     /// <param name="test"></param>
  8.     public TestFirstCode(UnitTest test)
  9.     {
  10.         UnitTest = test;
  11.     }
  12.     public override UnitTest UnitTest { get; set; }
  13.     public override string CodeText { get; set; }
  14. }

CONSIDER writing the tests before writing the code.

I find it interesting that this class is SO simple. The rules are intrinsic in the structure of the code. Now you’ll note that I say “Unit Test” when in reality it should be Collection<UnitTest>. I cannot remember a time when only one unit test covered all the bases.

In a very real sense, Test First Code is the only code that was never legacy code.

Now let’s look at the TestBase and UnitTest classes

Tests

TestBase

The test base class started out pretty simple, but then grew as I wrote tests. Passed is a pretty obvious property, but Cost, Benefit, and Value came from writing tests. I realized that I was interested in that information.

TestBase
  1. public abstract class TestBase
  2. {
  3.     public static double HourlyCost = 100.0;
  4.     public TestAuthorType AuthorType { get; set; }
  5.     public bool IsCompleted { get; set; }
  6.     public bool Passed { get; set; }
  7.     public abstract double Cost { get; }
  8.     public abstract double Benefit { get; }
  9.  
  10.     public double Value
  11.     {
  12.         get { return GetValue(Cost, Benefit); }
  13.     }
  14.  
  15.     internal static double GetValue(double cost, double benefit)
  16.     {
  17.         return benefit / cost;
  18.     }
  19. }

Most of the properties in this base class are self-evident, but I thought I would put it here so that you could see some of what is going on when we get to the unit test class.

UnitTest

So finally we are getting to the unit test. We have the needed context now.

 

UnitTest
  1. public class UnitTest : TestBase
  2. {
  3.     private static double _coverageTarget = 0.95;
  4.  
  5.     public UnitTest()
  6.     {
  7.         // Unit tests are written by the developer.
  8.         // When using TDD, the tests are written first.
  9.         AuthorType = TestAuthorType.Developer;
  10.     }
  11.  
  12.     private bool _isUICode;
  13.     /// <summary>
  14.     /// Gets or sets a value indicating whether the Code under
  15.     /// test is UI (User Interface) code.
  16.     /// </summary>
  17.     public bool IsUICode
  18.     {
  19.         get { return _isUICode; }
  20.         set
  21.         {
  22.             if (value)
  23.                 throw new InvalidOperationException("UnitTests seldom work for UI layer, so minimize the code you write there.");
  24.             else
  25.                 _isUICode = value;
  26.         }
  27.     }
  28.  
  29.     /// <summary>
  30.     /// Gets the value, in currency, for the cost of the test.
  31.     /// </summary>
  32.     public override double Cost
  33.     {
  34.         get { return Minutes * HourlyCost / 60.0; }
  35.     }
  36.  
  37.     /// <summary>
  38.     /// Gets or sets the minutes required to write the unit test.
  39.     /// </summary>
  40.     public double Minutes { get; set; }
  41.  
  42.     /// <summary>
  43.     /// Gets a value for the benefit of the test. This is a value
  44.     /// between 100 and 0.
  45.     /// </summary>
  46.     public override double Benefit
  47.     {
  48.         get
  49.         {
  50.             // Obviously this code seem silly (and hopefully
  51.             // the compiler is smart enough to take it out) but
  52.             // the point is that the tests adds value whether it
  53.             // passes or not.
  54.             if (Passed || !Passed)
  55.                 return GetBenefit(TotalCodeCoverage);
  56.             else
  57.                 return 0;
  58.         }
  59.     }
  60.  
  61.     internal static double GetBenefit(double codeCoverage)
  62.     {
  63.         if (codeCoverage > _coverageTarget)
  64.             return 100;
  65.         else
  66.             return 100.0 * codeCoverage / _coverageTarget;
  67.     }
  68.  
  69.     /// <summary>
  70.     /// Gets or sets the value for the code coverage of the
  71.     /// entire code base to which the code under test belongs.
  72.     /// </summary>
  73.     public double TotalCodeCoverage { get; set; }
  74.  
  75. }

The hourly cost was created to help determine the value.

While it might seem silly to say “Developers write unit tests.” if you’ve been around unit tests for many years, those new to unit tests hear the word test and think, “Oh… Testers write tests, developers only write code.”

Developers write unit tests.

So what is the value of a unit test. At the beginning of this post I pointed to the value of unit tests in refactoring. Let’s walk through some of the extremes to understand some of this value.

If I have a large legacy code base and I write one unit test, what is the value of that unit test? I’ll tell you, it is of no value. It will not help you with refactoring – there is so much untested code that you don’t dare use that solitary unit test as a basis for deleting code.

So when does a unit test become valuable? That’s not really possible to measure on one single unit test. The value is derived from having the maximum amount of code coverage. When you do that you suddenly move to a new level. Sort of like a sonic boom, your in a whole new world of high performance.

DO maximize your code coverage so you derive maximum value from your unit tests.

Let’s revisit the notion of writing unit tests on legacy code. One might argue that “I have to start somewhere, so I’ll write that first test, then the second, then someday I’ll be done.” That’s not the case. Without exception, when I tried to write unit tests for legacy code I discovered tight coupling in the code that made it hard to test. Mocking objects can help you some, but I found that writing unit tests for legacy code was one of the most frustrating exercises I’ve ever tried.

AVOID going back and writing tests on legacy code.

Misconceptions

DO NOT wait until lots of code is written then try to write your unit tests.

There are many misconceptions about unit testing that will make it a challenge for people to pick up. If you are going to write a bunch of code and then go back and write the tests IT WILL NEVER HAPPEN!

When you wait to write the tests you now view tests as a tax rather than as someone wise like Socrates.

Even with effective mocking strategies there are times you cannot achieve 100% code coverage.

DO view untested agile code as a challenge to test (“I will find a way to test that”) but don’t lose any sleep if it really is untestable.

I did have one code base that I could not get above 98%. But there are many times that someone told me something couldn’t be tested… There were quite surprised when I found a way.

Summary

I utilized the style in the book “Framework Design Guidelines” by Crzysztof Cwalina and Brad Abrams of DO, DO NOT, etc. in this summary of the points above.

  • DO NOT write legacy code.
  • DO minimize the code in your presentation tier.
  • DO write unit tests.
  • CONSIDER writing the tests before writing the code.
  • DO maximize your code coverage so you derive maximum value from your unit tests.
  • AVOID going back and writing tests on Legacy Code.
  • DO NOT wait until lots of code exists then try to write your unit tests.

I hope this captures some of the key reasons one should seriously consider making a lifestyle change for your coding practices so your code is “Fit for Life”.