Wednesday, July 17, 2013

Ever replace a lost wallet or purse? Losing your smartphone can just as worrisome

Most people I think have a rough idea of the steps involved in replacing a lost wallet or purse. Photo ID, bank cards, insurance cards, and if you're really unlucky, social security cards or birth certificates all need replaced. Sometimes in a certain order. And the things you have to do to replace each thing can often become mini projects of their own. Now enter the lost smartphone.

Replacing a lost smartphone includes everything that replacing a dumbphone involves such as contacting your service provider to discuss options for a new device, contract, or even a new service provider entirely. But smartphones have one really important element to them that you should know the implications of BEFORE you've lost yours: connectivity to your data. As of the time of me writing this, every smartphone I've seen to date: expects you to connect it to your data, and then caches the credentials you provided so that it can always access your data without you having to provide those credentials all the time. The upside to this is usability of course. Nobody wants to be typing passwords into any phone, like ever. But the downside is that the jackass that picked up your phone and walked off with it is now connected to your data. This means he gets to status jack you, dig through your email, and soon much more.

Smartphones and people are evolving together into so much more than something that allows us to call, text, and take pictures; they are becoming hubs into our data which we use to make decisions and take action. How many times have you pulled over pull up a route to somewhere, let someone know you're on your way, maybe put on some music or simply pull up an email that contains details of a something that needs thought through so you can figure things out along the way, such as the suite number you need to find of the interview you're headed to, or the campsite number where you are to rondevu with your friends and family. As people have come to trust smartphones to provide essential information, they also must trust that their data is accessible(even though battery life leaves a lot to be desired).

All the while, the cloud is omnipresent. Highly reliable and available are attractive specifications to smartphones wanting to deliver your data to you wherever and whenever you need it. And whether you are embracing the cloud, having it foisted upon you, or could care less, the cloud's ability to keep and dish out our data reliably and securely has thus create a celebrated, offensive or negligible (accordingly) marriage between it and your smartphone. And once you bless this union with your credentials, they don't need you anymore to stay connected.

In fact all they really need to be together is a data connection, some battery, and your credentials. This presents a few potentially potent problems when your smartphone fall into the hands of someone else AND it remembers your credentials. At least for now (until quantum computing), you don't have to worry about your phone spilling your password, thanks to one-way encryption. But while in the hands of its new owner, your data, sensitive as it may or may not be, is highly available to that person. Whether you get your phone back from its benevolent captor, status jacked for a good laugh, or have your identity stolen is simply a matter of how much the universe (dis)likes you. How concerned you are about this seems like it would be a function of how vulnerable you are when your data is in someone else's hands, and is as good a thing as any to risk-manage.

Fortunately, like replacing your wallet or purse, systems exist and are improving all the time for dealing with lost or stolen smartphones. The two I'd like to illuminate are GPS tracking functionality and what i'm going to just call "active session" management tools provided by the services that host your data. Instead of talking about them here myself, I will refer to posts which already exist to describe exactly these things.
Windows Phone GPS tracking
Android GPS tracking
Facebook active session management
Gmail active session management

Wednesday, May 16, 2012

Maybe you should leave those key lookups alone

So.... In my last post I shined a flashlight on the concept of creating covering indexes to eliminate key lookups for your queries. Turns out that covering indexes are killer to tables that endure constant updates. After adding covering indexes to several of these kind of tables, my database saves came crashing to a halt, overwhelmed with locks. I'm not sure yet what the answer is, but for the time being, I've made amends with those key lookups.

Monday, April 16, 2012

Eliminate those key-lookups

If you are developing for any database involved application that deals with a moderate quantity of data and you stick around long enough, you will find yourself investigating performance. Maybe you operate like our team did on a recent project where we started with some base expectations of performance, moved forward until gross disappointment set it, hacked at performance enhancements until we felt better about things, adjusted our expectations, and moved on. This cycle repeated itself many times and each time, I learned something new about how not to do a variety to things.

I won't spoil the fun for you, but if you are using SQL Server 2005 or greater, I will offer this one thing that I found enlightening: key-lookups in my query execution plans were my failure to understand(or care about) included columns. I would explain more myself, but that has already been done so very well in this post here by Joe Webb.

So this still counts as a blog entry right?

Cheers!

Friday, February 3, 2012

Comparing nullable dates

Sometimes working with Nullable<T> can be like arguing with a pedantic nerd.  And while being able to do this effectively is probably not something you want to advertise in the skills section of your résumé, you can win friends by translating for such challenged communicators.  Just as such, you can win programmer friends by writing extension methods that simplify overly verbose operations with... say... Nullable<DateTime>.

If you accept all that, then I present this here "friend catcher" to you.  Keep the anecdote close to heart that with great power comes great responsibility and always remember that you cannot hold me responsible if you crash Facebook with all the new friends you amass.  I will ignore any emails I receive blaming me so.

/// <summary>
/// Compares two nullable dates using only the day, month, and year.  
/// Ignores hours, minutes, seconds...
/// </summary>
public static int DateCompare(this DateTime? dt, DateTime? compareDate)
{
    DateTime? a = dt.HasValue ? dt.Value.Date as DateTime? : null;
    DateTime? b = compareDate.HasValue ? compareDate.Value.Date as DateTime? : null;
    return Nullable.Compare<DateTime>(a, b);
}

Wednesday, November 30, 2011

Unlocking the power of IEnumerable T

Since becoming proficient with using LINQ to find shit, I have never looked back. That is until I run into collection types that I can’t immediately query with LINQ expressions. And it seems like every time I encounter such a collection, my first course of action is hate on it for a little while. ControlCollection.FindControl hardly counts as a way to find controls and NameValueCollection has no apparent way to find things just to mention a couple. This goes for any of your custom collection types as well, unless you implemented your own search methods.

So one thing I have found myself doing time and time again is adding an extension method called AsEnumerable() to these types so that I don’t have to hate them. This method returns the collection as an IEnumerable<T>, unlocking a pile of extension methods that allow me to query and otherwise manipulate said collection in more ways than I can imagine.
Many collection classes in the framework already provide such a method, but many still do not. Also note that the non-generic IEnumerable is not the same as the generic one and is far lesser in power compared to its generic counterpart IEnumerable.
As an example, lets do this for the ControlCollection class so we can find all the controls on a page that have a certain css class.

public static IEnumerable<Control> AsEnumerable(this ControlCollection controls)
{
   foreach (Control ctrl in controls)
   {
       foreach (var subCtrl in ctrl.Controls.AsEnumerable())
       {
           yield return subCtrl;
       }
        yield return ctrl;
   }
}


Now that the extension is in place, we can write some code like this to find what we want:
protected void Page_Load(object sender, EventArgs e)
{
  IEnumerable<Control> blueControls = Controls.AsEnumerable().Where(c => c.CssClass.Contains(“blue”));
}

And for working with QueryStrings, this extension for the NameValueCollection class does the trick:
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this NameValueCollection col)
{
    return col.Cast<string>().Select(key => new KeyValuePair<string, string>(key, col[key]));
}

Allowing us to do something like this:
Request.QueryString.AsEnumerable().Where(c => c.Key == "someParameter");

Go forth now and be cured of lame collection hate knowing that you can spend less time crawling collections and more time racing against the clock that is always counting, always ticking away at the relevancy of your current developer skill set.  Hell, with all the extra time you can spend now writing LINQ instead of wrestling with iterators, think of how much longer you can procrastinate now on that certification your boss has pestering you about...

Friday, March 12, 2010

Making Sharepoint RTE aways create a popup window for editing

stick this hackjob of a function in your head tag and call it at the end of your document.

    function fixEditorButtons() {
        // rewire rich text editor buttons to popup source view RTE window in content mode
        var anchors = document.getElementsByTagName("A");
        if(anchors.length == 0) return;
       
        var rteArray = new Array();
       
        // gather up the rich text editor edit buttons
        for(var i = 0; i < anchors.length; i++) {
            if(anchors[i].attributes.getNamedItem("title") != null) {
                    if(anchors[i].attributes.getNamedItem("title").value == "Edit Content") {
                        rteArray.push(anchors[i]); }}}
        for(var i = 0; i < rteArray.length; i++) {
            var oEditButton = rteArray[i];
           
            // grab the code as SP created it
            var code = new String(oEditButton.onclick);

            // snag the control name of the RTE current display control
            var controlnameBeginIndex = code.search(/RTE2_LaunchEditor/) + 27;
            var temp = code.substring(controlnameBeginIndex);
            var controlnameEndIndex = temp.search(/\'/);
            var controlName = temp.substring(0,controlnameEndIndex) + "_displayContent";
       
            // find where we need to insert our additional code that creates the popup window
            var insertPoint = code.search(/cancelBubble/) - 67;
                       
            // build new code, start with old code
            var newcode = code.substring(0,insertPoint);
            newcode += "RTE2_SaveHtmlStateIfChanged('" + controlName + "');";
            newcode += "var instanceVariables=RTE_GetEditorInstanceVariables('"+ controlName +"');";
            newcode += "RTE2_LaunchEditor( instanceVariables.aSettings, instanceVariables.clientId,
                                        instanceVariables.displayContentElement.id, instanceVariables.emptyPanel.id,
                                        instanceVariables.hiddenInputField.id, instanceVariables.webUrl,
                                        'True', false,true);";

            newcode += code.substring(insertPoint); rteArray[i].onclick =
                                    Function(newcode.slice(20,newcode.length - 1));
        }
    }

