Wednesday, March 18, 2009

Final naming convention

This is follow up to my previous post about naming convention in PowerShell and especially need for namespaces.

Oisin shared some details about modules for PowerShell v2 – and these are changes I was looking for.

When you load function from module in Posh V2, you can access it though module. Consider example where my module is called Citrix and contains single function Get-Server.

If there is only single Get-Server function, I can use it directly, however if there are multiple modules with same function name, I must use syntax <Module>\<Verb>-<Noun>.

Our above example is therefore translated to Citrix\Get-Server.

This is naming convention I could use – all I need to do now is to simply change delimiter from “:” to “\” and that’s it – I am ready for Posh V2 ;)

Problem is that I have scripts that acts as containers for functions – similar way how Posh V2 works. Fixing these modules and their function is simple, however it will get much more complicated once I will try to fix different scripts that are using these functions.

For that, I will use following workaround in Posh V1: each function name will be <Module>\<Verb>-<Noun>. So for example my function declaration will looks like this:

Function Global:Citrix\Get-Server {…}

That way once Posh V2 will be released and I will decide that I want to use modules, I only need to change it to Function Get-Server {…} and I will get same user experience as with V1. Simple and not confusing for end users. Functions will have same names in V1 and V2 – and therefore I am prepared for very easy migration to V2 in future.

1 comment:

Henk Hofs said...

I agree with Martin on this one...
I'm currently writing some Microsoft Virtual Server related functions, and it makes absolutely NO SENSE at all for me to use Verb-PrefixNoun (for example Create-MSVSVirtualMachine) but far more sense to use Prefix:Verb-Noun (MSVS:Create-VirtualMachine)...
at this moment i'm using the colon as a separator, but I'll probably switch to the backslash, so I can use PosH v2 modeules in the future :)