- Story.kt
```kotlin
fun walking() {
binding.gameView.setImageResource(R.drawable.walking)
binding.gameTextView.setText("Maybe go home?")
binding.gameMC.setVisibility(View.INVISIBLE)
binding.inputName.setVisibility(View.INVISIBLE)
binding.choiceButton1.setText("")
binding.choiceButton2.setText("Yes, and watch film")
binding.choiceButton3.setText("Yes, and celebrate the halloween")
binding.choiceButton1.setVisibility(View.INVISIBLE)
nextPosition1 = ""
nextPosition2 = "film"
nextPosition3 = "halloween"
}
```
```kotlin
fun hiking() {
binding.gameView.setImageResource(R.drawable.hiking)
binding.gameMC.setVisibility(View.INVISIBLE)
binding.inputName.setVisibility(View.INVISIBLE)
binding.gameTextView.setText("How cozy... But it’s already getting dark...")
binding.choiceButton1.setText("")
binding.choiceButton2.setText("Go home and watch the film")
binding.choiceButton3.setText("Go home and celebrate Halloween")
binding.choiceButton1.setVisibility(View.INVISIBLE)
nextPosition1 = ""
nextPosition2 = "film"
nextPosition3 = "halloween"
}
```
```kotlin
fun field() {
binding.gameView.setImageResource(R.drawable.field)
binding.gameTextView.setText("You are sad... Let’s go home?")
binding.gameMC.setVisibility(View.INVISIBLE)
binding.inputName.setVisibility(View.INVISIBLE)
binding.choiceButton1.setText("")
binding.choiceButton2.setText("Maybe, let’s watch the film?")
binding.choiceButton3.setText("Yes, let’s celebrate the Halloween")
binding.choiceButton1.setVisibility(View.INVISIBLE)
nextPosition1 = ""
nextPosition2 = "film"
nextPosition3 = "halloween"
}
```
In the above code, the functions` walking()` `, hiking()`, and `field()` perform similar actions such as setting the image resource, updating the game text view, setting visibility of views, and setting the next positions. However, these actions are scattered across multiple functions, resulting in a shotgun surgery smell. If we need to make a change to any of these actions, we would have to modify each function individually, leading to unnecessary code duplication and a higher chance of introducing errors.
Example 1
- Story.kt ```kotlin fun walking() { binding.gameView.setImageResource(R.drawable.walking) binding.gameTextView.setText("Maybe go home?") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("") binding.choiceButton2.setText("Yes, and watch film") binding.choiceButton3.setText("Yes, and celebrate the halloween") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "film" nextPosition3 = "halloween" } ``` ```kotlin fun hiking() { binding.gameView.setImageResource(R.drawable.hiking) binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.gameTextView.setText("How cozy... But it’s already getting dark...") binding.choiceButton1.setText("") binding.choiceButton2.setText("Go home and watch the film") binding.choiceButton3.setText("Go home and celebrate Halloween") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "film" nextPosition3 = "halloween" } ``` ```kotlin fun field() { binding.gameView.setImageResource(R.drawable.field) binding.gameTextView.setText("You are sad... Let’s go home?") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("") binding.choiceButton2.setText("Maybe, let’s watch the film?") binding.choiceButton3.setText("Yes, let’s celebrate the Halloween") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "film" nextPosition3 = "halloween" } ``` In the above code, the functions` walking()` `, hiking()`, and `field()` perform similar actions such as setting the image resource, updating the game text view, setting visibility of views, and setting the next positions. However, these actions are scattered across multiple functions, resulting in a shotgun surgery smell. If we need to make a change to any of these actions, we would have to modify each function individually, leading to unnecessary code duplication and a higher chance of introducing errors.