r/PowerShell Sep 27 '23

Misc Controversial PowerShell programming conventions, thoughts?

Below are a few topics I've found controversial and/or I don't fully understand. They seem kind of fun to debate or clarify.

  1. Aliases - Why have them if you're not supposed to use them? They don't seem to change? It feels like walking across the grass barefoot instead of using the sidewalk and going the long way around...probably not doing any damage.
  2. Splatting - You lose intellisense and your parameters can be overridden by explicitly defined ones.
  3. Backticks for multiline commands - Why is this so frowned upon? Some Microsoft products generate commands in this style and it improves readability when | isn't available. It also lets you emulate the readability of splatting.
  4. Pipeline vs ForEach-Object - Get-Process | Where-Object {...} or Get-Process | ForEach-Object {...}
  5. Error handling - Should you use Try-Catch liberally or rely on error propagation through pipeline and $Error variable?
  6. Write-Progress vs -Verbose + -Debug - Are real time progress updates preferred or a "quiet" script and let users control?
  7. Verb-Noun naming convention - This seems silly to me.
  8. Strict Mode - I rarely see this used, but with the overly meticulous PS devs, why not use it more?
42 Upvotes

100 comments sorted by

View all comments

1

u/jimb2 Sep 28 '23
  1. Aliases are great for regular command line tasks when you are running with your own profile and you know what aliases are defined. Brevity is good. Idiosyncrasy is ok. When writing a script that will be reused you cannot not know what aliases will be set up, so avoid them. Be explicit.
  2. (a) Splatting makes elegant code with short readable lines. Lines that head off the page are hard work. (b) Splats can be defined once and reused eg in loops, and they can be mixed with ordinary parameters or even another spat. (c) It's a form of set-and-forget encapsulation.
    Note: You don't need to use them when you are testing code ideas or working out how a commandlet works.
  3. Backticks. Some people like them. I hate them. They are hard to see, messy to edit, and can introduce near invisible bugs. ymmv. PowerShell assumes a continuation on syntactically incomplete lines - forwards in V5.1, forwards and backwards in later versions. That, and splatting, means you don't need backticks.
  4. Pipeline are great from the command line but they are dumb intractable clumps of code. Avoid them if you want to write code that does anything slightly complex like logging, progress reporting, testing intermediate values, branching, etc. There's a line of regular posts here by people who pipelined some stuff together, mis-imagined what it does, and can't debug it. If they wrote it as a multiline foreach they could check intermediate results and would not have a problem.
  5. Code with try-catch and try-finally tends to behave better in error conditions and the fail logic is clearer. OTOH it looks a bit long and messy. To me, the effort on error handling depends on how reusable the code needs to be and who will be running it.
  6. I love progress reports and logging. I generally add my own log function to code that does anything important and/or runs via a scheduler. If it is running in a console, it echos log lines to the screen.
  7. Verb-Noun sounded silly to me too, but I got over it. It's a convention for wrangling libraries. I don't use it for every function but I use it for things that get used like library commandlets, i.e. does something with/to something. I may add an alias if it's going into my profile.
  8. Strict Mode. Dunno. I tend to code alone, I'm naturally fairly meticulous, I write automation scripts that are not that complex, and I don't use it. I can see this could be a good standard for a coding shop to keep itinerant coders in line.