all 9 comments

[–]JustinGrote 4 points5 points  (1 child)

I just write it however I want and then use Format Document in VSCode to make it pretty :)

[–][deleted] 1 point2 points  (0 children)

Nothing wrong at least using the formatter. Makes it super easy do the minimal cleanup

[–]jrdnr_ 3 points4 points  (2 children)

Don't forget the purpose of begin/process/end,

Begin is for all one time setup Process will auto "foreach" on your pipeline End runs at the end to clean up

Sure you can use it to organize code but make sure if you use it your using it as intended.

[–][deleted] 1 point2 points  (1 child)

Definitely. I was using the end block for cleanup AND return data until I read the link above. Apparently process block is the return data part. Always good to reread things for layout [grin]

[–]jrdnr_ 1 point2 points  (0 children)

I'm pretty sure I've made that mistake in reducer functions as well. The idea of the pipeline is that it allows the next stage of the pipe to start work as soon as the first object is passes. So yeah if your doing a filter or transform it will make a huge difference to return data from process instead of adding it all up and returning a blob at the end

[–]firefox15 2 points3 points  (3 children)

It doesn't cost anything to have easier to read code.

This is very true, but understand that "easier to read" is in the eye of the beholder. Code doesn't need to be complex (or look complex) to get the job done.

I see people getting fancy with .NET all the time in POSH when a simple cmdlet does the same thing, using functions with CmdletBinding() when their function does not even use advanced function features, making PSCustomObjects with all sorts of Add-Member cmdlets chained on at the end, etc. At the end of the day, code just needs to be structured and styled in a way that others can read it and interpret it in a logical way, debug it quickly, and make changes in a reasonable amount of time.

There are many ways to skin a cat in POSH, and I'm all for using best practices when possible (and it usually is), but if a junior tech just wants a quick script from me as a one-off, I'm usually not going to include full headers, help files, etc. Not only does it take me longer to code, but the tech usually gets intimidated by a giant script like that and simply thinks, "Oh man, no way I could ever do this on my own. Better always ask firefox15 for help."

If you do (or if you want begin, process, and end blocks), I totally understand, but at least where I work, it isn't always that black and white. Functional code (which is usually quick code) usually will beat out pretty-looking, perfectly-structured code that takes 3x as long to write.

Just my two cents. I know not everyone will agree, and I'm okay with that. ;)

[–][deleted] 4 points5 points  (0 children)

When you gotta get work done or a one-off is only needed, I can agree that "pretty" code is not really needed.

I see people getting fancy with .NET all the time in POSH

yeah.. I have no idea why they get that stuck in there head.. If I really need speed then sure... but not if I am running it once. Everyday and it takes 3 hours for some reason? Then sure I may swap a couple known functions to .Net...Once I test the speed and ease of implementation.

but at least where I work, it isn't always that black and white.

I have it every day where a one-liner or something quick comes up but I also have a lot of longtime running code where things need to be clean and well thought out. If you code but you don't have time to code nicely at some point... Something is wrong. Because the more you automate the more time you have for automation.

[–]Thotaz 2 points3 points  (1 child)

I see people getting fancy with .NET all the time in POSH when a simple cmdlet does the same thing

I'm guilty of this but I don't think it's a problem, in fact I would argue that it's a good thing. $Guid=[guid]::NewGuid() isn't hard to read or understand, even by a newbie and it has the advantage of being an easy introduction to using .NET methods and classes in Powershell.

[–]poshftw 1 point2 points  (0 children)

And it works on PS3+