Ensuring Backward Compatibility

by Feb 12, 2014

Let's assume you have created this function:

function Test-Function
{
  param
  (
    [Parameter(Mandatory=$true)]
    $ServerPath 
  )

  "You selected $ServerPath"
} 

It works fine, but half a year later in a code review, your boss wants you to use standard parameter names, and rename "ServerPath" to "ComputerName". So you change your function appropriately:

function Test-Function
{
  param
  (
    [Parameter(Mandatory=$true)]
    $ComputerName 
  )

  "You selected $ComputerName"
} 

What you cannot easily control, though, is who else calls your function, and may still use the old parameter. So to ensure backward compatibility, make sure your function can still work with the old parameter name, too:

function Test-Function
{
  param
  (
    [Parameter(Mandatory=$true)]
    [Alias("ServerPath")]
    $ComputerName 
  )

  "You selected $ComputerName"
} 

Old code can now still run, and new code (and code completion) will use the new name:

Twitter This Tip! ReTweet this Tip!