Giter Club home page Giter Club logo

Comments (5)

ChrisLGardner avatar ChrisLGardner commented on June 11, 2024 1

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.

ChrisLGardner avatar ChrisLGardner commented on June 11, 2024

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.

serjou1 avatar serjou1 commented on June 11, 2024

That script is just for example. I just wonder to know the best approach in such cases

from powershellpracticeandstyle.

ryan-jan avatar ryan-jan commented on June 11, 2024

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.

Jaykul avatar Jaykul commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.