优秀代码-01减少括号嵌套

优秀代码-01减少括号嵌套

lucas Lv4

前言

这是我新起的一个系列,目的地总结一下写出优秀代码的方法。

大家都知道,代码如果括号嵌套太多了,会影响代码的可读性和扩展性,这次就总结一些代码中减少嵌套的一些方法

第一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
fun test1(condition1: Boolean, condition2: Boolean, condition3: Boolean) {
if (obj != null) {
if (condition1) {
while (true) {
if (condition2) {
if (condition3) {
}
}
}
}
} else {
throw NullPointerException("obj is Null")
}
}

上面这个函数,下面一下子放了 6 层括号,已经很复杂了,我们可以进行如下更改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fun test2(condition1: Boolean, condition2: Boolean, condition3: Boolean) {
if (obj == null) {
throw NullPointerException("obj is Null")
}
if (!condition1) {
return
}

while (true) {
if (!condition2) {
continue
}
if (condition3) {
}
}
}
  1. 提前抛异常

    我们在 obj = null 的时候,直接抛出异常,减少了一个 else 括号

  2. 提前返回(Early Return)

    我们直接让在不满足 condition1 的情况下,直接返回。

    这样,我么就可以将满足 condition1 的情况直接拿出来,从而减少了一层嵌套。

  3. 提前跳出循环

    我们遇到不满足 condition2 的情况,直接跳过本次循环,将满足 condition2 的情况拿了出来,从而减少了一层嵌套

第二个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fun onClick(v: View) {
val id: Int = v.getId()
if (id == R.id.button1) {
startActivity(Intent(this, StandardActivity::class.java))
} else if (id == R.id.button2) {
startActivity(Intent(this, SingleTopActivity::class.java))
} else if (id == R.id.button3) {
startActivity(Intent(this, SingleTaskActivity::class.java))
} else if (id == R.id.button4) {
startActivity(Intent(this, SingleInstanceActivity::class.java))
} else if (id == R.id.button4) {
startActivity(Intent(this, SingleInstanceActivity::class.java))
} else if (id == R.id.button5) {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity1::class.java))
} else if (id == R.id.button6) {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity2::class.java))
} else if (id == R.id.button7) {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity3::class.java))
} else if (id == R.id.button8) {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity4::class.java))
} else if (id == R.id.button9) {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity5::class.java))
} else if (id == R.id.button10) {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity1::class.java))
} else if (id == R.id.button11) {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity2::class.java))
} else if (id == R.id.button12) {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity3::class.java))
} else if (id == R.id.button13) {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity4::class.java))
}
}

上述代码用了大量if-else

并且都是判断id的,这种键值对判断,我们就可以改用switch-casekotlin里用的是when,或者用map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
fun onClick(v: View) {
val id: Int = v.getId()
when (id) {
R.id.button1 -> {
startActivity(Intent(this, StandardActivity::class.java))
}
R.id.button2 -> {
startActivity(Intent(this, SingleTopActivity::class.java))
}
R.id.button3 -> {
startActivity(Intent(this, SingleTaskActivity::class.java))
}
R.id.button4 -> {
startActivity(Intent(this, SingleInstanceActivity::class.java))
}
R.id.button4 -> {
startActivity(Intent(this, SingleInstanceActivity::class.java))
}
R.id.button5 -> {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity1::class.java))
}
R.id.button6 -> {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity2::class.java))
}
R.id.button7 -> {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity3::class.java))
}
R.id.button8 -> {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity4::class.java))
}
R.id.button9 -> {
startActivity(Intent(this, SingleTopActivity.SingleTopActivity5::class.java))
}
R.id.button10 -> {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity1::class.java))
}
R.id.button11 -> {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity2::class.java))
}
R.id.button12 -> {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity3::class.java))
}
R.id.button13 -> {
startActivity(Intent(this, SingleTaskActivity.SingleTaskActivity4::class.java))
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
val classMap = HashMap<Int, Class<out FragmentActivity>>().apply {
put(R.id.button1, StandardActivity.class)
put(R.id.button2, SingleTopActivity.class)
put(R.id.button3, SingleTaskActivity.class)
put(R.id.button4, SingleInstanceActivity.class)
put(R.id.button5, SingleTopActivity.SingleTopActivity1.class)
put(R.id.button6, SingleTopActivity.SingleTopActivity2.class)
put(R.id.button7, SingleTopActivity.SingleTopActivity3.class)
put(R.id.button8, SingleTopActivity.SingleTopActivity4.class)
put(R.id.button9, SingleTopActivity.SingleTopActivity5.class)
put(R.id.button10, SingleTaskActivity.SingleTaskActivity1.class)
put(R.id.button11, SingleTaskActivity.SingleTaskActivity2.class)
put(R.id.button12, SingleTaskActivity.SingleTaskActivity3.class)
put(R.id.button13, SingleTaskActivity.SingleTaskActivity4.class)
}

override fun onClick(v: View) {
startActivity(Intent(this, classMap[v.id]))
}

ps:我推荐一个jetbrains插件,叫Better Highlights,他其中一个免费功能就是帮你检查方法的复杂度

  • Title: 优秀代码-01减少括号嵌套
  • Author: lucas
  • Created at : 2023-10-14 23:42:27
  • Updated at : 2024-11-12 09:01:29
  • Link: https://darkflamemasterdev.github.io/2023/10/14/优秀代码-01减少括号嵌套/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
优秀代码-01减少括号嵌套