1wearandtearType-agnostic function mapping.  This is old hat for LISP programmers, but for other type-defined languages, it often looks as horrific as staring at a tragic automotive accident.

In certain scenarios (subjective environmental analysis), there exists a near-equal referencing of data mapping between two pairs of associated relations.  Key and value.  You can define a global data structure (hash, dictionary, array, list, etc.) or wrap it in a function.  Either way, when you spread out the atomic pointer allocations the end result is often a wash.

Whether you stuff the data in one place and a function wrapper in another, or combine them, the overall footprint is not that different, depending upon the language construct and syntactical constraints.  Yes, I’ve been reading some books with big words, but stay with me, mmkay?  Either model is still better than one of them with repeated code fragments elsewhere (or within iterations, etc.), hence the advantages of refactoring.

function CMWT_WMI_ChassisType (MatchValue)
  dim x, y, z, result
  x = "1=Virtual," & _
    "2=Blade Server," & _
    "3=Desktop," & _
    "4=Low-Profile Desktop," & _
    "5=Pizza-Box," & _
    "6=Mini Tower," & _
    "7=Tower," & _
    "8=Portable," & _
    "9=Laptop," & _
    "10=Notebook," & _
    "11=Hand-Held," & _
    "12=Mobile Device in Docking Station," & _
    "13=All-in-One," & _
    "14=Sub-Notebook," & _
    "15=Space Saving Chassis," & _
    "16=Ultra Small Form Factor," & _
    "17=Server Tower Chassis," & _
    "18=Mobile Device in Docking Station," & _
    "19=Sub-Chassis," & _
    "20=Bus-Expansion chassis," & _
    "21=Peripheral Chassis," & _
    "22=Storage Chassis," & _
    "23=Rack-Mounted Chassis," & _
    "24=Sealed-Case PC"
  if IsNumeric(MatchValue) Then 
    for each y in Split(x,",")
      if split(y,"=")(0) = MatchValue then
        result = split(y,"=")(1)
        exit for
      end if
    next
  else
    for each y in Split(x,",")
      if split(y,"=")(1) = MatchValue then
        result = split(y,"=")(0)
        exit for
      end if
    next
  end if 
  CMWT_WMI_ChassisType = result
end function

Now, some interesting thoughts and comments on this tragedy, if I may:

  1. The use of a delimited string as the logical array/list/hash store is obviously a point that could be debated well beyond the point at which our spouses had already left the party and we didn’t even notice.  However, with smaller structures (less than 1 MB of data) the processing overhead on even an older Core2 Duo is “immeasurably small”.  How small?  As in, smaller than the chance your vote will affect any future presidential election outcome.  So, a Dictionary, or Array (in VB parlance) would suffice equally well, but I’m too lazy right now (been out shopping with my wife).
  2. The type-agnostic handling and return value is nice for avoiding an overload scenario (common to C/C++/Java and C# realms).  LISP was (is?) even more dynamic and elegant in that the function can self-define itself infinitely or with self-binding constraints (say that ten times).
  3. I’m sure the above disaster could be done with PowerShell (or Perl, Python, Ruby, JSON, two sticks and mud, a bowl of cornflakes, etc.)  But the point is not the language.  I just chose this as one example.  The point is we often make two (or more) functions which could be refactored more efficiently.
  4. No animals were harmed in the course of making this.  However, I did smooch my little dog to the point where she’s hiding right now.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s