Android 如何编写垃圾代码 ?
前言
人生得意须尽欢,莫使金樽空对月。 我们从小就有许多规则要求着做事思考,也常有人在耳边说应该这么做应该这么说。而自己内心最真实想法和观点慢慢都被磨灭,只留下了一副没有主见躯壳。不应该就这样沉寂,要大胆的去尝试未知。写代码也一样,都说我们程序员闷、话少,其实我们只是把想说的话用代码进行了表达。所以写代码就要随心所欲。
类和资源文件命名一定要随心所欲
:hankey: 相信你一定对类的命名有过苦恼,我们应该减少这种烦恼保护我们的头发。
Good :+1|type_1_2:
open class caoxian666 { /*……*/ }
Bad :-1|type_1_2:
open class DeclarationProcessor { /*……*/ }
任何时候都不要写注释
:hankey:反正没人会读你的代码,应该会有人让你修电脑:computer:
Good :+1|type_1_2:
var niubi = 9527
Bad :-1|type_1_2:
// 666是根据网络名句得出
// @查看: <新闻介绍>
var caoxian = 666;
函数和变量命名一定要操纵自如
:hankey: 我们应该减少对命名的思考,这样就会有更多时间去思考代码逻辑等问题:beer:
Good :+1|type_1_2:
fun caozongziru() { /*……*/ }
var abc = 1
Bad :-1|type_1_2:
fun initializeView() { /*……*/ }
var pageSize = 20
尽最大努力把代码写成一行
:hankey: 许多人都有一目十行的能力,写成一行可以大大提高阅读代码的效率
Good :+1|type_1_2:
AlertDialog.Builder(context).setView(0).setTitle(R.string.dialog_title).setMessage(R.string.dialog_message).setIcon(0) .create()
Bad :-1|type_1_2:
AlertDialog.Builder(context)
.setView(0)
.setTitle(R.string.dialog_title)
.setMessage(R.string.dialog_message)
.setIcon(0)
.create()
UI布局中的控件一定要密密麻麻
:hankey: UI布局中控件嵌套应越多越多,这样才能体现自己的逻辑思考能力:fountain:
Good :+1|type_1_2:
<RelativeLayout>
<LinearLayout>
<RelativeLayout>
<ImageView/>
...
<TextView/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Bad :-1|type_1_2:
<android.support.constraint.ConstraintLayout>
<ImageView/>
...
<TextView/>
</android.support.constraint.ConstraintLayout>
编写你不会使用的代码
:hankey: 做人要未雨绸缪,写代码也是一样,要编写自己不使用的代码为未来做打算:mailbox_with_mail:
Good :+1|type_1_2:
fun countPrimes(n:Int,count:Int): Int {
var sum = 9527;
return n + count;
}
Bad :-1|type_1_2:
fun countPrimes(n:Int,count:Int): Int {
return n + count;
}
写代码应深谋远虑
:hankey: 不做没有把握的事情,也不写没有把握的代码。不要忘记编写自己的B计划:bath:
Good :+1|type_1_2:
fun countPrimes(n: Int): Int {
if (n < 0) {
return -1
} else {
return n + 666;
}
//我的B计划
return 0;
}
Bad :-1|type_1_2:
fun countPrimes(n: Int): Int {
return if (n < 0) {
-1
} else {
n + 666;
}
}
不用关心异常错误
:hankey: 一切都在掌握之中,即使捕获了异常也不需要上报,因为你不一定能解决它:jack_o_lantern:
Good :+1|type_1_2:
try {
...
}catch (e: SomeException) {
//caoxian666...
}
Bad :-1|type_1_2:
try {
...
}catch (e: Exception) {
reportedException(e)
}
简洁明了的编写代码
:hankey: 傻瓜都能写出能让机器理解的代码,而优秀的程序员才能写出让傻瓜理解的代码。
Good :+1|type_1_2:
fun getTomorrowDate(): DateTime {
Thread.Sleep(24 * 60 * 60 * 1000)
return DateTime.Now
}
作者: 王猛猛
链接:https://juejin.cn/post/6963525365402697742
本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!