Do not write Java code that's cool but my colleagues do not understand

You okay, I was silent king, and a height, like Wong Ka Kui, Yan and Andy Lau as the value of the programmer. In order to improve the skills of Java programming, I recently learned some code written expert on GitHub. The following line of code (from the hands of Daniel) is said can conquer your friends, let them think you write the code is 6, to appreciate it.

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

Although I have 10 years of Java programming experience, but to be honest, very ashamed, this code is like a brick pat on my head, a little ignorant force. Lambda expressions I learned, Stream stream I learned, forEach()the way I also learned, but the point of all this knowledge stuffed into one line of code, I was a little indigestion.

I always think it is better to replace the old line of code syntax (Java before 7) to write like this:

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

Wow, beautiful code, concise and clear! Not only I can understand, even just to get Java White can understand. You know, the code is written on posters, if only you can understand, just feel very 6, while others seem foggy, it is not necessarily a good codes, although seems to be walking in the forefront of technology .

As the saying goes, "With great power comes great responsibility." Java 8 provides us with powerful capabilities, it is a landmark of the new features (after 8 Java version of the new features are not enough dazzling), including Lambda expressions and Stream flow, we can write concise and efficient through them code. To make an inappropriate analogy, before Java 8, developers are driving Santana, after Java 8, developers driving a Ferrari.

But if not, then driving skills, it can become Ferrari Santana, even worse. Before that period seem to get code is 6 for instance, it is not for some good code - despite the use of new technology, but it is difficult to understand.

Before Java 8, if you want to write functional code, it should be using Google's Guava library, it is a great open-source library (not familiar with it), it can make up for the lack of native Java class libraries to a certain extent . I saw a piece of advice like the following on its wiki, made it very visionary:

Excessive use of Guava’s functional programming idioms can lead to verbose, confusing, unreadable and inefficient code. .. when you go to preposterous lengths to make your code “a one-line”, the Guava team weeps.

应该能看得懂吧?大致的意思就是说,如果过度使用 Guava 的函数式编程的话,会导致代码冗长、混乱、不可读,甚至低效;如果有些开发者为了减少代码的长度,刻意把多行代码“优化”成一行代码时,Guava 甚至会被玩哭。

我只能说,优秀的人真可怕,他不仅知道自己的长处,更了解自己的不足——说的就是你,Guava 的开发者。至于开头提到的那位大牛,他写的代码我就不敢恭维,只能说炫技炫到盲目自信吧。根据我的经验,只有很少一部分的大牛能够保持理智,在追求技术创新的同时意识到炫技的问题。

我认为,Guava wiki 上的建议同样适用于 Java 8,好技术要妥善的利用,而不是滥用。众所周知,Java 8 的新特性可以用来减少冗余代码,当我们把一个复杂的匿名内部类变成一个简洁的 Lambda 表达式就是一个很好的例子。

Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("匿名内部类,搞起来");
    }
});
t1.start();

// 优化后

Thread t2 = new Thread(() -> {
    System.out.println("Lambda 表达式,搞起来");
});
t2.start();

你看,优化后的代码量更少,并且一目了然,任谁也不会搞到看不懂的地步。函数式编程出现的目的可不仅仅是为了减少冗余代码,它是为了解放生产力——言外之意就是说,代码复杂点没关系,只要可用可靠。编程的目标不是产生尽可能少的代码,而是产生易于维护的、高性能的系统。

举个例子来说,假如我从洛阳出发,去郑州参加一个技术沙龙,我就没必要坐飞机;高铁和驾车才是最优的选择。明白我说的意思吧?别整那些花里胡哨的,实用至上。

好了,我亲爱的读者朋友,以上就是本文的全部内容了,能看到这里的就是最优秀的程序员。原创不易,莫要白票,请你为本文点赞吧,这将是我写作更多优质文章的最强动力。

如果觉得文章对你有点帮助,请微信搜索「 沉默王二 」第一时间阅读,回复【666】【1024】更有我为你精心准备的 500G 高清教学视频(已分门别类),以及大厂技术牛人整理的面经一份,本文源码已收录在码云传送门~

Guess you like

Origin www.cnblogs.com/qing-gee/p/12432756.html