Do you think this is 6 to write code, but I do not understand

Source | silent king

Zebian | Carol

Cover photo | CSDN│ download the visual China

In order to improve Java programming skills, learn some authors have recently written code 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(1, 5).boxed().map(i -> { System.out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(System.out::println);

Although the author has 10 years of experience in Java programming, 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.

You should be able to be able to understand, right? Generally means is that, if functional programming Guava excessive use of words will lead to code tedious, confusing, unreadable, even inefficient; if some developers in order to reduce the length of the code, deliberately put multiple lines of code "optimization" in a row when the code, Guava will be playing even cry.

I can only say, good people are really terrible, he not only knew their strengths, learn more about his own shortcomings - that is you, Guava developers. As for Daniel, who mentioned at the beginning, he wrote the code I can boast about, except to say virtuoso to show off their blind confidence in it. In my experience, only a small part of a large cattle to maintain sanity in the pursuit of technological innovation virtuoso aware of the problem.

I think, on the recommendation of Guava wiki also applies to Java 8, good technology to properly use, not abuse. As we all know, Java 8's new features can be used to reduce redundant code, when we put a complex anonymous inner class into a simple Lambda expression is a good example.

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();

You see, the less amount of code optimization, and at a glance, who can not read will not get the point. The purpose of functional programming can appear not only to reduce redundant code, which is to liberate the productive forces - that is to say between the lines, code complexity point does not matter, as long as the available and reliable. The program goal is not to generate as little code, but from easy maintenance, high-performance system.

Take, for example, if I start from Luoyang to Zhengzhou to attend a technical Sharon, I have no need to fly; and driving high-speed rail is the best choice. Understand what I mean, right? Do the whole of those bells and whistles, practical first.

"Force plan [the second quarter] - learning ability Challenge" started! From now until March 21, ten million flow to support the original author! Exclusive [more] medal waiting for you to challenge

推荐阅读:如何与亦敌亦友的 null 说拜拜?大神原来是这么做的!
大数据成长之路:谈谈那些必须学习的Linux基础知识
无需3D运动数据训练,最新人体姿势估计方法达到SOTA | CVPR 2020
国产 14nm 迎曙光,进口荷兰光刻机顺利入厂!
从哈希函数、哈希冲突、开散列出发,一文告诉你哈希思想与哈希表构造到底是什么!
前端 Webpack 工程化的最佳实践
真香,朕在看了!
Published 276 original articles · won praise 1230 · Views 1.15 million +

Guess you like

Origin blog.csdn.net/FL63Zv9Zou86950w/article/details/104787588