You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documented the critical rule to emit objects to the pipeline immediately instead of collecting them in arrays or ArrayLists. Added guidance against using -Detailed/-Simple output mode switches and updated checklists and golden rules to reflect these best practices for memory efficiency, user experience, and pipeline compatibility.
-**Always prefer parameterized queries** when using T-SQL with dynamic values
406
406
-**Document your choice** when T-SQL is used instead of SMO for non-obvious reasons
407
407
408
+
### PIPELINE OUTPUT - EMIT OBJECTS IMMEDIATELY
409
+
410
+
**CRITICAL RULE**: NEVER collect objects in an ArrayList or array and output them at the end. Output objects to the pipeline immediately as they are created.
411
+
412
+
**Why Immediate Output:**
413
+
-**Memory Efficiency**: Objects are released to the pipeline immediately, not held in memory
414
+
-**User Experience**: Users see results streaming in real-time, not waiting until the end
415
+
-**Pipeline Compatibility**: Enables proper pipeline chaining and early termination (Ctrl+C)
416
+
-**Error Resilience**: Partial results are available even if the command fails partway through
**WRONG - Using += with arrays (even worse performance):**
459
+
460
+
```powershell
461
+
# WRONG - Array concatenation is extremely slow
462
+
$results = @()
463
+
464
+
foreach ($db in $databases) {
465
+
$results += [PSCustomObject]@{
466
+
Name = $db.Name
467
+
}
468
+
}
469
+
470
+
$results
471
+
```
472
+
473
+
**NO -Detailed or -Simple Parameters:**
474
+
475
+
Do NOT create `-Detailed` or `-Simple` switch parameters that change the output object structure. This is an outdated pattern that creates confusion and breaks pipeline expectations.
476
+
477
+
```powershell
478
+
# WRONG - Do not create output mode switches
479
+
param(
480
+
[switch]$Detailed,
481
+
[switch]$Simple
482
+
)
483
+
484
+
if ($Detailed) {
485
+
# Return more properties
486
+
} elseif ($Simple) {
487
+
# Return fewer properties
488
+
}
489
+
```
490
+
491
+
Instead, return a consistent object with all relevant properties. Users can select the properties they want with `Select-Object`.
492
+
493
+
**Proper Pattern for Process Block:**
494
+
495
+
When using `ValueFromPipeline`, output in the `process` block, not `end`:
0 commit comments