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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…