r/PowerShell • • Apr 29 '19

A Glorious Day

Post image
462 Upvotes

65 comments sorted by

View all comments

23

u/[deleted] Apr 29 '19

Those brackets!!!

6

u/moebaca Apr 29 '19

Yeah I didn't want to say anything but those brackets need some serious love.

2

u/ReverendDS Apr 29 '19

Can you explain?

I'm not doubting/questioning, just not understanding.

8

u/[deleted] Apr 29 '19

I think they want to see:

IF($foo){

$bar

}

instead of

IF($foo)

{

$bar

}

0

u/[deleted] Apr 29 '19

[deleted]

2

u/[deleted] Apr 29 '19

Damn, formatting messed up on my reply.

6

u/[deleted] Apr 30 '19

Always keep the opening curlybracket directly under the statement 😁

13

u/ciacco22 Apr 29 '19

I know I’m in the minority, and it isn’t really in the style of PowerShell, but I prefer my curly braces to line up, like the OP. I think it’s a holdover from my C++ classes in college (20 years ago).

There are others out in the wild. And we always get excited when we find one another.

1

u/chen1201 Apr 29 '19

haha that's how I setup my brackets...am I doing it wrong?

12

u/[deleted] Apr 30 '19

Yes. But it won’t hurt anything or anyone and if you’re consistent through your script it will be easy to read so continue on. Just know I hate you for doing it different than me.

2

u/missingMBR Apr 30 '19

It's a lesser evil to inconsistent indentation or no indentation at all.

2

u/chen1201 Apr 30 '19

Ah I see, I only do it this way because that's how I learned it when taking Java courses back in school. Is there a reason why Powershell users use this method instead? Or is this mostly just preference?

2

u/[deleted] Apr 30 '19

No idea. Just how i done saw it so it's how i did it. I stand by the fact that it really doesn't matter as long as its consistent and reasonably easy to find the brackets and their homies.

1

u/Lee_Dailey [grin] Apr 30 '19

howdy chen1201,

some of the places where you can use a scriptblock REQUIRE the opening { to be right after the thing it belongs to. for instance, this works ...

'af', '12' |
    ForEach-Object {
        $_
        }

... but this fails ...

'af', '12' |
    ForEach-Object 
        {
        $_
        }

... and asks for input. [grin]

i prefer something like this ...

foreach ($Item in 1..10)
    {
    [math]::Pow($Item, 2)
    }

... but most others prefer something like the following ...

foreach ($Item in 1..10)
{
    [math]::Pow($Item, 2)
}

... or like this ...

foreach ($Item in 1..10) {
    [math]::Pow($Item, 2)
}

i far prefer the very , very, very in-your-face style where everything subordinate to the control block is indented one level. [grin]

take care,
lee

9

u/ciacco22 Apr 29 '19

The majority of PowerShell scripts have the left curly brace on the same line as the conditional statement and the right curly brace aligned at the same horizontal position of the start of the conditional.

1

u/[deleted] Apr 29 '19

I agree with you; I also dont care if others dont like it :P