YoungKru-D / CatAndActivity

0 stars 0 forks source link

Long Method #1

Open YoungKru-D opened 1 year ago

YoungKru-D commented 1 year ago

Example 1

From GamePlay.kt file

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

        val btn1Hide = findViewById<Button>(R.id.btn2Choice)
        val btn2Hide = findViewById<Button>(R.id.btn2Choice2)
        val btn3Hide = findViewById<Button>(R.id.btn2Choice3)
        val btnAcceptHide = findViewById<Button>(R.id.btn2Accept)
        val InputHide = findViewById<TextView>(R.id.textInputName)
        val page = intent.getStringExtra(PageName.page).toString()

        when (page) {
            "2" -> {
                btn1Hide.visibility = View.GONE
                btn2Hide.visibility = View.GONE
                btn3Hide.visibility = View.GONE
                binding.text2Edit.text = getString(R.string.p2_hello)
            }
            "3" -> {
                InputHide.visibility = View.GONE
                btnAcceptHide.visibility = View.GONE
                val name = intent.getStringExtra(PageName.name).toString()
                binding.text2Edit.text = getString(R.string.p3_great, name)
            }
        }

        binding.btn2Accept.setOnClickListener {
            when (page) {
                "2" -> {
                    if (binding.textInputName.text != null) {
                        val intent = Intent(this, GamePlay::class.java)
                        intent.putExtra(PageName.name, binding.textInputName.text.toString())
                        intent.putExtra(PageName.page, "3")
                        startActivity(intent)
                        finish()
                    }
                }
            }
        }

        binding.btn2Choice.setOnClickListener {
            val intent = Intent(this, ScenarioPlay::class.java)
            intent.putExtra(PageName.page, "4")
            startActivity(intent)
            finish()
        }

        binding.btn2Choice2.setOnClickListener {
            val intent = Intent(this, ScenarioPlay::class.java)
            intent.putExtra(PageName.page, "5")
            startActivity(intent)
            finish()
        }

        binding.btn2Choice3.setOnClickListener {
            val intent = Intent(this, ScenarioPlay::class.java)
            intent.putExtra(PageName.page, "6")
            startActivity(intent)
            finish()
        }
    }

Within that code snippet, there are several different logics that are executed sequentially in the onCreate method. This includes setting the visibility of the button, setting the text in binding.text2Edit, logic based on the page value to set the visibility and text, and adding click functionality on the buttons to intent to the ScenarioPlay class.

Lengthy onCreate methods like this can make it difficult to understand the code and maintain it afterwards. It is recommended to separate the different logic into separate methods to increase the clarity and maintainability of the code.

Example 2

