Monday, October 6, 2008

How does the Luhn Test work?

The code to find credit cards in a block of text is available on GitHub: Credit Card

The Luhn Test or Luhn Algorithm is a test of a series of numbers to determine if they could be a valid credit card. The algorithm is fairly simple and I'll demonstrate it with 4 digits.

The number to test: 4 - 5 - 6 - 7

Step 1: Starting from the right, double every second digit:

8 - 5 - 12 - 7

Step 2: If the result of the doubling ends up with a 2 digit number then add those 2 digits together.

8 - 5 - 3 - 7

Step 3: Add all the digits together.

8 + 5 + 3 + 7 = 23

Step 4: Mod the number against 10.

23 % 10 = 3

Step 5: Is the number exactly divisible by 10?

i.e. does 3 equal 0?

If it is (exactly divisible by 10) then it passes the Luhn Test and could be a credit card number.

Ask a simple question

I asked on Stack Overflow why i++ followed by ++i would compile but ++i++ would not compile under C#. When I saw the answer it was obvious why it was a simple question. I hadn't spent very long thinking about it and breaking the problem down.

What I found interesting though was that the question was immediately voted down and had a -2 vote after a few minutes. I checked again an hour later and it was back to zero.

I originally titled this post as "ask a stupid question" before realizing that the question wasn't stupid but one that many programmers could have. It was well answered and I am the wiser for it and I hope someone else is as well.

Based on this micro analysis, I wonder if Stack Overflow has a few elitist members who are fast to the draw and vote down anything that's a simple-to-solve problem.

Best practices for creating websites in IIS 6.0

 Omar AL Zabir has a good write-up on Best practices for creating websites in IIS 6.0.

Friday, October 3, 2008

Stopping a Hyper-V VM that refuses to stop

I posted this question on the Microsoft Tech Forums: I have a Vista (x64 Ultimate) VM that refuses to stop. (Host is Server 2008 x64.)  I've tried using the hard stop but it remains in the state of Stopping. How do I stop it from here?

And received this (great) answer from Stephen Edgar:

If you grab Sysinternals Process Monitor you can check each VMWP.EXE Process (Virtual Machine Worker Process) and Select 'Properties' and each process will have a reference in the 'Command Line' section of the properties dialogue which refers to the VM's GUID which can be found for each VM by examining the configuration files contained in C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines and then just kill the approriate VMWP.EXE Instance and that should kill that VM without affecting any of your other currently running VM's.

This is the second time that I've had this problem with Hyper-V and I have no doubt that it won't be the last. Thanks Stephen!

Thursday, October 2, 2008

Buying a PCI bus card on Craigslist

 Here's a question that I can't find an answer to. I've found a PCI bus card (TV tuner) on Craigslist that I want to buy. The seller says that they've got rid of their PC and now have a Mac and so don't have a need for this card. How can I find out if this card is working without lugging a PC around to his house and installing it then and there before buying it?

Wednesday, October 1, 2008

How not to leave breadcrumbs

I was listening to Jeff Atwood and Joel Spolsky's podcast from a couple of weeks ago (podcast 22) and Jeff mentioned the Fight Club rule (The first rule of fight club is you do not talk about fight club) and how Stack Overflow is not intended to be a site that discusses itself. i.e. The first rule of Stack Overflow is you do not talk about Stack Overflow on Stack Overflow. This is also the reason that I'm not posting this on Stack Overflow and instead posting it here.

One of the reasons that Jeff doesn't want Stack Overflow discussion on Stack Overflow is that he doesn't want Google et al indexing Stack Overflow pages that discuss how the site works. When a user finds Stack Overflow through a Google search it will hopefully bring them to a page with a question and solution to a problem that they might be trying to solve. Not to a meta-discussion.

So how do you solve this problem. In my opinion fairly easily: You dynamically create the robots.txt file that's in the root of the site each time it's requested (instead of having a static one). You then query the database for all questions that are marked with the "stackoverflow" tag and add their URL's to the disallow section of the robots.txt content that you're going to return to the requesting search-bot. That way the meta-discussion won't be indexed by the search-bot.

After typing that I thought of what is probably an even easier solution. When you retrieve and construct the page from the DB just add the <META name="robots" content="NOINDEX,NOFOLLOW" /> to the <head> tag in the page if it has a "stackoverflow" tag and Google won't index that page.

Not sure why I got so carried away with dynamically generating the robots.txt page/file...

Inheritance Question

In C#, which Console.WriteLine() would be hit?
Without consulting your compiler, take a look at the code and then vote your answer.

    public class A
    {
        public void Bar(int n)
        {
            Console.WriteLine("A.Bar");
        }
    }

    public class B : A
    {
        public void Bar(double n)
        {
            Console.WriteLine("B.Bar");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.Bar(5);
        }
    }

Vote your answer now before you read any further.

 

At the time of writing this the general consensus was wrong. Note - I was also wrong when I took a guess at what the answer is.

The answer is that B.Bar will be hit and not A.Bar

My theory behind why this happens is that I think that C# walks the inheritance tree from the outer class inwards and each time that it finds a matching function name it then checks to see if all of the parameters can be cast to the first method that it finds. If so, it then uses that method.

With the help of a friend, I then tested this on Java. It turns out that Java does not behave in this manner. The Java compiler, I'm guessing takes 2 walks of the inheritance tree. The first time it looks for an exact match. The second time it attempts to do a cast. So the answer for a Java compiler would be a print of A.Bar. However, this question was asking about C#.

Here are the results of the poll

EDIT: Name mangling - that's how it does it in Java and C++. It creates a unique name for each function call based on its function name and the parameter types. I just remembered that.