zul-aji / CodeRef_LabPro

Repository for Code Refactoring Lab project 1
0 stars 0 forks source link

God Object #6

Open zul-aji opened 1 year ago

zul-aji commented 1 year ago
Example 1 ## Code Snippet ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) .... aDateInteger = intent.getIntExtra(PassedData.DATE_INTEGER, 0) dateString = intent.getStringExtra(PassedData.DATE_INFO).toString() btnState = intent.getBooleanExtra(PassedData.DEBTOR_INFO, false) nameField = binding.addCreditorName amountField = binding.debtSum setInfo() val debtDao = DebtDatabase.getInstance(this).debtsDao() val model: DebtContract.Model = DebtModel(debtDao) val presenter = DebtPresenter(this, model) binding.debtorSwitch.setOnCheckedChangeListener { _, isChecked -> btnState = isChecked == true } val c = Calendar.getInstance() .... binding.dateButton.setOnClickListener { val datePickerDialog = DatePickerDialog( this, { _, year, monthOfYear, dayOfMonth -> .... }, year, month, day ) datePickerDialog.show() } val currIndex = when (intent.getStringExtra(PassedData.CURR_INFO)) { .... } val spinValue = binding.currencySpinner ArrayAdapter.createFromResource( this, R.array.spinner_value, android.R.layout.simple_spinner_item ).also { adapter -> adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) spinValue.adapter = adapter spinValue.setSelection(currIndex) } binding.saveBtn.setOnClickListener { val debtorName = binding.addCreditorName.text.toString() .... isAllFieldsChecked = checkAllFields() debtDate = fillDate(debtDate) if (isAllFieldsChecked) { when (intent.getStringExtra(PassedData.ADD_SAVE)) { "add" -> { .... ) Toast.makeText(this, getString(R.string.update_toast), Toast.LENGTH_SHORT).show() } "save" -> { .... } } finish() } } binding.deleteBtn.setOnClickListener { val builder = AlertDialog.Builder(this@AddDebtDetails) builder.setMessage(getString(R.string.delete_popup)) .setCancelable(false) .setPositiveButton("Yes") { _, _ -> .... } .setNegativeButton("No") { dialog, _ -> dialog.dismiss() } val alert = builder.create() alert.show() } } ``` ## Code Description The code snippet above is a method for when the activity is created what will be shown and what is bonded to its layout. In the code snippet, we can see that it has multiple responsibilities that know too much, does too much, and has too much control over the system. ## Flaw Reason The code snippet violates the Single Responsibility Principle (SRP) and the Separation of Concerns. The method directly interacts with multiple objects and dependencies, such as `binding`, `DebtDatabase`, `DebtContract.Model`, `DebtPresenter`, and the Android UI elements which exhibits tight coupling.
LidiaIvanova commented 1 year ago

God object is whole class usually. 0,5