Demos Need Not Be Bad Examples


I got sent a link today of a “Walkthrough on WCF 4.0 Service in Silver Light 4.0”. Ignore the spelling for a second, as this is a foreign post. Understandable. Click. Click. You can download a PDF explaining how to create a WCF service for consumption by Silverlight.

Cutting to the chance, here is my issue:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetMessage(string number1, string number2);
}
public class Service1 : IService1
{
    public string GetMessage(string number1, string number2)
    {
        return (Convert.ToInt32(number1) 
+ Convert.ToInt32(number2)).ToString(); } }

Take a second and look at this. I understand that the point here is to demo a simple service and how it works with Silverlight, but think the example through.

Now, let’s break this down. Suppose I create the following test:

namespace WcfService1.Test
{
    [TestClass()]
    public class Service1Test
    {
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("c:\projects\test\WcfService1\WcfService1", "/")]
        [UrlToTest("http://localhost:8165/")]
        public void GetMessageTest()
        {
            Service1 target = new Service1(); 
string number1 = "1"; string number2 = "2"; string expected = "3"; string actual= target.GetMessage(number1, number2); Assert.AreEqual(expected, actual); } } }

Assuming we set things up correctly, this works fine. Looks like we have a decent service. But what if we actually run one of the easiest rabbit holes:

[TestClass()]
public class Service1Test
{
    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("c:\projects\test\WcfService1\WcfService1", "/")]
    [UrlToTest("http://localhost:8165/")]
    public void GetMessageTest()
    {
        Service1 target = new Service1(); 
        string number1 = "broken"; 
        string number2 = "code"; 
        string expected = "3"; 
        string actual= target.GetMessage(number1, number2);
        Assert.AreEqual(expected, actual);
    }
}

 

If you have been playing along, your tests are likely failing, as testing WCF services with a unit test framework are notorious buggy. Realistically, you need to treat the service as a UI and then move the code into some type of application library (aka, business tier library – although I disagree that UI is an “application tier” (another time?)).

So let’s focus solely on the algorithm for a second. Service changed to:

namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetMessage(string number1, string number2)
        {
            return Helper.GetMessage(number1, number2);
        }
    }
}

Implementation moved to class (to mimic a business library):

using System;

namespace WcfService1
{
    public class Helper
    {
        public static string GetMessage(string number1, string number2)
        {
            return (Convert.ToInt32(number1) 
+ Convert.ToInt32(number2)).ToString(); } } }

Now I can easily test this and I end up with:

Test method WcfService1.Test.HelperTest.GetMessageTest threw exception:
System.FormatException: Input string was not in a correct format.

Of course it is incorrect format, it is a “real” string and not a number masquerading as a string. What if we run our good friend Pex and Moles (white box testing):

image

At least Nulls don’t fail. Not good.

What is the problem? At first, you should note that there is no input checking. But even an input check would be useless, in a WCF service, if it does not throw the proper fault. As we are reading this from Silverlight, we probably have the service set up as a SOAP service, which means we should throw a SOAP fault message instead of a FormatException exception object. Ouch!

My point here is not to bash an example, but to once again express the need for disclaimers on examples. I had the same beef with some of the Microsoft ASP.NET guys at the last MVP Summit. On some issues we agreed to disagree, which is fine.

Actually, that is not completely true. The Microsoft code I had issue with would have passed a Pex and Moles test, although the test code would have been running as an integration test rather than a unit test. This one simply fails on all counts except passing in a NULL.

Fixing the code? Options:

Throw a soap exception if the strings are not numbers.

I won’t illustrate this, as it means another 1/2 hour up tonight. Perhaps tomorrow?

Try to parse the input. This means you at least return 0 if the strings “broken” and “code” are entered:

public static string GetMessage(string number1, string number2)
{
    int num1 = 0;
    int num2 = 0;

    int.TryParse(number1, out num1);
    int.TryParse(number2, out num2);

    return (Convert.ToInt32(num1) + Convert.ToInt32(num2)).ToString();
}

This is not a perfect solution, by any means, as I doubt a business wants garbage in to equal zero, but it at least gets us around stupid exception errors passed via a web service.

Peace and Grace,
Greg

Twitter: @gbworld

“Innocent Until Proven Guilty” And Other Outdated Concepts in the Internet Age


I just got a call about 30 minutes from my wife about a retraction I made on a website. I will not go into details, as my intent here is not to drudge up sympathy or support for “my side” or to have my “high five” crowd seek out the “other side”. This is due to a few factors:

  • I strongly believe any disagreement between two individuals is best handled in private.
  • I highly respect the work done by the person who started the firestorm, which was evident in my retraction
  • There is a lesson to learn from this incident, even without the particulars
  • I am more proactive than reactive in nature
  • The only reason to dredge up particulars would be my own vanity, and not an intent to better this world
  • Reducing to an attack only lowers the level of the discussion

History

For a bit of history, I recently read an article on the Internet that had some incorrect facts. As there was no way to directly contact the media to get things corrected, I posted a retraction comment. My reasoning was two-fold:

  1. I have a strong belief that truth should prevail. This comes partially from my training in journalism (mass communications, with a specialty in film and video (broadcast journalism)) and partially from my belief in a God of truth and justice. We can return to this later.
  2. A strong sense of pride in the actions a certain person has taken, which I felt were lessened by the nature of the article.

My retraction note was read tonight by someone who saw it as an attack. And that person posted it on a social media site where I was skewered without one of the individuals writing me personally and asking my intent. Worse, many of the individuals have personally attacked the person I have pride in, which has hurt me intensely. Personally, I would rather use this outlet to skewer them back, but I would rather this be a productive post. Clarification: Most of the truly negative comments have been received via email.

Intent

Intent is an extremely important concept in a court of law. If a person kills someone accidentally, without intent, the sentence, if any, will be lesser. If there was intent to kill, it is more likely there will be a conviction. The charged starts out innocent until proven guilty. And, if proven guilty, a sentence will be passed. If there was intent to kill, the sentence will be harsher than if there was no intent.

While intent can be inferred by actions, it requires a large amount of evidence to prove intent by inference. More likely, it is proven through the actions of the individual on trial.

In the court of public opinion, the best way to determine intent is to ask. Unfortunately, we are often so caught up with loyalties to a specific group (“our side” versus “their side”), we simply don’t ask. And, since it is not a court of law, we have no culpability in the matter. We simply do.

My Side versus Your Side

I see this as one of the most damaging trends in America. Rather than examine reporting at face value, we are more likely to believe or disbelieve based on the news source. Thus, to some, Fox News == Faux News and to others CNN == The Clinton News Network. In reality, each leans, but not nearly as far as the “other side” would have one believe.

What is sad about this is most of us are human beings, not Liberals or Conservatives, Democrats or Republicans. In fact, most of us would think someone who defined themselves solely by labels to be sick, even if we agreed with the viewpoints of the label in question. Think about it. How would you view a person who refused to speak to his own teenage son or daughter because the son or daughter debunked a story on his favorite news channel?

Innocent Until Proven Guilty

The my side versus your side often leads to a “guilty until proven innocent” type of viewpoint. Many years ago, I had an individual state I should be arrested and dropped off in Iraq in an insurgent area for voting for Bush. He could not fathom that my vote was focused on factors other than the war and that Kerry had not shown me anything that gave me confidence in his ability to lead this country. He also could not accept I voted the lesser of two evils, based on what I knew at the time. Four years later, I “threw my vote away”, as I could not vote D or R in good conscience.

Please note that my statements above are not derisive to anyone who voted for Kerry in 2004, as I don’t see things through “my side/your side” glasses. It is sad I have to add these types of disclaimers, however, as I think I have stated my point very clearly. I have found, when I don’t, someone assumes that anything not clearly stated must be an attack.

And that is the crux here. So many people read into what is stated rather than actually read what is stated. I am not sure why this is. Perhaps it is that our society is becoming less educated due to political correctness or trying to make sure we don’t damage someone else’s self esteem.

