5

垃圾代码书写准则(有意思)

 3 years ago
source link: http://www.itwanger.com/java/2021/01/21/shit-code.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

开门见山地说吧,在逛 GitHub 的时候,发现了一个非常有意思的项目,地址如下所示:

https://github.com/trekhleb/state-of-the-art-shitcode

名叫“垃圾代码书写准则”,瞧这名字,我真的是服了。作者也是良苦用心,从反面教材的角度来阐述正确书写代码格式的重要性。作者使用 JavaScript 编写的代码示例,我把它重制成了 Java 版,并且用我自己的语言风格翻译成了中文,希望小伙伴们能够喜欢。

💩 以一种容易造成代码混淆的方式命名变量

命名越短,就需要越多的时间去思考代码逻辑等问题。

Good 👍🏻

int a = 42;

Bad 👎🏻

int age = 42;

💩 变量/方法命名风格不统一

为风格不统一干杯。

Good 👍🏻

int wWidth = 640;
int w_height = 480;

Bad 👎🏻

int windowWidth = 640;
int windowHeight = 480;

💩 不写注释

反正没人能读懂你的代码。

Good 👍🏻

int cdr = 700;

Bad 👎🏻

注释应该包含一些“为什么”,而不是一些“是什么”。如果代码连是“什么”都表达不清楚,那代码也太烂了。

// 700ms 的数量是从 UX A/B 测试结果中得到的一个经验值。
// @查看: <详细解释 700 的一个链接>
int callbackDebounceRate = 700;

💩 使用母语写注释

如果你的母语是英语,那么请忽略这条准则。

Good 👍🏻

// Закриваємо модальне віконечко при виникненні помилки.
toggleModal(false);

Bad 👎🏻

// 隐藏错误弹窗
toggleModal(false);

PS:如果英语书写能力不是很强的话,建议还是用母语吧。毕竟说清楚总比说不清楚要强。

💩 声明变量的风格不统一

再次为风格不统一干杯。

Good 👍🏻

String [] i1 = {"沉", "默", "王", "二"};
String i2 [] = {"沉", "默", "王", "三"};

Bad 👎🏻

String [] wanger = {"沉", "默", "王", "二"};
String wangsan [] = {"沉", "默", "王", "三"};

💩 尽可能把代码写成一行

Good 👍🏻

IntStream.range(1, 5).boxed().map(i -> { System.out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(System.out::println);

Bad 👎🏻

for (int i = 1; i < 5; i++) {
    System.out.println("Happy Birthday " + (i == 3 ? "dear NAME" : "to you"));
}

💩 对错误信息不管不顾

无论什么时候发现错误,都没有必要让其他人知道。

Good 👍🏻

try {
  // 意料之外的情况。
} catch (error) {
  // tss... 🤫
}

Bad 👎🏻

try {
  // 意料之外的情况。
} catch (error) {
  // and/or
  logError(error);
}

💩 使用大量的全局变量

全球化的原则。

Good 👍🏻

int x = 5;

void multi() {
  x = x * 2;
}

multi(); // 现在 x 是 10

Bad 👎🏻

int x = 5;

int multi(int num) {
  return num * 2;
}

x = multi(x); // 现在 x 是 10

💩 声明根本不会使用的变量

万一以后用了呢?以备不时之需。

Good 👍🏻

int sum(int a, int b, int c) {
  int timeout = 1300;
  int result = a + b;
  return a + b;
}

Bad 👎🏻

int sum(int a, int b) {
  return a + b;
}

💩 如果条件允许的话,从不指定类型。

Good 👍🏻

// 享受便捷的快乐
List list = new ArrayList();
list.add("沉默王二");
list.add(18);

Bad 👎🏻

List<String> nameList = new ArrayList<String>();

// 编译出错
nameList.add(18);

💩 没鸟用的代码

看起来更严谨,其实很多余。

Good 👍🏻

Integer multi(Object num) {
    if (!(num instanceof Integer)) {
        return null;
    } else if (num != null) {
        return (Integer) num * 2;
    }
    return null;
}

Bad 👎🏻

Integer multi(Object num) {
    if (num instanceof Integer) {
        return (Integer) num * 2;
    }
    return null;
}

💩 大量的 if-else 嵌套

Good 👍🏻

void someMethod(int a, int b, int c) {
    if (a > 0) {
        if (b > 0) {
            if (c > 0) {
               int result = a / b / c;
            }
        }
    }
}

Bad 👎🏻

void someMethod1(int a, int b, int c) {
    if (a < 0 || b < 0 || c < 0) {
        return;
    }
    int result = a / b / c;
}

💩 参差不齐地缩进

参差不齐乃幸福本源。

Good 👍🏻

String [] wanger = {"沉", 
        "默", "王", "二"};
String [] wangsan = {"沉", "默", "王", "三"};
Arrays.asList(wanger).stream().
        forEach(System.out::println);
Arrays.asList(wangsan).
        stream().
                forEach(System.out::println);

Bad 👎🏻

String [] wanger = {"沉", "默", "王", "二"};
String [] wangsan = {"沉", "默", "王", "三"};
Arrays.asList(wanger)
        .stream()
        .forEach(System.out::println);
Arrays.asList(wangsan)
        .stream()
        .forEach(System.out::println);

💩 代码行数多的方法的比少的好

不要把代码逻辑分成可读的部分。

  • 一个类中的代码行数超过 10000 行。
  • 一个方法中的代码行数超过 1000 行。
  • 一个方法里既做减法处理又做加法处理,还做乘除的处理。

💩 不要测试你的代码

代码测试是测试工程师的事,关我屁事。

💩 避免代码风格统一

随心所欲地编写代码,特别是在一个团队中有多个开发人员的情况下,我崇尚“自由”。

💩 不要写文档

从一开始就不要。

💩 不要删除废弃掉的代码

代码尽管已经废弃了,注释掉就行了,没必要删掉。

好了,小伙伴们还有需要补充的吗?

喜欢逛 GitHub 的小伙伴们可以点击下面的地址跳转过去查看,我已经提交了:

https://github.com/itwanger/state-of-the-art-shitcode/blob/master/README.zh-CN.md

另外再推荐一个项目,里面包含了 Java 程序员常读的书单,帮你构建最强知识体系,机不可失时不再来。但不限于 Java,包括设计模式、计算机网络、操作系统、数据库、数据结构与算法、大数据、架构、管理等等。

shit-code-01.png

https://github.com/itwanger/JavaBooks

小伙伴们也可以去 star 下。周四加油,记得点赞,重拾我们的传统美德!

一枚沉默但有趣的程序员
chenmowanger_430.jpg
微信扫描二维码,关注我的公众号,回复“资源”领取海量学习资料

(转载本站文章请注明作者和出处 沉默王二

Show Disqus Comments

Related Issues not found

Please contact @qinggee to initialize the comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK