Hikapasya05 / CodeRefactoring1

0 stars 0 forks source link

Long Method #3

Open Hikapasya05 opened 1 year ago

Hikapasya05 commented 1 year ago

Example 1

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMegacharizardBattleBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val megacharizardHP = binding.megacharizardHP
        megacharizardHP.apply {
            ...
        }
        val mewtwoHPmegacharizard = binding.mewtwoHPmegacharizard
        mewtwoHPmegacharizard.apply {
            ...
        }

        var progressMewtwo = 100f
        var progressMegacharizard = 100f
        var currentvalue: Int = binding.mewtwoHPValuemegacharizard.text.toString().toInt()
        binding.firespinBtn.setOnClickListener {
            Handler(Looper.getMainLooper()).postDelayed({
                ...
            }, 2000)

            progressMewtwo -= 25f
            battle(mewtwoHPmegacharizard, progressMewtwo)
            binding.mewtwoHPValuemegacharizard.text = (progressMewtwo.toInt()).toString()
        }
        binding.blastburnBtn.setOnClickListener {
            Handler(Looper.getMainLooper()).postDelayed({
                ...
            }, 2000)

            progressMewtwo -= 25f
            battle(mewtwoHPmegacharizard, progressMewtwo)
            binding.mewtwoHPValuemegacharizard.text = (progressMewtwo.toInt()).toString()
        }
        binding.flareblitzBtn.setOnClickListener {
            Handler(Looper.getMainLooper()).postDelayed({
                ...
            }, 2000)

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

This whole onCreate is for the mechanism of the Pokemon battle. It features multiple duplicated codes that could be extracted to a new class or method.

They violate the Single Responsibility Principle (SRP), which states that a method or function should have a single, well-defined responsibility.

LidiaIvanova commented 1 year ago

0,5