PowerShell Readability For Others

Intro

I’ve been meaning to start some sort of online blog to jot down snippets, ideas, and solutions to begin giving back to the online community I regularly benefit from. Here is my first short post to begin that journey!

The Great Debate

I work on a team of 6 developers that write automated solutions. We are currently a little divided in our preferred languages with a heavier focus on Python. Unfortunately, I’m the PowerShell guy that has been slowly learning Python to support different projects and our overall code base.

I’ve been having a debate with myself over the past couple weeks. Do I go out of my way to make my PowerShell more readable or stick to the syntax I’ve learned and are more comfortable writing?

Can You Read This Now?

Here is a quick example with a method used very frequently: piping to Where-Object to perform operations based on an object property value.

This is the way I’m used to writing my code:

<span>Get-NetworkAdapter</span><span> </span><span>|</span><span> </span><span>Where-Object</span><span> </span><span>{</span><span>$_</span><span>.</span><span>NetConnectionID</span><span> </span><span>-eq</span><span> </span><span>"NIC1"</span><span>}</span><span> </span>
<span>Get-NetworkAdapter</span><span> </span><span>|</span><span> </span><span>Where-Object</span><span> </span><span>{</span><span>$_</span><span>.</span><span>NetConnectionID</span><span> </span><span>-eq</span><span> </span><span>"NIC1"</span><span>}</span><span> </span>
Get-NetworkAdapter | Where-Object {$_.NetConnectionID -eq "NIC1"}

Enter fullscreen mode Exit fullscreen mode

For anyone familiar with PowerShell, this probably makes perfect sense. However, on my team this presents an initial roadblock concerning readability.

Based on a poll I sent out, they seemed more familiar/comfortable when the same command was presented this way:

<span>Get-NetworkAdapter</span><span> </span><span>|</span><span> </span><span>Where</span><span> </span><span>NetConnectionID</span><span> </span><span>-eq</span><span> </span><span>"NIC1"</span><span> </span>
<span>Get-NetworkAdapter</span><span> </span><span>|</span><span> </span><span>Where</span><span> </span><span>NetConnectionID</span><span> </span><span>-eq</span><span> </span><span>"NIC1"</span><span> </span>
Get-NetworkAdapter | Where NetConnectionID -eq "NIC1"

Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Overall, I think our decision is going to come down to what works best for our team. I’m fine to give a little, as some of the syntax of PowerShell is quite different than Python. However, I will also be challenging them to invest some time learning PowerShell as I learn Python 🙂

Let me know what you think in the comments. I may later turn this into a series about our team adventure to being a multi-language group.

Thanks for reading!

原文链接:PowerShell Readability For Others

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
The best things in life are free.
生活中最美好的事都是免费的
评论 抢沙发

请登录后发表评论

    暂无评论内容