riswux / VisualNovel-1

0 stars 0 forks source link

Primitive Obsession #6

Open riswux opened 1 year ago

riswux commented 1 year ago
Example 1

```kotlin data class Record(val position: String, val input: String) ``` ### Description of the deficiency The code directly uses primitive types (`String`) to represent the position and `input` fields. ### Why is this code bad? What principles does it violate? What can the existence of such code lead to? This code violates the principles of Single Responsibility Principle (SRP) and data encapsulation. It lacks behavior-specific encapsulation for position and input, making it harder to enforce constraints and maintain consistency . Using primitive types reduces code clarity as they do not convey the intent or purpose of the data they represent. This can make the code less self-explanatory and more challenging to understand. This flaw may have appeared due to a lack of domain modeling or considering the specific meaning and behavior of position and input.

Example 2

```kotlin var nextPosition1 = "" var nextPosition2 = "" var nextPosition3 = "" ``` ### Description of the deficiency The code exhibits Primitive Obsession by relying heavily on primitive types like strings to represent various concepts. ### Why is this code bad? What principles does it violate? What can the existence of such code lead to? it violates the Single Responsibility Principle (SRP) by combining the responsibilities of managing the game's story logic and handling the user interface within the same class. This makes the code harder to understand, maintain, and test.

Example 3

```kotlin binding.gameView.setImageResource(R.drawable.walking) binding.gameView.setImageResource(R.drawable.hiking) binding.gameView.setImageResource(R.drawable.field) binding.gameView.setImageResource(R.drawable.film) binding.gameView.setImageResource(R.drawable.costum) binding.gameView.setImageResource(R.drawable.haloween) ``` ### Description of the deficiency These code snippets use primitive string values (`walking`, `hiking`, `field`, `film`, `costum`, `haloween`) to represent images in the application. Each image is identified by a string value and then loaded into the gameView using the corresponding resource identifier. ### Why is this code bad? What principles does it violate? What can the existence of such code lead to? Violation of Single Responsibility Principle (SRP): The code mixes the responsibility of handling UI elements and representing images. Decreased maintainability: As the application grows, it becomes harder to manage and modify the code due to the scattered usage of string values for images. This flaw could appear due to a lack of awareness about best practices or a quick implementation without considering long-term maintainability.

Example 4

```kotlin binding.gameTextView.setText("Hello! My name is Jack. And you?") binding.gameTextView.text = "Great, $username! What are we going to do?" binding.gameTextView.setText("Maybe go home?") binding.gameTextView.setText("How cozy... But it’s already getting dark...") binding.gameTextView.setText("You are sad... Let’s go home?") binding.gameTextView.setText("Do you like this film?") binding.gameTextView.setText("Great! It’s time to sleep...") binding.gameTextView.setText("May be go sleep?") binding.gameTextView.setText("Very beautiful!") binding.gameTextView.setText("I like your costume.") binding.gameTextView.setText("Thank you! Let’s go to sleep.") binding.gameTextView.setText("It’s ok, i’m not taking offence. Let’s go to sleep!") binding.gameTextView.text = "Thank You for playing!" ``` ### Description of the deficiency The code uses primitive string values directly to set the text on gameTextView. ### Why is this code bad? What principles does it violate? What can the existence of such code lead to? This violates the principle of Primitive Obsession, as it relies on low-level, primitive types instead of creating meaningful abstractions. It leads to code duplication, decreased maintainability, and increased chances of errors when modifying or expanding the code. reason of is Lack of awareness, the developer may not be familiar with the principles of clean code and the negative consequences of Primitive Obsession.

LidiaIvanova commented 1 year ago

First and second examples are correct, because you use String to save the position. The 3rd and 4th - it's normal working with text literals in Android. 1,6