Consider
$a = 100 + (&{ if($bonus) {10} else {0} })
Am I coding that in a normal way? Or is there a better PowerShell style to
express that logic as one line?
- Larry
Consider
$a = 100 + (&{ if($bonus) {10} else {0} })
Am I coding that in a normal way? Or is there a better PowerShell style to
express that logic as one line?
- Larry
Larry
Do not fall to the dark side. WAY WAY WAY too much importances is made about
Powershell's ability to succinctly write a series of actions in one line.
The Powershell team's moto certainly does help
"automating the world one-liner at a time"
While this is a huge advantage when you at the command line, its far more
important to write so that your intentions are clear; particularlly if this
will be put into a script you'll be reading in 3 months time.
Here is what I'd do
if ($Exceeded_Goals) {$bonus = 10} else {$bonus = 0}
$paycheck = 100 + $bonus
Granted is more typing and 2 lines but now the code is self documenting.
"Larry__Weiss" wrote:
> Consider
>
> $a = 100 + (&{ if($bonus) {10} else {0} })
>
> Am I coding that in a normal way? Or is there a better PowerShell style to
> express that logic as one line?
>
> - Larry
>
>
<grin> It may too late for that!
Bob, what if they forced you to do it as a one-liner (like when Spock had to
"make the best guess I can" in that one movie...). What is the "best" one-liner
that fits the logic I abstracted earlier?
Is it in fact
$a = $b + (&{ if($x) {$y} else {$z} })
Or can it be done "better" ?
- Larry "Skywalker" (Luke's evil twin)
Bob Landau wrote:
> Larry
> Do not fall to the dark side....
If the one-liner must be self-contained then the method you've described is
as good as I can think of.
"Larry__Weiss" wrote:
> <grin> It may too late for that!
>
> Bob, what if they forced you to do it as a one-liner (like when Spock had to
> "make the best guess I can" in that one movie...). What is the "best" one-liner
> that fits the logic I abstracted earlier?
>
> Is it in fact
> $a = $b + (&{ if($x) {$y} else {$z} })
>
> Or can it be done "better" ?
>
> - Larry "Skywalker" (Luke's evil twin)
>
>
> Bob Landau wrote:>
> > Larry
> > Do not fall to the dark side....
Thanks! You know, when you first use a language new to you, there's no telling
what kind of strange construction might happen during that period when you don't
have some "usual" patterns ingrained.
And I do take your earlier comments about readability seriously.
On that score, I used to be one who wanted my code to exhibit its patterns as
seen from even across the room from the printout or screen. But fairly recently
I've wanted more so that the current windowed portion of code show me more of
the source than possible with maximal layout.
I used to hate OTBS because the braces didn't line up. But now I don't care as
much because OTBS lets me get more code into that window.
OTBS = One True Brace Style
http://c2.com/cgi-bin/wiki?OneTrueBraceStyle
http://en.wikipedia.org/wiki/Otbs#K.26R_style
- Larry
Bob Landau wrote:
> If the one-liner must be self-contained then the method you've described is
> as good as I can think of.
>
> "Larry__Weiss" wrote:
>> <grin> It may too late for that!
>> Bob, what if they forced you to do it as a one-liner (like when Spock had to
>> "make the best guess I can" in that one movie...). What is the "best" one-liner
>> that fits the logic I abstracted earlier?
>> Is it in fact
>> $a = $b + (&{ if($x) {$y} else {$z} })
>> Or can it be done "better" ?
>> - Larry "Skywalker" (Luke's evil twin)
>>
>> Bob Landau wrote:
>>> Larry
>>> Do not fall to the dark side....
I can't wait to get Payette's book so I can figure out the difference in what
these four statements mean to PowerShell
$a = $b + (&{ if($x) {$y} else {$z} }) # works as expected
$a = $b + &{ if($x) {$y} else {$z} } # errors occur
$a = &{ if($x) {$y} else {$z} } + $b # no errors, but + $b doesn't
$a = (&{ if($x) {$y} else {$z} }) + $b # works as expected
- Larry
Bob Landau wrote:
> If the one-liner must be self-contained then the method you've described is
> as good as I can think of.
>
> "Larry__Weiss" wrote:
>> $a = $b + (&{ if($x) {$y} else {$z} })
>>
Thanks for the alternative. Contrast
$a = $b + $(if($x) {$y} else {$z})
$a = $b + (&{if($x) {$y} else {$z}})
Clearly to me your style is much better.
- Larry
Joel Bennett wrote:
> I think it's better to use $() instead of (&{})
> $a = $b + $(if($x) {$y} else {$z})
>
| Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help understanding one liner | AlexT. | VB Script | 3 | 27 Aug 2009 |
| Exchange Powershell one-liner to Mail-Enable Contacts | RobDG | PowerShell | 4 | 23 Sep 2008 |
| Multi-line text becomes a one-liner | MichielV | PowerShell | 1 | 28 Jun 2007 |
| PowerShell Leaders Join Forces and offer a pre-release version of PowerShell for 50% off the retail value | klumsy@xtra.co.nz | PowerShell | 0 | 19 Jun 2007 |
| Exchange 2007 "Activate the PowerShell" One liner Contest | Abhishek Agrawal [MSFT] | PowerShell | 0 | 17 Jun 2006 |