I've taken over a project and am dealing with some pretty old PowerShell scripts that were created five to seven years ago. The previous developer didn't set up parameters; they relied solely on whatever arguments were passed in. This means the scripts assume arguments are in the right order and the correct number, or they have some basic validation checks. I'm curious if there's a reason why it's done this way. Is it a matter of personal preference, or is using arguments without defining parameters more efficient or elegant? It seems like it complicates things unnecessarily.
3 Answers
Definitely use parameters! They make your code much more consistent and powerful. With parameters, you get features like validation and automatic ordering, which help avoid bugs and confusion.
I think relying on unnamed arguments is pretty risky. It seems like a lazy way to code. It's a good idea to include validation and upgrade your functions to use 'advanced functions' if you're on PowerShell 2.0 or later. Your predecessor was likely stuck in an outdated way of working.
It’s not just a matter of skill; omitting parameters is really unnecessary if your script requires specific inputs. The only time you might do this is if you're wrapping another tool. Explicitly specifying parameters makes things clearer and more manageable.
I agree! I’m also in the process of making my functions more parameterized and reusable. It definitely makes them easier to understand!