# Arcade Game Example

Here is an example of how each of the refactoring techniques for if conditions could be applied in the context of an arcade game:

1. **Use descriptive variable names:**

```javascript
Copy code// Bad:
if (p === 'A' && q === 2) {
  // Do something
}

// Good:
if (playerMove === 'A' && enemyHealth === 2) {
  // Do something
}
```

**2. Extract complex conditions into separate functions:**

```javascript
Copy code// Bad:
if (player.hasPowerUp('shield') && enemy.isWeakTo('fire') && player.fireballs > 0) {
  // Do something
}

// Good:
function canUseFireAttack(player, enemy) {
  return player.hasPowerUp('shield') && enemy.isWeakTo('fire') && player.fireballs > 0;
}

if (canUseFireAttack(player, enemy)) {
  // Do something
}
```

**3. Use early return statements:**

```javascript
Copy code// Bad:
if (player.health <= 0) {
  console.log('Game over');
} else if (player.hasCompletedLevel()) {
  console.log('Level complete');
} else {
  console.log('Keep playing');
}

// Good:
if (player.health <= 0) {
  console.log('Game over');
  return;
}

if (player.hasCompletedLevel()) {
  console.log('Level complete');
  return;
}

console.log('Keep playing');
```

**4. Use ternary operator:**

```javascript
Copy code// Bad:
let message;
if (player.hasCompletedLevel()) {
  message = 'Level complete';
} else {
  message = 'Keep playing';
}

// Good:
const message = player.hasCompletedLevel() ? 'Level complete' : 'Keep playing';
```

**5. Use object destructuring:**

```javascript
Copy code// Bad:
if (player.health <= 0 && player.lives === 0) {
  console.log('Game over');
// Good:
const { health, lives } = player;
if (health <= 0 && lives === 0) {
console.log('Game over');
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://demirels-organization.gitbook.io/javascript-tutorial/refactoring-if-conditions/arcade-game-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
