PowerShellPracticeAndStyle icon indicating copy to clipboard operation
PowerShellPracticeAndStyle copied to clipboard

Style Guide for classes

Open tyteen4a03 opened this issue 8 years ago • 2 comments
trafficstars

It would be nice to have PS Style Guides for classes as well.

tyteen4a03 avatar Sep 14 '17 15:09 tyteen4a03

This post from @xainey really helped me understanding classes and develop an own style.

But I always struggle about things like this:

  • Is there a best case scenario when to define classes instead of creating PSCustomObjects and giving them a individual type name?
  • How complex should the class get? - How many different constructors do I need.
  • Should I use a class for combining different bbjects without any methods?

So maybe it's worth thinking about a style guid for the syntax and a best practice for the usage itself?

OCram85 avatar Nov 23 '17 08:11 OCram85

I think the only time where I would consider it a mandatory requirement to have a class is for input validation where you need a specific type of object with a specific set of properties. I would also include a constructor that can take any PsCustomObject provided it has the right properties (and would otherwise throw an error), as well as one that simply takes all necessary properties as input.

As for combining objects, I believe Join-Object is capable of this regardless. If you wanted this join to result in a different class, you could do the join in a constructor.

vexx32 avatar Apr 20 '18 13:04 vexx32