Thursday, March 6, 2008

Are parents responsible the delinquency of teenaged SPLists?

Ever since I hooked up with Microsoft and began fathering little bits of softwares, I always thought that I would raise good code. However, like many have endured, one of my softwares began acting defiant and outrageous. We debugged the software repeatedly and how stubborn it was! Certainly didn't get any of that from MY influence! ahem! Lets get into the psyche of this little rebel a bit...

public static SPList GetListByName(SPWeb rootWeb, string listName)
{
    foreach (SPList list in rootWeb.Lists)
    {
        if (list.Title.Equals(listName,
            StringComparison.InvariantCultureIgnoreCase))
        {
            return list;
        }
    }
    
    foreach (SPWeb web in rootWeb.Webs)
    {
        SPList resultList = GetListByName(web, listName);
        if (resultList != null)
        {
            return resultList;
        }
    }
    
    return null;
}

Take a look at the base case where the list is found and returned. Pretty standard stuff no? What is not (by even a far stretch of even the most eclectic programmer's imagination) obvious here is that the list being returned has a... how do I say this.... special Views property (of type SPViewCollection). The crime here is that these views are wrong. No, The collection isn't empty, it doesn't throw exceptions when you try to access it, it isn't null, it doesn't contain any number of views other than the amount of views it really does have, and the views aren't uninitialized or anything. They are wrong because they only have two columns, no matter what.

The victim of such a crime is one that wants the list for its views and is given this list as joke. How did I deal with such reckless behavior? Well, it didn't go down without a hairy fight thats for sure, but in the end, I taught it to return regular SPLists that were not special.

SPSite s = SPContext.Current.Site;
SPList l = s.AllWebs[rootWeb.Name].Lists[listName];
return l;

Tuesday, February 5, 2008

Revision# 6,450,002.0 of the command line parameter parsing piece

One of the most re-invented wheels of all time is the one which takes options from a set of command line arguments. It's a fine tradition to reinvent this wheel and here I present the fruits of today's celebration.
static void Main(string[] args)
{
    string option1 = getCommandLineParameter("-option1", args);
    string option2 = getCommandLineParameter("-option2", args);

    if (option1 != null && option2 != null)
    {
        // react to these parameters
    }
    else
    {
        // react to these parameters not being given
    }
}


private static string getCommandLineParameter(string optionName, string[] args)
{
    short i;
    for (i = 0; i < args.Length; i++)
    {
        if (args[i] == optionName)
        {
            if (args[i + 1] != null)
            {
                return args[i + 1];
            }
            else
            {
                return null;
            }
        }
    }

    return null;
}

Monday, January 28, 2008

If operating systems were beers...

  • DOS Beer

  • Requires you to use your own can opener, and requires you to read the directions carefully before opening the can. Originally only came in an 8-oz. can, but now comes in a 16-oz. can. However, the can is divided into 8 compartments of 2 oz. each, which have to be consumed separately. Although soon to be discontinued, a lot of people are going to keep drinking it after it’s no longer available.

  • Mac Beer

  • At first, came only in a 16-oz. can, but now comes in a 32-oz. can. Considered by many to be a “light” beer. All the cans look identical. When you take one from the fridge, it opens itself. The ingredients list is not on the can. If you call the brewery to ask about the ingredients, you are told that “you don’t need to know.” A notice on the side reminds you to drag your empties to the trash can.

  • Windows 3.1 Beer

  • The world’s most popular. Comes in a 16-oz. can that looks a lot like Mac Beer’s. Requires that you already own DOS Beer. Claims that it allows you to drink several DOS Beers simultaneously, but in reality you can only drink a few of them, very slowly. Especially slow if you are drinking the Windows Beer at the same time. Sometimes, for apparently no reason, a can of Windows Beer will explode when you open it.

  • OS/2 Beer

  • Comes in a 32-oz can. Does allow you to drink several DOS Beers simultaneously. Allows you to drink Windows 3.1 Beer simultaneously too, but somewhat slower. Advertises that its cans won’t explode when you open them, even if you shake them up first. You never really see anyone drinking OS/2 Beer, but the manufacturer (International Beer Manufacturing) claims that 9 million six-packs have been sold.

  • Windows 95 Beer

  • No one drinks it much yet, but a lot of people have taste-tested it and claim it’s wonderful. The can looks a lot like Mac Beer’s can, but tastes more like Windows 3.1 Beer. It comes in 32-oz. cans, but when you look inside, the cans only have 16 oz. of beer in them. Most people will probably keep drinking Windows 3.1 Beer until their friends try Windows 95 Beer and say they like it. The ingredients list, when you look at the small print, has some of the same ingredients that come in DOS beer, even though the manufacturer claims that this is an entirely new brew.

  • Windows NT Beer

  • Comes in 32-oz. cans, but you can only buy it by the truckload. This causes most people to have to go out and buy bigger refrigerators. The can looks just like Windows 3.1 Beer’s, but the company promises to change the can to look just like Windows 95 Beer’s - after Windows 95 Beer starts shipping. Touted as an “industrial strength” beer, and suggested only for use in bars.

  • UNIX Beer

  • Comes in several different brands, in cans ranging from 8 oz. to 64 oz. Drinkers of UNIX Beer display fierce brand loyalty, even though they claim that all the different brands taste almost identical. Sometimes the pop-tops break off when you try to open them, so you have to have your own can opener around for those occasions, in which case you either need a complete set of instructions or a friend who has been drinking UNIX Beer for several years.

  • AmigaDOS Beer

  • The company has gone out of business, but their recipe has been picked up by some weird German company, so now this beer will be an import. AmigaDOS Beer never really sold very well because the original manufacturer didn’t understand marketing. Like UNIX Beer, AmigaDOS Beer fans are an extremely loyal and loud group. It originally came in a 16-oz. can, but now comes in 32-oz. cans too. When this can was originally introduced, it appeared flashy and colorful, but the design hasn’t changed much over the years, so it appears dated now. Critics of this beer claim that it is only meant for watching TV anyway.

  • VMS Beer

  • Requires minimal user interaction, except for popping the top and sipping. However, cans have been known on occasion to explode, or contain extremely un-beer-like contents. Best drunk in high pressure development environments. When you call the manufacturer for the list of ingredients, you’re told that it’s proprietary and referred to an unknown listing in the manuals published by the FDA. Rumors have it that this was once listed in the Physicians’ Desk Reference as a tranquilizer, but no one can claim to have actually seen it.

    Note: I did not author this, I found it on the net somewhere.

    An initial public offering

    I no longer know how this thing works and so I must set it free. My old roomate remembers me cracking away at this some years ago - I'll take his word for it. I should probably remove VBScript from my résumé (and good riddance!). Save this text into a text file with a .wsf extension (i.e. playlistgenerator.wsf) It is a playlist generator BTW.
    <job>
      <object id = oRS progid = "ADODB.Recordset"/>
      <reference object = "ADODB.Recordset"/>
      <script language = "VBScript">
        Option Explicit
    
        Sub DefineAndOpenRS()
          'Define and open the disconnected recordset
    
          With oRS
            .ActiveConnection = Nothing
            .CursorLocation = adUseClient
            .CursorType = adOpenStatic
            .LockType = adLockBatchOptimistic
            With .Fields
              .Append "MyStuff", adVarChar, 255
            End With
            .Open
          End With
        End Sub
    
        Sub InsertRow(sData)
          'Add data to a new row in the recordset
    
          oRs.AddNew
          oRS.Fields.Item("MyStuff").Value = sData
        End Sub
    
        Function ConcatRows()
          With oRS
            .MoveFirst
            ConcatRows = ""
            Do
              if oRS.Fields.Item("MyStuff").Value = "playlist generator" or _
                 oRS.Fields.Item("MyStuff").Value = "playlist.m3u" then
                ' do nothing
              else
                ConcatRows = ConcatRows _
                & .Fields.Item("MyStuff").Value _
                & vbNewLine
              end if
    
              .MoveNext
            Loop Until .EOF
          End With
        End Function
    
        Dim lItem, sResults
    
        DefineAndOpenRS
    
        dim fso
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        dim folder
        Set folder = fso.GetFolder(".")
    
        dim fileList
        Set fileList = folder.Files
    
        dim playListFile
        Set playListFile = folder.CreateTextFile("playlist.m3u", true)
    
        dim file
        for each file in fileList
          if file.Name = "playlist generator.wsf" or file.Name = "playlist.m3u" then
            ' do nothing
          else
            InsertRow(file.Name)
          end if
        next
    
        oRS.Sort = "MyStuff ASC"
    
        do while not oRS.eof
          playListFile.WriteLine(oRS.Fields.Item("MyStuff").Value)
          oRS.movenext
        loop
    
        playListFile.Close
    
        sResults = ConcatRows()
        oRS.Close
        MsgBox sResults, vbOkOnly
      </script>
    </job>