From ScenarioPlay.kt file

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

        val page = intent.getStringExtra(PageName.page).toString()
        val btnHide = findViewById<Button>(R.id.btn4Choice2)

        when (page) {
            "4" -> {
                //binding.layout.background = getDrawable(R.drawable.walking)
                binding.imgView2.setImageResource(R.drawable.walking)
                //imageView.setImageResource(R.drawable.walking)
                binding.text4Edit.text = getString(R.string.p4_question)
                binding.btn4Choice1.text = getString(R.string.p4_btn1)
                binding.btn4Choice2.text = getString(R.string.p4_btn2)
            }
            "5" -> {
                //binding.layout.background = getDrawable(R.drawable.hiking)
                binding.imgView2.setImageResource(R.drawable.hiking)
                //imageView.setImageResource(R.drawable.hiking)
                binding.text4Edit.text = getString(R.string.p5_question)
                binding.btn4Choice1.text = getString(R.string.p5_btn1)
                binding.btn4Choice2.text = getString(R.string.p5_btn2)
            }
            "6" -> {
                //binding.layout.background = getDrawable(R.drawable.field)
                binding.imgView2.setImageResource(R.drawable.field)
                //imageView.setImageResource(R.drawable.field)
                binding.text4Edit.text= getString(R.string.p6_question)
                binding.btn4Choice1.text = getString(R.string.p6_btn1)
                binding.btn4Choice2.text = getString(R.string.p6_btn2)
            }
            "7" -> {
                binding.imgView2.setImageResource(R.drawable.watching)
                binding.text4Edit.text = getString(R.string.p7_question)
                binding.btn4Choice1.text = getString(R.string.p7_btn1)
                binding.btn4Choice2.text = getString(R.string.p7_btn2)
            }
            "8" -> {
                binding.imgView2.setImageResource(R.drawable.helloween)
                binding.text4Edit.text = getString(R.string.p8_question)
                binding.btn4Choice1.text = getString(R.string.p8_btn1)
                binding.btn4Choice2.text = getString(R.string.p8_btn2)
            }
            "9" -> {
                btnHide.visibility = View.GONE
                binding.imgView2.setImageResource(R.drawable.watching)
                binding.text4Edit.text = getString(R.string.p9_question)
                binding.btn4Choice1.text = getString(R.string.p9_btn1)
                //binding.btn6Choice2.text = getString(R.string.p9_btn2)
            }
            "10" -> {
                btnHide.visibility = View.GONE
                binding.imgView2.setImageResource(R.drawable.watching)
                binding.text4Edit.text = getString(R.string.p10_question)
                binding.btn4Choice1.text = getString(R.string.p10_btn1)
                //binding.btn6Choice2.text = getString(R.string.p8_btn2)
            }
            "11" -> {
                binding.imgView2.setImageResource(R.drawable.costume)
                binding.text4Edit.text = getString(R.string.p11_question)
                binding.btn4Choice1.text = getString(R.string.p11_btn1)
                binding.btn4Choice2.text = getString(R.string.p11_btn2)
            }
            "12" -> {
                btnHide.visibility = View.GONE
                binding.imgView2.setImageResource(R.drawable.costume)
                binding.text4Edit.text = getString(R.string.p12_question)
                binding.btn4Choice1.text = getString(R.string.p9_btn1)
                //binding.btn6Choice2.text = getString(R.string.p9_btn2)
            }
            "13" -> {
                btnHide.visibility = View.GONE
                binding.imgView2.setImageResource(R.drawable.costume)
                binding.text4Edit.text = getString(R.string.p13_question)
                binding.btn4Choice1.text = getString(R.string.p10_btn1)
                //binding.btn6Choice2.text = getString(R.string.p8_btn2)
            }
        }

        binding.btn4Choice1.setOnClickListener {
            when (page) {
                "4", "5", "6", "8" -> {
                    val intent = Intent(this, ScenarioPlay::class.java)
                    intent.putExtra(PageName.page, "7")
                    startActivity(intent)
                    finish()
                }
                "7" -> {
                    val intent = Intent(this, ScenarioPlay::class.java)
                    intent.putExtra(PageName.page, "9")
                    startActivity(intent)
                    finish()
                }
                "9", "10", "12", "13", -> {
                    val intent = Intent(this, EndGame::class.java)
                    startActivity(intent)
                    finish()
                }
                "11" -> {
                    val intent = Intent(this, ScenarioPlay::class.java)
                    intent.putExtra(PageName.page, "12")
                    startActivity(intent)
                    finish()
                }
            }
        }

        binding.btn4Choice2.setOnClickListener {
            val intent = Intent(this, ScenarioPlay::class.java)
            when (page) {
                "4","5","6" -> {intent.putExtra(PageName.page, "8")}
                "7" -> {intent.putExtra(PageName.page, "10")}
                "8" -> {intent.putExtra(PageName.page, "11")}
                "11" -> {intent.putExtra(PageName.page, "13")}
            }
            startActivity(intent)
            finish()
        }
    }

The onCreate method in the code has a lot of logic related to setting the view based on the page value. Each when block checks the page value and configures the view accordingly.

Long Method occurs when a method becomes too long and complex. In this case, the onCreate method has many different when blocks that adjust the view by page. Because this logic is tied to view settings, extending this method makes it difficult to understand and maintain.

LidiaIvanova commented 1 year ago

1