include::../rationale.adoc[]

== How to fix it in JSON templates

The fix for this issue is straightforward.
Once you ensure the unused variable is not part of an incomplete implementation leading to bugs, you just need to remove it.

=== Code examples

==== Noncompliant code example

[source,json,diff-id=1,diff-type=noncompliant]
----
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "variables": {
      "unusedVariable": "unusedValue",
      "virtualMachinesName": "[uniqueString(resourceGroup().id)]"
  },
  "resources": [
    {
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[variables('virtualMachinesName')]",
      "apiVersion": "2023-09-01",
      "location": "[resourceGroup().location]"
    }
  ]
}
----

==== Compliant solution

[source,json,diff-id=1,diff-type=compliant]
----
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "variables": {
      "virtualMachinesName": "[uniqueString(resourceGroup().id)]"
  },
  "resources": [
    {
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[variables('virtualMachinesName')]",
      "apiVersion": "2023-09-01",
      "location": "[resourceGroup().location]"
    }
  ]
}
----

== How to fix it in Bicep

The fix for this issue is straightforward.
Once you ensure the unused variable is not part of an incomplete implementation leading to bugs, you just need to remove it.

=== Code examples

==== Noncompliant code example

[source,bicep,diff-id=2,diff-type=noncompliant]
----
var unusedVariable = 'unusedValue' // Noncompliant
var virtualMachinesName = '${uniqueString(resourceGroup().id)}'

resource vm 'Microsoft.Compute/virtualMachines@2023-09-01' = {
  name: virtualMachinesName
  location: resourceGroup().location
}
----

==== Compliant solution

[source,bicep,diff-id=2,diff-type=compliant]
----
var virtualMachinesName = '${uniqueString(resourceGroup().id)}'

resource vm 'Microsoft.Compute/virtualMachines@2023-09-01' = {
  name: virtualMachinesName
  location: resourceGroup().location
}
----



== Resources

=== Documentation

* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/best-practices#variables[ARM template best practices]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/best-practices#names[Best practices for Bicep]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/variables[Variables in ARM templates]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/variables[Variables in Bicep]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
