riswux / VisualNovel-1

0 stars 0 forks source link

Duplicated Code #3

Open riswux opened 1 year ago

riswux commented 1 year ago
Example 1

- Story.kt ```kotlin fun showAllButtons() { binding.choiceButton1.visibility = View.VISIBLE binding.choiceButton2.visibility = View.VISIBLE binding.choiceButton3.visibility = View.VISIBLE } fun hideAllButtons() { binding.choiceButton1.visibility = View.INVISIBLE binding.choiceButton2.visibility = View.INVISIBLE binding.choiceButton3.visibility = View.INVISIBLE } ``` From the code block above, it can be seen that there are two functions for the visibility of a button, but there is duplicating code that should be simplified in` binding.choiceButton1,` `binding.choiceButton2`, `binding.choiceButton3`

Example 2

- Story.kt ```kotlin fun likefilm() { binding.gameView.setImageResource(R.drawable.film) binding.gameTextView.setText("Great! It’s time to sleep...") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("Yes, it’s too late...") binding.choiceButton2.setText("") binding.choiceButton3.setText("") binding.choiceButton1.setVisibility(View.VISIBLE) binding.choiceButton2.setVisibility(View.INVISIBLE) binding.choiceButton3.setVisibility(View.INVISIBLE) nextPosition1 = "endPoint" nextPosition2 = "" nextPosition3 = "" } ``` ```kotlin fun dislikefilm() { binding.gameView.setImageResource(R.drawable.film) binding.gameTextView.setText("May be go sleep?") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("Yes, it’s too late...") binding.choiceButton2.setText("") binding.choiceButton3.setText("") binding.choiceButton1.setVisibility(View.VISIBLE) binding.choiceButton2.setVisibility(View.INVISIBLE) binding.choiceButton3.setVisibility(View.INVISIBLE) nextPosition1 = "endPoint" nextPosition2 = "" nextPosition3 = "" } ``` In the above code, both the likefilm() and dislikefilm() functions contain duplicated code for setting the image resource, updating the game text view, setting visibility of views, and setting the next positions. The only difference between the two functions is the text set in the gameTextView.

Example 3

- 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 hallowen") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "film" nextPosition3 = "halloween" } ``` ```kotlin fun hiking() { binding.gameView.setImageResource(R.drawable.hiking) binding.gameTextView.setText("How cozy... But it’s already getting dark...") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) 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" } ``` In the above code, both the walking() and hiking() functions contain duplicated code for setting the image resource, updating the game text view, setting visibility of views, and setting the next positions. The only difference between the two functions is the text set in the gameTextView.

Example 4

- Story.kt ```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" } ``` ```kotlin fun film() { binding.gameView.setImageResource(R.drawable.film) binding.gameTextView.setText("Do you like this film?") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("") binding.choiceButton2.setText("I like it!") binding.choiceButton3.setText("No...") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "likefilm" nextPosition3 = "dislikefilm" } ``` In the above code, both the field() and film() functions contain duplicated code for setting the image resource, updating the game text view, setting visibility of views, and setting the next positions. The only difference between the two functions is the text set in the gameTextView.

Example 5

- Story.kt ```kotlin fun halloween() { binding.gameView.setImageResource(R.drawable.costum) binding.gameTextView.setText("Very beautiful!") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("") binding.choiceButton2.setText("Yes! Let’s watch the film!") binding.choiceButton3.setText("Yes! Let’s create costume!") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "film" nextPosition3 = "costom" } ``` ```kotlin fun costom() { binding.gameView.setImageResource(R.drawable.haloween) binding.gameTextView.setText("I like your costume.") binding.gameMC.setVisibility(View.INVISIBLE) binding.inputName.setVisibility(View.INVISIBLE) binding.choiceButton1.setText("") binding.choiceButton2.setText("Your costume is beautiful too!") binding.choiceButton3.setText("To tell you the truth, I don’t like your...") binding.choiceButton1.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "likecostom" nextPosition3 = "dislikecostom" } ``` In the above code, both the halloween() and costom() functions contain duplicated code for setting the image resource, updating the game text view, setting visibility of views, and setting the next positions. The only difference between the two functions is the text set in the gameTextView.

LidiaIvanova commented 1 year ago

Why is this code bad? What principles does it violate? What can the existence of such code lead to? 1,5