Tag Archives: DRY Principal

DRY (Don’t repeat yourself) principal

Admit it, we’re all guilty of this at some point. There’s no shame in it, but if you persist in your ways once it has been brought to your attention…

I had a WET (We Enjoy Typing, get your mind out of the gutter!) moment recently. It had to do with getting the header from a csv File. Here’s where a little knowledge can derail real quick. Headers in a csv file are the first line, right? So get the first line of the csv file, do a -split ‘delimeter’ and I’m good to go right? Nothing wrong with the logic, but see, I’ll still be doing an import-csv later on… DRY!

Instead of getting the header ahead, why not get it after I’ve done the import? How? Why thank you for asking… 🙂

#region: csvLocation
$csvLocation =
@"
LocationCode,StraatNaam,PostCode,Stad
1X,Elmstreet 26,1666 XX,NightMare
2X,PeterPan 01,0001 ZT,Neverland
3x,Mulan 5, 1111 TK, Disney
"@ |
ConvertFrom-Csv -Delimiter ','

$header = ($csvLocation | Get-Member | Where-Object {$_.membertype -eq 'noteproperty'}).name
 #endregion

I got that one on FB PowerShell group. It’s rapidly becoming my favorite social media hub for everything PowerShell related.

Here’s another: Lookup CSV row by value

This has helped me many a times in the past.

#region Group as a hashtable by 'LocationCode'
$lookupLocationCode = $csvLocation | Group-Object -AsHashTable -Property 'LocationCode'
#endregion

Now if I’m looking for a CSV row by LocationCode value that’s pretty straightfoward. At least that’s what I thought… DRY!

Here’s the better option:

$csvLocation.Where({$_.LocationCode -eq '1x'})

LocationCode        StraatNaam          PostCode            Stad
------------        ----------          --------            ----
1X                  Elmstreet 26        1666 XX             NightMare

With this option I can even add a lil’ extra say :

$csvLocation.Where({($_.LocationCode -eq '1x') -and ($_.Stad -eq 'NigtMare')  })

#Oops a 'typo' no result, let's retry...
$csvLocation.Where({($_.LocationCode -eq '1x') -and ($_.Stad -eq 'NightMare')  })

#Ah... Better...
LocationCode        StraatNaam          PostCode            Stad
------------        ----------          --------            ----
1X                  Elmstreet 26        1666 XX             NightMare

With group-object as hashtable I need to select the key beforehand, this is way better! Thanks Hey, Scripting guy!

Know your cmdlets!

So this one is kinda embarassing. I had an old function lying around to ping in PowerShell back in the days before Test-Connection. Thing is, I was still using it. Michaja van der Zouwen pointed out to me that there’s a cmdlet for that… “Oh ok, Thanks!” And as luck would have it, at the PowerShell Summit, Don Jones pointed out that if you’re using ping instead of Test-Connection then your’e doing it wrong! I laughed “like a farmer with a toothache (Denglish)” Hehe… English equivalent: A phony laugh. Ok it wasn’t ‘that’ phony… Take away: Don’t reinvent the wheel… That’s enough shaming for one day…

Whatever your present/future endeavour, think DRY! You won’t regret it…

Hope it’s worth something to you,

Ttyl,

Urv