Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use colons for all arguments of a function/script call?

I recently started using PowerShell, and noticed that I could pass argument values using a space between the argument name and value, or using a colon, like this:

MyFunction -Parameter value
or
MyFunction -Parameter:value

I started using the colon because it differentiates the line a bit more (for readability reasons), but from what I've seen, most people do not use it.

I've read a bit also about the differences between these approaches when working with switch typed arguments, that normally do not need values. In that situation, one needs to use the colon, or else the command will not work. This is another reason why I'm leaning towards using the colon for every parameter, for consistency reasons.

Is there something else I should keep in mind?

like image 752
julealgon Avatar asked Oct 31 '14 12:10

julealgon


1 Answers

Generally speaking, when I need to execute a function with a switch parameter set to false, I simply omit the switch. That's the design intent of a switch parameter. The only time I think I would ever use the colon in a parameter is when I need to programmatically determine the value of a switch.

For example, let's say I need to get a regular directory listing on even days, and a recursive directory listing on odd days:

Get-ChildItem -Path $Path -Recurse:$((Get-Date).Day % 2 -eq 1) | ForEach-Object {...}

Beyond that, I personally wouldn't bother with the colon unless it significantly added to the readability of a given statement. It's not a commonly used syntax, so people who read your code later are more likely to be confused by it.

like image 110
Bacon Bits Avatar answered Dec 09 '22 16:12

Bacon Bits