在 Kotlin 应用程序中将数据计算与数据格式化分开是最佳做法吗?

Is it best practice to separate data calculations from data formatting in Kotlin app?

我有一个代码库,我的所有数据计算和数据格式化都在一个函数中进行。我听说每个函数都有一个任务是最佳做法,但我想知道计算、格式化和显示是否可以作为数据处理适合一个函数?还是我应该把它们全部分开?

这里是我指的函数:

private fun processData(data: ByteArray) {
        progressBar.visibility = GONE
        Log.d(TAG, "displayDiagnosticData: ")

        val bmsVersionView = findViewById<TextView>(R.id.textview_bms_version)
        val boardVersionView = findViewById<TextView>(R.id.textview_board_version)
        val cellOneView = findViewById<TextView>(R.id.textview_cell_1)
        val cellTwoView = findViewById<TextView>(R.id.textview_cell_2)
        val cellThreeView = findViewById<TextView>(R.id.textview_cell_3)
        val cellFourView = findViewById<TextView>(R.id.textview_cell_4)
        val cellFiveView = findViewById<TextView>(R.id.textview_cell_5)
        val cellSixView = findViewById<TextView>(R.id.textview_cell_6)
        val cellSevenView = findViewById<TextView>(R.id.textview_cell_7)
        val cellEightView = findViewById<TextView>(R.id.textview_cell_8)
        val cellNineView = findViewById<TextView>(R.id.textview_cell_9)
        val cellTenView = findViewById<TextView>(R.id.textview_cell_10)
        val cellElevenView = findViewById<TextView>(R.id.textview_cell_11)
        val cellTwelveView = findViewById<TextView>(R.id.textview_cell_12)
        val cellThirteenView = findViewById<TextView>(R.id.textview_cell_13)
        val cellFourteenView = findViewById<TextView>(R.id.textview_cell_14)
        val packTotalView = findViewById<TextView>(R.id.textview_diagnostic_voltage)
        val packSocView = findViewById<TextView>(R.id.textview_diagnostic_soc)
        val chargeTempView = findViewById<TextView>(R.id.textview_charge_temp)
        val dischargeTempView = findViewById<TextView>(R.id.textview_discharge_temp)
        val chargeCurrentView = findViewById<TextView>(R.id.textview_diagnostic_charge_current)
//        val dischargeCurrentView = findViewById<TextView>(R.id.textview_diagnostic_discharge_current)
        val dischargeCircuitStateView = findViewById<TextView>(R.id.textview_discharge_circuit)
        val chargeCircuitStateView = findViewById<TextView>(R.id.textview_charge_circuit)
        val balanceCircuitStateView = findViewById<TextView>(R.id.textview_balance_circuit)
        val emptyCircuitStateView = findViewById<TextView>(R.id.textview_empty_circuit)

        val bmsVersion = data[0] + (data[1] * 256)
        val cellOne = data[2].toDouble() / 100 + 3.52
        val cellTwo = data[3].toDouble() / 100 + 3.52
        val cellThree = data[4].toDouble() / 100 + 3.52
        val cellFour = data[5].toDouble() / 100 + 3.52
        val cellFive = data[6].toDouble() / 100 + 3.52
        val cellSix = data[7].toDouble() / 100 + 3.52
        val cellSeven = data[8].toDouble() / 100 + 3.52
        val cellEight = data[9].toDouble() / 100 + 3.52
        val cellNine = data[10].toDouble() / 100 + 3.52
        val cellTen = data[11].toDouble() / 100 + 3.52
        val cellEleven = data[12].toDouble() / 100 + 3.52
        val cellTwelve = data[13].toDouble() / 100 + 3.52
        val cellThirteen = data[14].toDouble() / 100 + 3.52
        val cellFourteen = data[15].toDouble() / 100 + 3.52
        val totalVoltage = 47.8 + (data[16].toDouble() / 10)
        val chargeTempCelsius = data[19]
        val dischargeTempCelsius = data[20]
        val chargeTempFahr = (chargeTempCelsius * 9.0 / 5.0) + 32.0
        val dischargeTempFahr = (dischargeTempCelsius * 9.0 / 5.0) + 32.0
        val chargeCurrent = data[21]
//        val dischargeCurrent = (data[23].toDouble() * 100 + data[22]).toInt()
        val chargeCircuitState = data[25].toInt()
        val dischargeCircuitState = data[26].toInt()
        val balanceCircuitState = data[27].toInt()
        val emptyCircuitState = data[28].toInt()

        val chargeCircuit: String = if (chargeCircuitState == 1) {
            "On"
        }else {
            "Off"
        }

        val dischargeCircuit: String = if (dischargeCircuitState == 1) {
            "On"
        }else {
            "Off"
        }

        val balanceCircuit: String = if (balanceCircuitState == 1) {
            "On"
        }else {
            "Off"
        }

        val emptyCircuit: String = if (emptyCircuitState == 1) {
            "On"
        }else {
            "Off"
        }

        val bmsVersionString = SpannableString("BMS Version: $bmsVersion")
        bmsVersionString.setSpan(StyleSpan(Typeface.BOLD), 0, 11, 0)
        val boardVersionString = SpannableString("Board Version: 2.1")
        boardVersionString.setSpan(StyleSpan(Typeface.BOLD), 0, 13, 0)
        val cellOneString = SpannableString("Cell 1: %.2fV".format(cellOne))
        cellOneString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellTwoString = SpannableString("Cell 2: %.2fV".format(cellTwo))
        cellTwoString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellThreeString = SpannableString("Cell 3: %.2fV".format(cellThree))
        cellThreeString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellFourString = SpannableString("Cell 4: %.2fV".format(cellFour))
        cellFourString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellFiveString = SpannableString("Cell 5: %.2fV".format(cellFive))
        cellFiveString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellSixString = SpannableString("Cell 6: %.2fV".format(cellSix))
        cellSixString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellSevenString = SpannableString("Cell 7: %.2fV".format(cellSeven))
        cellSevenString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellEightString = SpannableString("Cell 8: %.2fV".format(cellEight))
        cellEightString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellNineString = SpannableString("Cell 9: %.2fV".format(cellNine))
        cellNineString.setSpan(StyleSpan(Typeface.BOLD), 0, 6, 0)
        val cellTenString = SpannableString("Cell 10: %.2fV".format(cellTen))
        cellTenString.setSpan(StyleSpan(Typeface.BOLD), 0, 7, 0)
        val cellElevenString = SpannableString("Cell 11: %.2fV".format(cellEleven))
        cellElevenString.setSpan(StyleSpan(Typeface.BOLD), 0, 7, 0)
        val cellTwelveString = SpannableString("Cell 12: %.2fV".format(cellTwelve))
        cellTwelveString.setSpan(StyleSpan(Typeface.BOLD), 0, 7, 0)
        val cellThirteenString = SpannableString("Cell 13: %.2fV".format(cellThirteen))
        cellThirteenString.setSpan(StyleSpan(Typeface.BOLD), 0, 7, 0)
        val cellFourteenString = SpannableString("Cell 14: %.2fV".format(cellFourteen))
        cellFourteenString.setSpan(StyleSpan(Typeface.BOLD), 0, 7, 0)
        val packTotalString = SpannableString("Pack Total: %.1fV".format(totalVoltage))
        packTotalString.setSpan(StyleSpan(Typeface.BOLD), 0, 10, 0)
        val socString = SpannableString("SOC: ${data[17].toInt()}%")
        socString.setSpan(StyleSpan(Typeface.BOLD), 0, 3, 0)
        val chargeTempString = SpannableString("Charge Temp: ${chargeTempFahr.toInt()}" + "°F")
        chargeTempString.setSpan(StyleSpan(Typeface.BOLD), 0, 11, 0)
        val dischargeTempString = SpannableString("Discharge Temp: ${dischargeTempFahr.toInt()}" + "°F")
        dischargeTempString.setSpan(StyleSpan(Typeface.BOLD), 0, 15, 0)
        val chargeCurrentString = SpannableString("Charge Current: $chargeCurrent" + "A")
        chargeCurrentString.setSpan(StyleSpan(Typeface.BOLD), 0, 14, 0)
//        val dischargeCurrentString = "Discharge Current: $dischargeCurrent" + "A"
        val chargeCircuitStateString = SpannableString("Charge Circuit: $chargeCircuit")
        chargeCircuitStateString.setSpan(StyleSpan(Typeface.BOLD), 0, 14, 0)
        val dischargeCircuitStateString = SpannableString("Discharge Circuit: $dischargeCircuit")
        dischargeCircuitStateString.setSpan(StyleSpan(Typeface.BOLD), 0, 17, 0)
        val balanceCircuitStateString = SpannableString("Balance Circuit: $balanceCircuit")
        balanceCircuitStateString.setSpan(StyleSpan(Typeface.BOLD), 0, 15, 0)
        val emptyCircuitStateString = SpannableString("Empty Circuit: $emptyCircuit")
        emptyCircuitStateString.setSpan(StyleSpan(Typeface.BOLD), 0, 13, 0)


        bmsVersionView.text = bmsVersionString
        boardVersionView.text = boardVersionString
        cellOneView.text = cellOneString
        cellTwoView.text = cellTwoString
        cellThreeView.text = cellThreeString
        cellFourView.text = cellFourString
        cellFiveView.text = cellFiveString
        cellSixView.text = cellSixString
        cellSevenView.text = cellSevenString
        cellEightView.text = cellEightString
        cellNineView.text = cellNineString
        cellTenView.text = cellTenString
        cellElevenView.text = cellElevenString
        cellTwelveView.text = cellTwelveString
        cellThirteenView.text = cellThirteenString
        cellFourteenView.text = cellFourteenString
        packTotalView.text = packTotalString
        packSocView.text = socString
        chargeTempView.text = chargeTempString
        dischargeTempView.text = dischargeTempString
        chargeCurrentView.text = chargeCurrentString
//        dischargeCurrentView.text = dischargeCurrentString
        chargeCircuitStateView.text = chargeCircuitStateString
        dischargeCircuitStateView.text = dischargeCircuitStateString
        balanceCircuitStateView.text = balanceCircuitStateString
        emptyCircuitStateView.text = emptyCircuitStateString

    }

