Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

kotlin - 缩短代码会有负面影响吗?(Are there negative effects of shortening code?)

I wanted to know whether there are negative side effects (perfomance issues, readability etc) of shortening code, for example converting this:

(我想知道缩短代码是否有负面影响(性能问题,可读性等),例如将其转换为:)

fun getColor():Int{
    val randNumGenerator = Random
    val randNum =randNumGenerator.nextInt(colourList.size)
    val colorInt = colourList[randNum]

    return Color.parseColor(colorInt)
}

to this

(对此)

fun getColor():Int = Color.parseColor(colourList[Random.nextInt(colourList.size)])

OR from this

(或者从这里)

private fun rollDice(){
    val drawableResource = when(Random.nextInt(6) + 1){
        1 -> R.drawable.dice_1
        2 -> R.drawable.dice_2
        3 -> R.drawable.dice_3
        4 -> R.drawable.dice_4
        5 -> R.drawable.dice_5
        else -> R.drawable.dice_6
    }
    diceImageView.setImageResource(drawableResource)
}

to this

(对此)

private fun rollDice(){        
    diceImageView.setImageResource(
         when(Random.nextInt(6) + 1){
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }
    )
}
  ask by Minathe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...