---
title: No Duplicate Variable Names In Scope
impact: HIGH
impactDescription: prevents shadowing bugs and improves code clarity
tags: variables, shadowing, scope, quality, kotlin
---

## No Duplicate Variable Names In Scope

Variable shadowing occurs when a variable declared within a certain scope has the same name as a variable declared in an outer scope. This leads to subtle bugs and confusion about which variable is being accessed.

**Incorrect (shadowed variables):**

```kotlin
val user = getCurrentUser()

fun processOrder(order: Order) {
    val user = order.user // Shadows outer 'user'!
    
    // Which user is this?
    println(user.name)
}

// Shadowing in lambdas
items.forEach { item ->
    val item = transform(item) // Shadows lambda parameter!
}
```

**Correct (unique names):**

```kotlin
val currentUser = getCurrentUser()

fun processOrder(order: Order) {
    val orderUser = order.user // Distinct name
    
    println(orderUser.name)
}

// Different names in nested scope
items.forEach { item ->
    val transformedItem = transform(item)
}
```

**Tools:** detekt (VariableShadowing), Android Studio Linter, Manual Review
