-
Notifications
You must be signed in to change notification settings - Fork 4
Optimization
VectorBCO edited this page Jun 30, 2021
·
3 revisions
- Variables should be defined in a place where they will be used (about_scopes)
- Functions used in one script should be placed at the beginning (just after param() block)
- 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.
- Script and Function input parameters should be placed in Param() block, where they should be declared and validated.
- 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)
- Mandatory parameters quantity should be minimized (no one will use script which will request 100 parameters)
- It`s ok, read finished script once again and fix (optimize) it "for future"
- For final script\function good to use
$ErrorActionPreference = "stop"withTry{}Catch{}Finally{} - Always task can be done in many ways. Maybe your solution is best of the best, but good to have that in mind :)
- Use filters from left side
- If cmdlet accept array as an input - use this possibility ;)
- If loops is not necessary - don`t put them just for fun. Loops are slow.
- If you need to chose between hash tables and object array - use last one
- Code duplicates should be minimized - remove them or move functionality to the functions
- Less not needed actions - better performance (your cap)