riswux / VisualNovel-1

0 stars 0 forks source link

Long Method #2

Open riswux opened 1 year ago

riswux commented 1 year ago
Example 1

- Story.kt ```kotlin fun inputPoint() { binding.gameTextView.setText("Hello! My name is Jack. And you?") binding.inputName.setVisibility(View.VISIBLE) binding.choiceButton1.setText("") binding.choiceButton2.setText("") binding.choiceButton3.setText("Accept") binding.choiceButton1.setVisibility(View.INVISIBLE) binding.choiceButton2.setVisibility(View.INVISIBLE) nextPosition1 = "" nextPosition2 = "" nextPosition3 = "startingPoint" } } ``` This method is considered long because it contains multiple lines of code performing various tasks. It handles setting text, visibility, and click listeners for multiple views, and updates the values The code block violates the Single Responsibility Principle (SRP). The Single Responsibility Principle states that a class or a method should have only one reason to change. In this case, the inputPoint() method is responsible for setting text, visibility, and click listeners for multiple views, as well as updating the values of nextPosition1, nextPosition2, and nextPosition3. This indicates that the method is handling multiple responsibilities, which makes it harder to understand, modify, and maintain.

Example 2

- Story.kt ```kotlin fun selectedPosition(position: String){ val mainActivity = MainActivity() when(position){ "inputPoint" -> inputPoint() "startingPoint" -> startingPoint() "walking" -> walking() "hiking" -> hiking() "field" -> field() "film" -> film() "likefilm" -> likefilm() "dislikefilm" -> dislikefilm() "halloween" -> halloween() "costom" -> costom() "likecostom" -> likecostom() "dislikecostom" -> dislikecostom() "endPoint" -> endPoint() "goTitleScreen" -> mainActivity.goToTitleScreen() } // Record the current position and user input val inputEditText = binding.inputName.editText val input = inputEditText?.text.toString() records.add(Record(position, input)) } } ``` ### Description of the deficiency Long Method, The selectedPosition method is responsible for handling different positions and executing corresponding logic. It contains a long list of when conditions, resulting in a large and complex method. ### Why is this code bad? What principles does it violate? What can the existence of such code lead to? The selectedPosition method violates SRP by taking on multiple responsibilities. It handles position-based logic, records the current position and user input, and even interacts with MainActivity/ The long method increases code complexity, making it harder to reason about and maintain.

LidiaIvanova commented 1 year ago

1