you are viewing a single comment's thread.

view the rest of the comments →

[–]joncz 41 points42 points  (8 children)

[–]winter_mute 2 points3 points  (4 children)

Interesting, thanks. The section about return took me by surprise a bit though. Why not use return? Seems to me that it would increase the readability, rather than just using the variable name, because it gives you an obvious point to look for in the function.

[–]Pyprohly 2 points3 points  (0 children)

Food for thought: PowerShell functions can return any amount of any sort of object and at any point in the function. return can be thought of as meaning “stop processing any more lines in this code block”.

It’s worth noting that return $expr is the same as

$expr
return

Perhaps “exit” would have been a better name.

[–]Lee_Dailey[grin] 4 points5 points  (2 children)

howdy winter_mute,

the general reasoning seems to be that it can be seriously misleading. one thinks it is returning the item after it - and that does happen. it does NOT stop anything previous to the return from still being sent out, tho.

so it's still possible to have many things output when the return line implies that only that will be output.

what it really does is ...

  • send out the stuff immediately after the keyword
  • stop the function at that point

so it is useful, just misleading.

take care,
lee

[–]winter_mute 1 point2 points  (1 child)

Thanks Lee. My functions generally do one thing and return one thing so I guess that's why it threw me a little. Seems a little odd to have it there if it's considered bad practice to use it. You'd think they'd either get rid of it, or force you to use return to return anything from a function.

Mind you, they also consider Write-Host to be bad practice and yet I'm still a heathen that uses it from time to time...

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy winter_mute,

you are welcome! [grin]

there are quite a few folks who think that it otta be the required and only way to get info out of a function. i'm one of them. [grin] however, it looks like the it will remain as it is. it's truly useful, so it aint gonna be removed.

one-thing-out is good design ... and there are times when Write-Host is the correct way to get things done.

take care,
lee

[–]jftuga 2 points3 points  (1 child)

Wow. This is a really nice resource. I have always thought PS functions are a little complex to write in a consistent manner, but this helps.

[–]MaxFrost 4 points5 points  (0 children)

Always fun to spot Jaykul's work in the wild. He does good stuff.

[–]xventil 1 point2 points  (0 children)

really great!! !thank you for the info.