我可以保持原样还是为了便于阅读将它分开更好?

首先,我强烈建议使用视图和数据绑定。它将减少 findviewById 行;现在是 2022 年,您必须改变这种行为。

其次,将代码拆分成多个函数。否则会产生代码味

当您发现自己的 copy-pasting 代码时,总是停下来考虑是否可以通过迭代或创建函数来解决相同的任务。

您对每个字符串都做同样的事情,使文本达到 : 粗体,因此您可以为此编写一个函数并消除一堆代码:

private fun String.withStyling() = SpannableString(this).apply {
    setSpan(StyleSpan(Typeface.BOLD), 0, indexOfFirst(':'), 0)
}

// and then for example:
bmsVersionView.text = "BMS Version: $bmsVersion".withStyling()
boardVersionView.text = "Board Version: 2.1".withStyling()
cellOneView.text = "Cell 1: %.2fV".format(cellOne).withStyling()
// ...

此外,您对每个单元格视图都执行完全相同的操作。所以列出它们并迭代!

val cellViews = listOf(
    cellOneView,
    cellTwoView,
    //...
)

for ((i, cellView) in cellViews.withIndex()) {
    val value = data[2 + i].toDouble() / 100 + 3.52
    val cellNumberString = (i + 1).toString()
    cellValue.text = "Cell $cellNumberString: %.2fV".format(value).withStyling()
}