Thursday, June 14, 2012

The biggest news from Apple.

I’m not an Apple fan-boy exactly, but I do respect the company and am constantly impressed by them. I’ve learned never to bet against them, especially in my stock portfolio.
So I’ve been keeping an eye on the latest news from their developer’s conference, and I think one story has been under-told.

This one.

Auto week reported this. For the most part, the mainstream media (and even the tech community) has been ignoring it.

Let me summarize, to save you the time of reading: Apple has agreements to put Siri in the cars made by these auto makers: BMW, GM, Mercedes, Jaguar, Audi, Toyota, Chrysler and Honda.

Ford already has a tight relationship for voice control software with Microsoft. Besides Ford, Apple gets…um… everyone else? OK, Hyundai and Ferrari aren’t in the list, but come on, everyone else is.

This is a big fat hairy deal for Apple. And a big, fat, hairy coup. Microsoft was really the first one to do this. They pioneered the trail and owned the market with Ford. And then Apple just stole it from them – not one or two other potential clients – but *all* the rest. I mean, what were the MS sales guys doing while Apple was pitching this? Were they off trying to flirt with Siri?

I think it’s a big, fat, hairy hit for Microsoft. OK, it’s a small hit for Google, too, but Google is only loosing what it never really had – not being a real player in this market anyway. But Microsoft… man… talk about shutting down an entire part of the business! Sure, they’ll hang onto Ford for a while, but how long before Apple gets that? How long before someone wants the same voice system in their Lincoln that their friends have in the Beemers? And even if they keep Ford, the growth potential of the market is gone .

And think about what this means for Apple. While all the world has been cursing or praising Windows 8 and MS is paddling hard to catch up in the ARM space (or is it ARMs race?), Apple flanked them big. Think about it. Think about what happens if you get used to Siri in your car giving you directions to a new Sushi place. That means extra revenue for Apple. It means add dollars from the Sushi place. It means integration with Apple’s new maps (take *that* Google). It means integration with Facebook and Open Table (they announced that too, by the way) so you can see if your friends liked the Sushi place and make a Siri-based reservation on the way. And it means a new reliance on Siri. So, if you are on a trip and you rent with Hertz, you’ll want Siri. If you are at home and you’re wondering where to grab dinner, you‘ll want Siri. It means getting people so used to the service that they’ll look for it everywhere.

And what amazes me, again, is that Microsoft invented the market, but just didn’t have the ability to capitalize on it. Microsoft is the true innovator here (something they claim to do more than they really do). But – most likely—Apple is the true winner.

Now, I wish I wouldn’t have sold that Apple stock…..
--kevin

Monday, June 4, 2012

Debugging tricks

I was sitting with someone the other day and watching him debug an application and it made me want to post a couple tricks with the debugger that can help. 

first: learn some of the simple shortcuts. F5 starts the app with debugging ctrl+F5 starts it without. I was surprised by the fact that he took his hands off the keyboard to look for the mouse so that he could click the little icon on the toolbar every time he wanted to run something.

F9 sets (and clears) breakpoints – this is probably the handiest thing--  and F11 will “step into” a method, while F10 will step over it.
Those are the basics.

In addition, you can go to view->other windows->command window in Studio and have access to all the debugger info (and then some). A couple of the most useful things are:

        “Debug.DisableAllBreakpoints” and “Debug.EnableAllBreakpoints”. These alone are so useful, it’s scary. On any complex application, you know, the one that you’ve been working on for the last 3 days without coming up for air, you’ve got lots of steps to do before you get to the “good stuff”. Once you do actions X + Y, you want to step through loop Z to see what it’s doing. But you don’t want to step through Z until you’ve done X and Y. So you can end up stepping through lots of code that you really don’t care about. But if you disable all breakpoints, then run, do X and Y, then Enable all breakpoints, you will fly right past your breakpoints until you need them. Very cool.



      typing ? followed by a variable (there needs to be a space between the ? and the variable name) will print the contents of the variable. This is basically the same as hovering over the variable with your mouse or doing a quick watch, but typing it is easier when you’re looking for someArray[i].someObject.someProperty . I’ve seen lots of people spend all day trying to “drill down” in hover over without clicking on the wrong thing and making it vanish. I do it too, but the debug command is way easier. 

 
      Also, remember conditional breakpoints. When you set a break point, you can right click on it and select “condition”. This will open a dialog that will allow you to enter whatever you like and stop only when the condition is true. So, you can break on the 321st iteration of the loop without having to step through the first 320. 

.  Hope this all helps. Please list some of your favorites below. 
-    --kevin

Friday, June 1, 2012

Extension methods



Yay! I finally have an excuse to use extension methods.
First, a brief explanation of what they are. Suppose you have an object – say a string. And you want to add some functionality to it. You could subclass the object (assuming it’s not sealed) and add your new method.

But if all you want to do is add one tiny little method, subclassing seems a bit top-heavy, doesn’t it? Besides, unless you’re using an object factory pattern (my favorite design pattern, by the way), you face the prospect of finding and changing every object instantiation in every line of code of your project to replace the parent with the child class. On large, multiple developer projects, this gets rough.

Microsoft gives you a work around. What you can do is to add a special static method somewhere in your code and .Net will automagically assign this to the desired class.

For example,
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ''.''?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
The magic is the keyword “this” in the method signature. “This” tells the compiler that “WordCount” is an extension method on the string object.
The end result is that your code can now do something like:

       string x = "this is a test string";
       Console.WriteLine(x.WordCount());

And the Console will show the word count of x, just as if “WordCount” was a public method on the string class.

I’ve thought this was cool for a long time, but couldn’t find a use for it.

But, Dmytrii Nagirniak posted a reply to a thread on Stack Overflow that provides an awesome use:

http://stackoverflow.com/questions/1725903/determine-if-datacolumn-is-numeric

To sum it up, .Net has no “IsNumber()” method. So looking at a potential data value, it’s really hard to know if it’s a number type. What you end up with, is a complex if statement that does a GetType(), but then has to check to see if the type is a Double OR a Single OR an Int32 OR an Int64 OR a UInt16 OR…. Etc.

Putting this into an extension method makes a ton of sense. (I’m not sure why Microsoft didn’t already do this, frankly). This means that developers don’t need to deal with this mess each time, but just have to call
x.IsNumber().

The code for the extension just looks like this:
  public static Type[] numericTypes = new[] { typeof(Byte), typeof(Decimal), typeof(Double),
        typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte),
        typeof(Single), typeof(UInt16), typeof(UInt32), typeof(UInt64)};
 
 public static bool IsNumber(this object x)
    {
        if (x == null)
            return false;
      
        return numericTypes.Contains(x.GetType() );
    }

And this allows  you to instantiate any object and define whether the object is a number type.

A second (useful) variation of this examines whether a DataColumn (ie, in a DataTable) is a number type. It’s different because the DataColumn.GetType() would just return “DataColumn, you moron”, so you have to look at “DataColumn.DataType” :
  public static bool IsNumeric(this DataColumn col)
    {
        if (col == null)
            return false;
        return numericTypes.Contains(col.DataType);
    }


Pretty cool stuff.
--kevin