Back to the Issue at Hand

In my retraction, I stated facts. I did not attack the individual who was incorrectly accredited. In fact, I was quite complementary on their accomplishments over the past few years. I hope the individual reads this blog entry, as the individual has ensured I cannot post anything in defense on the social media site. This is probably an oversight, as well, and I do not hold the individual personally culpable, as I doubt the individual personally stopped me from posting.

What was uncharitable in the whole mess was the comments fired back from friends of the individual. Not one took the time to ask me intent prior to stating their comments. If someone had asked me, I would have stated my intent and those now projecting me as the next Hitler would have discovered I meant no ill will on anyone.

I am deeply hurt by this event, as I did what I felt was ethically right. Given the same circumstances, I might have taken a bit more time to craft a note to ensure all disclaimers where in place, but I would have still sent the note. I still have this crazy belief truth matters.

At this time, there are some who are brandishing torches. Unfortunately, they are not just aiming at me, but also at the ones that truthfully should have credit for their accomplishments. Rather than focus solely at me and my actions, even in their wrong assessment of my intent, they are lashing out at others who were not involved.

Final Note

Here is a note I tried to post on the social media page. Unfortunately, I was no longer given permission to post, so I have to do it here (with names and groups in {} to avoid dredging this up to the public awareness:

I wish I had received the same charity and benefit of the doubt that I gave {person} in this matter. I never accused her of anything, as I assumed it was a media mistake.  I just reached out, as I have with many news stories over the past few years, and tried to make sure the truth was told. This comes from my high sense of ethics and my classwork in journalism.

I am deeply hurt at the judge, jury and executioner attitude of so many in this thread. Perhaps determining my intent would have been better before pulling the switch. All it would have taken was a message sent to my account.

If someone would like to send me a personal message telling me your feelings, we can discuss this. If you would rather attack me publically, I want no part in it.

I sat in the living room when the idea bake. I watched as {person1} posted on {site}. I saw the {group} consume time for many, many months. This is not deriding anyone else’s efforts, nor did {person1}ever ask for any fame in this, but my sense of justice won’t allow me to be silent as you bash her and try to take this away from her.

If you want to attack me, that is fine. I would prefer you do it in private, but you are allowed to choose your forum, as we live in a free country. But don’t destroy {person1} for my actions, as that is an unjust action. And, please, take the time to actually read through the entire retraction and see there was no ill intent. I wish I felt the same for some of the comments here. I am deeply hurt.

I am most hurt that not one of you asked me before firing the bullet. I would have at least given that benefit of the doubt to someone posting something about me.

The Point

The point is we have become an uncivil society. Years ago we would have told our neighbor his {something in his yard} was ugly; today we sue him. Years ago we would have asked someone what they meant by a comment; today we fire it off in the social media sites and watch our [friends/followers] high five and execute the other person.

The interesting thing is we don’t feel any remorse because we don’t see the damage done in the wake. If you think about it, it is much like warfare. In the days you had to run someone through with a sword, war was very hard. And, compared to today’s battles, fewer people died. But as the weapons allowed you to kill without seeing the carnage, war became easier. In like manner, telling off your neighbor, face to face, is hard. Sending him a letter is easier. Getting a lawyer to send the letter is even easier. And skewering him on Facebook is even easier.

This is sad, however, as many neighbors might be easier to approach if you asked them why. And, if you heard the reason, you might actually have sympathy and agree. If not, you might get the neighbor to see your side. Instead, today, we choose to be enemies. Very tragic.

I am not sure if anyone in the thread will read this, but I hope they will. I bear no ill will, nor have I ever. And if someone had actually asked my intent prior to firing off nasty comments, perhaps they would have seen my sole focus was truth. Perhaps I could have stated it in the disclaimer language so popular, and sometimes necessary, today? If so, that is my sole crime, not the one I have been convicted of without a trial.

Peace and Grace,
Greg

Twitter: @gbworld