Hikapasya05 / CodeRefactoring1

0 stars 0 forks source link

Duplicate Code / Unit Testing #1

Open Hikapasya05 opened 1 year ago

Hikapasya05 commented 1 year ago

Example 1

val megacharizardHP = binding.megacharizardHP
        megacharizardHP.apply {
            setProgressWithAnimation(100f, 2000)
            progressMax = 100f
            progressBarColor = Color.GREEN
            progressBarColorEnd = Color.GREEN
            progressBarColorDirection = CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
            backgroundProgressBarColor = Color.GRAY
        }
val mewtwoHPmegacharizard = binding.mewtwoHPmegacharizard
        mewtwoHPmegacharizard.apply {
            setProgressWithAnimation(100f, 2000)
            progressMax = 100f
            progressBarColor = Color.GREEN
            progressBarColorEnd = Color.GREEN
            progressBarColorDirection = CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
            backgroundProgressBarColor = Color.GRAY
        }

This codes functions to show the health bar of each pokemon subjected to it, by implementing a progress bar for each pokemon.

Duplicate code is considered bad for several reasons. Firstly, it violates the DRY (Don't Repeat Yourself) principle, which emphasizes the importance of code reuse and maintaining a single source of truth.

Example 2

 binding.firespinBtn.setOnClickListener {
            Handler(Looper.getMainLooper()).postDelayed({
                if (progressMewtwo >= 25) {
                    progressMegacharizard -= 20f
                    binding.megacharizardHPValue.text = (progressMegacharizard.toInt()).toString()
                    battle(megacharizardHP, progressMegacharizard)
                    Toast.makeText(
                        this@megacharizard_battle,
                        "Mewtwo used Hyper beam.",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }, 2000)

            progressMewtwo -= 25f
            battle(mewtwoHPmegacharizard, progressMewtwo)
            binding.mewtwoHPValuemegacharizard.text = (progressMewtwo.toInt()).toString()
        }
binding.blastburnBtn.setOnClickListener {
            Handler(Looper.getMainLooper()).postDelayed({
                if (progressMewtwo >= 25) {
                    progressMegacharizard -= 20f
                    binding.megacharizardHPValue.text = (progressMegacharizard.toInt()).toString()
                    battle(megacharizardHP, progressMegacharizard)

                    Toast.makeText(
                        this@megacharizard_battle,
                        "Mewtwo used Psycho cut.",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }, 2000)

            progressMewtwo -= 25f
            battle(mewtwoHPmegacharizard, progressMewtwo)
            binding.mewtwoHPValuemegacharizard.text = (progressMewtwo.toInt()).toString()
        }

This code is to activate the attack button on a pokemon which then when clicked it will decrease the value of the progress bar or health of the opposite pokemon.

Having duplicate code can lead to various issues. It makes the codebase harder to maintain since any changes or bug fixes need to be applied to multiple places, increasing the chances of inconsistencies.

Example 3

This code is for the transition of a battle for when the opposite pokemon's progress bar or health has reached 0 , it will then continue to another activity.

Duplicated code can lead to a proliferation of bugs, as fixing an issue in one instance may be overlooked in others. It can also hinder code evolution and scalability.

Example 4

</androidx.constraintlayout.widget.ConstraintLayout>



These XML codes are for showing the pages of the first and second scenes of the story. All the elements cased on both XML are the same but differed by the contents. Instead of making these separate layouts, I could have made it into one activity.

This is bad because if this was a team project it could make the other developers confused. Also, it costs more resource to divide these XML into two layouts.
LidiaIvanova commented 1 year ago

2

Hikapasya05 commented 1 year ago

Unit Testing

Code Flaw

this unit testing i'm doing is from example 2. the code there works to make a sequence of battle between mega charizard and mewtwo, where mega charizard attacks mewtwo for 25 damage and mewtwo attacks back after that dealing 20 damage. But in a case where mewtwo's health is >=25, it doesnt get to attack charizard back because it fainted.

Description

 fun megacharizardTurnAttackMewtwo(
        mewtwoHealth : Int,
        megacharizardDamage : Int,
        mewtwoState : Boolean
    ): Boolean {
        return if (mewtwoHealth <= megacharizardDamage && !mewtwoState) {
            true
        } else if (mewtwoHealth > megacharizardDamage && mewtwoState) {
            true
        } else {
            false
        }
    }

this test unit code is an extracted method from example 2 where it takes 3 parameters, the health of mewtwo, the damage from mega charizard, and whether mewtwo is still alive or not (mewtwo state). if mewtwo's health is lower than mega charizard's damage, mewtwo state should be false. and if mewtwo's health is above charizard's health, mewtwo's state should be true.

Test 1

@Test
    fun megacharizardTurn_mewtwoEqualDamageAlive_returnFalse() {
        val result = Lab2Unit.megacharizardTurnAttackMewtwo(
                25,
                25,
                true
                )
        assertFalse(result)
    }

so in this test, the turn starts with mewtwo's health being equal to charizard's damage, thus after testing the code it will return false because mewtwo will not be alive after taking the amount of damage with that health remaining.

Test 2

@Test
    fun megacharizardTurn_mewtwoEqualDamageAlive_returnTrue() {
        val result = Lab2Unit.megacharizardTurnAttackMewtwo(
            25,
            25,
            false
        )
        assertTrue(result)
    }

so in this test, the turn starts with mewtwo's health being equal to charizard's damage, thus after testing the code it will return true because mewtwo will not be alive after taking the amount of damage with that health remaining.

Test 3

@Test
    fun megacharizardTurn_mewtwoAboveDamageDead_returnFalse() {
        val result = Lab2Unit.megacharizardTurnAttackMewtwo(
            50,
            25,
            false
        )
    assertFalse(result)
    }

so in this test, the turn starts with mewtwo's health being more than charizard's damage, thus after testing the code it will return false because mewtwo will still be alive after taking the amount of damage with that health remaining.

Test 4

@Test
    fun megacharizardTurn_mewtwoAboveDamageAlive_returnTrue() {
        val result = Lab2Unit.megacharizardTurnAttackMewtwo(
            50,
            25,
            true
        )
        assertTrue(result)
    }

so in this test, the turn starts with mewtwo's health being more than charizard's damage, thus after testing the code it will return true because mewtwo will still be alive after taking the amount of damage with that health remaining.