Code Smells in AI Generated PowerShell
What is a Code Smell
Code smell is an informal term for a pattern in source code that suggests a deeper design problem, even though the code is not technically wrong and may still work. Code smells are warning signs of poor code quality that can slow development or increase the risk of future bugs, and they’re often treated as a trigger for refactoring.
Common issues with AI generated code
AI generated code can feel like magic when it’s unlocking skills which may lay outside your current reach, or perhaps outside your current sprint deadline, but is important that you maintain ownership of the code that you commit your name to.
Walkthrough
What follows is a pull request containing (assumed) AI generated logic for parsing Azure resource ID segments to enumerate details on a networking resource.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Find resourceType and resourceName pairs
# segments like: subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Network/<type>/<name>/...
for ($i = 0; $i -lt $segments.Count; $i++) {
switch -Regex ($segments[$i]) {
'networkInterfaces|loadBalancers|applicationGateways|virtualNetworkGateways|bastionHosts' {
$type = $segments[$i]
$name = ($i + 1) -lt $segments.Count ? $segments[$i + 1] : ''
# Try to also grab the ip/frontend config name (two more segments)
$cfgName = ''
if (($i + 2) -lt $segments.Count) {
$nextType = $segments[$i + 2]
if ($nextType -in @('ipConfigurations', 'frontendIPConfigurations')) {
$cfgName = ($i + 3) -lt $segments.Count ? $segments[$i + 3] : ''
}
}
return [PSCustomObject]@{
AssociatedResourceType = $type
AssociatedResourceName = $name
AssociatedConfigName = $cfgName
}
}
}
}
What are the smells here?
While syntactcally correct, there are several classic AI-generated smells here.
- Switch -Regex without anchors.
- C-Style index arithmetic.
- Silent empty strings returned on mismatch.
- Ternary operands.
There is an additional smell of assuming that Azure Resource requests all have the same format, but I’ll leave that for you to consider.
Switch -Regex without anchors
There is a joke that once you learn regex you approach every problem with, “OH! I can solve this with regex!!”… and now you have two problems. Let’s take a closer look at the expression:
code
1
2
switch -Regex ($segments[$i]) {
'networkInterfaces|loadBalancers|applicationGateways|virtualNetworkGateways|bastionHosts' {}
this expression takes a string and tries to find one of five substrings (the pipe in this case is a logical or).
why this smells
matching substrings without anchors is overkill and very generous. An anchorless match for ‘bastionHosts’ is the same as -like “*bastionHosts*” which will match anything containing that substring. i.e. fake-bastionHost-v2
a better way
it would be better to couch the expression inside anchors (^(match_string)$), but for me, since we are looking for an exact match against a small array of strings, creating an object list of strings and checking for membership is the right answer.
1
2
3
4
5
6
7
8
9
$resourceTypes @(
'networkInterfaces'
'loadBalancers|applicationGateway'
'virtualNetworkGateways'
'bastionHosts'
)
# now we can test membership with -in
if ($Segment[$i] -in $resourceTypes) {}
C-Style index arithmetic.
Stepping through an array and peeking ahead of the index by parsing $i + n.
code
1
2
3
4
5
6
7
$cfgName = ''
if (($i + 2) -lt $segments.Count)) {
$nextType = $segments[$i + 2]
if ($nextType -in @('ipConfigurations', 'frontendIPConfigurations')) {
$cfgName = ($i + 3) -lt $segments.Count ? $segments[$i + 3] : ''
}
}
why this smells
Peeking ahead from an index is a common LLM pattern when positional parsing is asked for, but is both brittle and harder to read than the more idiomatic named matching. I also noticed that, in this smell, lies the previous solution to the anchorless regex ;)
a better way
same as before, isolate string matching into an object list of strings and match explicitly against that list.
1
2
3
4
5
6
7
8
9
10
$configTypes = @(
'ipConfigurations'
'frontendIPConfigurations'
)
$cfgName = $null
if ($i + 3 -lt $Segments.Count -and $Segments[$i + 2] -in $configTypes) {
$cfgName = $Segments[$i + 3]
}
Ternary operands and Silent empty strings returned on mismatch.
These two are interconnected and can be looked at together.
Added in PowerShell 7, a ternary operator tests a condition and returns true or false, They take the following shape: condition ? valueIfTrue : valueIfFalse.
In this code, when a match in not found in a provided string, an empty string is returned.
code
1
2
$name = ($i + 1) -lt $segments.Count ? $segments[$i + 1] : ''
$cfgName = ($i + 3) -lt $segments.Count ? $segments[$i + 3] : ''
why this smells
Along with do-while loops, ternary operators are a personal peeve of mine. While syntactcally correct, they smell because they are not intuitive for people who don’t have more robust programming experience and require PowerShell 7.
Secondly, missing names and non-matching child segments silently fallback to ‘’ instead of $null or flagging the irregular path shape. This masks bad input, potentially provides bad output, and can in this example, slip past truthiness checks to pollute downstream logic in a way that is not easy to detect.
a better way
Use explicit declarations or else blocks
set a mismatched value to $null, or couch the statements in try/catch blocks to find processing failures.
1
2
3
4
5
6
$name = if ($i + 1) -lt $segments.Count) { $segments[$i + 1] } else { $null }
$cfgName = $null
if ($i + 3 -lt $Segment.Count -and $Segment[$i + 2] -in $configTypes) {
$cfgName = $Segment[$i + 3]
}
Putting it together
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Get-AssociatedResourceInfo {
param([string[]] $Segments)
$resourceTypes = @(
'networkInterfaces'
'loadBalancers'
'applicationGateway'
'virtualNetworkGateways'
'bastionHosts'
)
$configTypes = @(
'ipConfigurations'
'frontendIPConfigurations'
)
for ( $i = 0; $i -lt $Segments.count; $i++ ) {
if ($Segments[$i] -in $resourceTypes) {
$type = $segments[$i]
$name = if (($i + 1) -lt $segments.Count) { $segments[$i + 1] } else { $null }
$cfgName = $null
if ($i + 3 -lt $Segments.Count -and $Segments[$i + 2] -in $configTypes) {
$cfgName = $Segments[$i + 3]
}
return [PSCustomObject]@{
AssociatedResourceType = $type
AssociatedResourceName = $name
AssociatedConfigName = $cfgName
}
}
}
}
Final thoughts
AI tools are great at getting you from a blank screen to working code fast, but “working” is the lowest bar in software engineering. LLMs optimize for plausible output, not maintainability, edge-case resilience, or your team’s specific conventions.
When you open a pull request, your name goes on the commit, not the model’s. Treat all code the same way you would a snippet copied from a forum: read every line, question every pattern, test the failure paths, and refactor it until you could confidently defend each decision in a review, because if you can’t, pushing that request is a waste of everyone’s time.