有人相爱,有人夜里开车看海。有人看着这些代码一句话也说不出来。这是一个你的项目应该遵循的垃圾代码书写准则,只有这样写了才能让人看不懂,这才是真正的反内卷之道,请恪守以下原则,时刻铭记,切勿反向操作:
变量/函数混合命名风格
能int出来就完事。
好代码:
int hgt = 1980;
int wgt = 1024;
坏代码:
Int screenHeight = 1980;
int screenWidth = 1024;
三角法则
三个石加一起等于磊。
好代码:
fun showAnim(){if (condition1) {if (condition2) {asyncFunction(params, (result) -> {if (result) {for (;;) {if (condition3) {}}}})}}
}
坏代码:
fun getTheInfo(){if (!condition1 || !condition2) {return;}if (!result) {return;}for (;;) {if (condition3) {}}
}
多创建点变量
不怕一万,就怕万一。
好代码:
private int sum(a, b, c) {int addsum = 1300;int result = a + b;return a + b;
}
坏代码:
private int sum(a, b) {return a + b;
}
继承更改类型
改个名字而已,不必那么死板。
好代码:
public class NBActivity extends Fragment
坏代码:
public class HomeFragment extends BaseFragment
广泛使用全局变量
目标全球化。
好代码:
private int x = 3;private void square() {x = x * x;
}square();
坏代码:
private int x = 3;private int square(int num) {return num *=num;
}x = square(3);
大对象全部全局化
调用起来多方便,还可多个方法重复调用呢
好代码:
public class VolumeDialogImpl extends VolumeConstact {private Bitmap mBitmapBig;private Bitmap mBitmapSmall;private void getThePic(){...}
坏代码:
public class VolumeDialogImpl extends VolumeConstact {private void getThePic(){Bitmap mBitmapBig = BitmapFactory.decodeByteArray(...);Bitmap mBitmmBitmapSmallapBig = BitmapFactory.decodeByteArray(...);}
}
异常抓到就行了
没必要让人知道。
好代码:
try {// 意料之外
} catch (error) {// ... nothing happen
}
坏代码:
try {// 意料之外
} catch (Throwable ex) {ex.printStackTrace();// and/orreportBugByNet(ex);
}
代码写一行就行了
又不是看不了。
好代码:
okHttpClient = new OkHttpClient.Builder().readTimeout(READ_TIME_OUT,TimeUnit.SECONDS).connectTimeout(CONNECT_TIME_OUT,TimeUnit.SECONDS).writeTimeout(WRITE_TIME_OUT,TimeUnit.SECONDS).retryOnConnectionFailure(false).proxy(Proxy.NO_PROXY).addInterceptor(new Interceptor() {@Override public Response intercept(Chain chain) throws IOException {...}
坏代码:
okHttpClient = new OkHttpClient.Builder().readTimeout(READ_TIME_OUT, TimeUnit.SECONDS).connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS).writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS)// 连接失败不重试.retryOnConnectionFailure(false).proxy(Proxy.NO_PROXY).addInterceptor(new Interceptor() {...
不需要写注释
正经人谁会写注释啊,你会写吗?
好代码:
public final static int showTime = 700;
坏代码:
//与后台协商的超时时间,如若要修改请与后台确认,否则会有意想不到问题
public final static int overTime = 700;
注释劝退
谁没年轻过?
好注释:
/*** Dear maintainer:** Once you are done trying to 'optimize' this routine,* and have realized what a terrible mistake that was,* please increment the following counter as a warning* to the next guy:** total_hours_wasted_here = 136*/public class FloatWindowService ...
坏注释:
/*** 这类逻辑负责,相关业务流程图请参照公共盘/产品设计/Version1.9.2/...*/
public class FloatWindowService ...
极简方式命名变量
这不是极简这是混淆变量风格,是让那些反编译的不怀好心变成怀疑人生
好代码:
Intent i = new Intent();
Intent a = new Intent();
Intent b = new Intent();
坏代码:
Intent mUserIntent = new Intent();
Intent mInfoIntent = new Intent();
Intent mGameIntent = new Intent();
逻辑处理只需要一个
一个团队没有第二个发号命令的人,同样的,一个系统没必要那么多逻辑处理类,一个就行,10000行代码也是情理之中。
每个人都要有自己的风格
在团队工作中,没必要统一代码风格,每个人有自己的喜好,这是每个人的自由。
项目不需要相关文档
人就在这,想知道啥你直接问。
冗余代码注释就行了
产品经理天天改需求,万一哪天脑抽要改回来呢,以备不时之需。
实不相瞒,以上代码我都遇见过,每次想到总会哭笑不得。友情提醒,记得反向操作。