Comments (5)
Whatever looks right to you is probably the best answer, it's probably a heavily opinionated area like a few others around style. Personally I prefer the second so it's more clear it's part of that single command.
from powershellpracticeandstyle.
Remove the Measure-Object entirely since it's not adding anything that isn't already provided by PS.
You could conceivably also drop the parents and name the variable books, then in the if do $books.count, because presumably you actually care what the books are later in your script so should only look them up once and reuse that variable and it's contents later.
from powershellpracticeandstyle.
That script is just for example. I just wonder to know the best approach in such cases
from powershellpracticeandstyle.
Taking into account that the code is obviously for demo purposes only. I would suggest splatting the parameters for Get-ChildItem
for example
$Params = @{
Path = $MyBooksDir
Recurse = $true
File = $true
Include = '*.txt', '*.pdf', '*.doc*'
}
$NumberOfBooks = (Get-ChildItem @Params | Measure-Object).Count
if ($NumberOfBooks -gt 1000) {
Write-Host "There are $NumberOfBooks books in the folder. Are you sure you can read that much?"
}
Also I would use PascalCase for variable names to align with the PowerShellPracticeAndStyle Capitalization guidelines but this is obviously personal preference.
from powershellpracticeandstyle.
For what it's worth (and to demonstrate that this is very much a matter of taste), I would probably just leave it that long. I've got 24" monitors, and my "you should wrap soon" line is set at 120, but ....
But if I did want to shorten that, I would go ahead and wrap on the parenthesis to give myself more space, and then maybe write it like this:
$numberOfBooks = (
Get-ChildItem $myBooksDir -Recurse -File -Include '*.txt', '*.pdf', '*.doc*' |
Measure-Object
).Count
if ($numberOfBooks -gt 1000) {
Write-Host "There are $numberOfBooks books in the folder. Are you sure you can read that much?"
}
from powershellpracticeandstyle.
Related Issues (20)
- Question: Best Practice fot HOT 3
- Indentation advice is bad/wrong HOT 9
- Constant variables placement with in a script. HOT 1
- Is it better to use $PSItem vs $error[0] or $_ HOT 2
- Add Set-Variable vs. $foo = "bar" best practice HOT 2
- Unquoted strings HOT 3
- Indentation on Comment-Based help HOT 5
- Consider revising PERF-02 HOT 1
- Naming-Conventions refers to a broken link
- Guidance on External Module Dependencies HOT 3
- Suggestion - explain why the second command in PERF-01 is faster
- Capitalization of function parameter type HOT 2
- Broken link and sentence fragment on naming conventions page
- Module structure HOT 4
- Avoid Using Semicolons (`;`). HOT 3
- Parameter Comments and Get-Help HOT 2
- Begin/Process/End or Process only. No Process/End, Begin/End, Begin/Process. HOT 5
- Indentation for lines after a pipe character HOT 3
- How to deal with a module that contains multiple interdependent classes? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershellpracticeandstyle.