> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/refactoring-if-conditions/arcade-game-example.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
