Skip to content

Optimization

VectorBCO edited this page Jun 30, 2021 · 3 revisions

Goto topics list

Good practices (General)

  1. Variables should be defined in a place where they will be used (about_scopes)
  2. Functions used in one script should be placed at the beginning (just after param() block)
  3. Functions usen in multiple scripts should be placed in to the modules and loaded where they needed. Copy-Paste for functions and code blocks produce huge problems in the future.
  4. Script and Function input parameters should be placed in Param() block, where they should be declared and validated.
  5. Good to have possibility execute your script\function in non-interactive mode (by default it should be non-interactive) Read-Host may be acceptable just only as backup plan for inputs (parameter not provided or incorrect)
  6. Mandatory parameters quantity should be minimized (no one will use script which will request 100 parameters)
  7. It`s ok, read finished script once again and fix (optimize) it "for future"
  8. For final script\function good to use $ErrorActionPreference = "stop" with Try{}Catch{}Finally{}
  9. Always task can be done in many ways. Maybe your solution is best of the best, but good to have that in mind :)

Good practices (Performance)

  1. Use filters from left side
  2. If cmdlet accept array as an input - use this possibility ;)
  3. If loops is not necessary - don`t put them just for fun. Loops are slow.
  4. If you need to chose between hash tables and object array - use last one
  5. Code duplicates should be minimized - remove them or move functionality to the functions
  6. Less not needed actions - better performance (your cap)

Clone this wiki locally