Hello, I am silent king 2, a and Huang Jiaju the same height, and Andy Lau the same appearance level programmer. In order to improve my Java programming skills, I’ve been learning some of the best code on GitHub. Take a look at this line of code (written by Daniel) that is supposed to impress your friends by making them think your code is 6.

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);
Copy the code

I have 10 years of Java programming experience, but to be honest, this code feels like a brick in my head. I’ve learned about Lambda expressions, I’ve learned about streams, I’ve learned about the forEach() method, but I still get a little indigestion trying to cram it all into one line of code.

I always felt that this line of code would be better written in old-fashioned (pre-Java 7) syntax, as follows:

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

Wow, beautiful code, simple and clear! Not only I can understand, even the introduction of Java white can understand. You know, code is written for people to read, and if only you can understand it, and only you think it’s 6, and everyone else looks foggy, it’s not necessarily good code, even though it seems to be walking on the cutting edge of technology.

As the saying goes, “With great power comes great responsibility”. Java 8 gives us the power to write clean and efficient code with groundbreaking new features (none of which have been noticeable since Java 8), including Lambda expressions and Streams. To use an unfortunate analogy, before Java 8 developers drove Santana, after Java 8 developers drove Ferraris.

But a Ferrari can turn into a Santana if not a good driver, or worse. Take the code that looked like it was 6. It wasn’t good code — it was hard to understand, despite the novel technology.

Prior to Java 8, if you wanted to write functional code, you would have used Google’s Guava class library, which is a great open source class library that helps to compensate for Java’s native libraries to some extent. I saw this insightful piece of advice on its wiki:

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.

That should make sense, right? Basically, if you overuse Guava’s functional programming, your code will be long, confusing, unreadable, and even inefficient. Guava even gets teased when developers deliberately “optimize” multiple lines of code into one in order to reduce code length.

All I can say is that good people are terrible. They know not only their strengths, but also their weaknesses — I’m talking about you, the developer of Guava. As for the guy I mentioned at the beginning, I can’t say much about the code he wrote. In my experience, only a few of them are sane enough to realize the problem of showcasing skills while pursuing technological innovation.

In my opinion, the advice on the Guava Wiki also applies to Java 8, that good technology should be harnessed, not abused. It is well known that new features in Java 8 can be used to reduce redundant code, as is the case when we turn a complex anonymous inner class into a concise Lambda expression.

Thread t1 = new Thread(new Runnable() {
    @Override
    public void run(a) {
        System.out.println("Anonymous inner class, let's do it."); }}); t1.start();/ / after optimization

Thread t2 = new Thread(() -> {
    System.out.println(Lambda expression, let's do it.);
});
t2.start();
Copy the code

You see, the optimized code is less, and at a glance, no one can get to the point of not understanding. Functional programming isn’t just about eliminating redundant code, it’s about liberating productivity-the implication being that it doesn’t matter if your code is complicated, as long as it’s usable and reliable. The goal of programming is not to produce as little code as possible, but to produce high-performance systems that are easy to maintain.

For example, if I go from Luoyang to Zhengzhou to attend a technical salon, I don’t need to fly. High-speed rail and driving are the best choices. See what I mean? Don’t do the bells and whistles. Be practical.

Well, my dear readers, that’s all for this article, and the best programmers you can see here. Original is not easy, do not want a free ticket, please praise this article, it will be the strongest motivation for me to write more high-quality articles.

If you feel that the article is a little help to you, please wechat search “Silent King two” for the first time to read, reply [666] [1024] more I prepared for you 500G HD teaching video (has been classified), as well as dfactory technology niu people finishing the face by a copy, the source of this article has been included in the code cloud, portal ~