Java's Angry Code 5 Selections and Amendments

Introduction

In this article, I'll publish the code that was offended in Java. ・ Too long sideways -Stream with too much processing -Meaningless wrapper class ・ Return is uselessly slow

I will write these four points. I picked up from grammars that exist in various languages, not limited to Java.

① It is too long sideways.

It's a code that tends to be done when you feel like you can do it a little. One example is the useless use of ternary operators.

BatCodes.java


    /**
     * 
     *Angry code ①: Too long sideways
     *Anger: ★★★ ☆☆
     *Angry point: Max is hard to read
     */
    private boolean checkBadCode(String str) {
        return str.equals("abc") ? true : str.endsWith("c") ? true : str.startsWith("a") ? true : false ;
    }

In such a case, use the if else statement obediently.

GoodCodes.java


    /**
     *Angry code ① Correction plan
     */
    private boolean checkGoodCode(String str) {
        if(str.equals("abc")) {
            return true;
        }
        if (str.startsWith("a") || str.endsWith("c")) {
            return true;
        }
 
        return false;
    }

** The ternary operator is convenient, but it is highly recommended to follow my rules or coding conventions. ** ** By the way, I use it when the number of characters in the code does not exceed 70 and the judgment is not performed more than once.

GoodCodes.java


    private void checkBadCode(String str) {
        //This is easy to read
        str = str.equals("abc") ? str.concat("def") : str
    }

(2) Stream with too much processing

For example, forEach ()

BatCodes.java


    /**
     *Angry code (2): Stream with too much processing
     *Anger: ★★★ ☆☆
     *Angry point: Anything for Each()Is not an excuse if you use
     */
    private void streamBadCode(List<String> targetList) {
        targetList.stream().forEach((String str) -> 
                                    {str.concat("a");
                                     if(str.startsWith("a") ){
                                        System.out.print(str);
                                     };});
    }

Make sure to choose a suitable intermediate operation. Mostly prepared. In the above example, you can also use the extended for statement.

GoodCodes.java


     /**
     *Angry code ② Correction plan
     */
    private void streamBadCode(List<String> targetList) {
        targetList.stream().map(s -> s.concat("a")).filter(s -> s.startsWith("a")).forEach(System.out:: print);
    }

GoodCodes.java


     /**
     *Angry code ② Correction plan
     */
    private void streamBadCode(List<String> targetList) {
        for (String target : targetList) {
            target.concat("a");
            if(target.startsWith("a")) {
                System.out.print("a");
            }
        }
    }

~~ By the way, if you work in a workplace that never allows the use of Stream, you should consider retirement. ~~

③ meaningless wrapper class

I did this when I got used to it. Anyway, it's processed by AutoBoxing, and should I declare it in the wrapper class from the beginning? That's a shallow idea.

BatCodes.java


    /**
     *Angry code ③: meaningless wrapper class
     *Anger: ★★★★ ☆
     *Angry point: Wasted memory consumption
     */
    private Integer literalBadCode(Integer i) {
        Integer firstValue = 10;
        Integer secondValue = 20;
        Integer sumValue = i + firstValue + secondValue;
        return sumValue;
    }

The wrapper class uses about four times as much memory as a primitive variable, so It can cause a memory leak. Use it only when you need it. With the above code. You can return it directly with return, though ...

GoodCodes.java


    /**
     *Angry code ③: meaningless wrapper class
     *Anger: ★★★★ ☆
     *Angry point: Wasted memory consumption
     */
    private int literalBadCode(int i) {
        return  i + firstValue + secondValue;
    }

⑤ Return is uselessly slow

The return clause is the code when I thought it was cool to write it at the end of the method. ** That's a mistake. ** **

BatCodes.java


    private boolean checkBatCode2(String target) {
        boolean japanFlg = false;
        boolean englishflg = false;
        boolean nonflg = true;
        switch(target) {
            case "Hello" :
                englishflg = true;
                break;
            case "Hello" : 
                japanFlg = true;
                break;
            default :
                nonflg = false;
                break;
        }

        if(japanFlg) {
            return japanFlg;
        } else if (englishflg) {
            return englishflg;
        } else {
            return nonflg;
        }

    }

It's too long and I don't feel like reading the process, so let's return immediately when the result is confirmed. Doing so keeps your code short and concise so you don't have to embed unnecessary bugs or annoy people to read later.

BatCodes.java


public class GoodCodes {
    public static final String LANGEAGE_ENG = "Hello";
    public static final String LANGEAGE_JP = "Hello";

    /**
     *Angry code ④ Correction plan
     */
    private boolean checkGoodCode2(String target) {
        if(target.equals(LANGEAGE_ENG) || target.equals(LANGEAGE_JP)) {
            return true;
        }
        return false;
    }

At the end

This time, I picked up some things that I did in the past and some that flew in the code review. There are many similar articles that are of better quality than this one, but I think some are quite rare.

※Source code https://github.com/mamoru12150927/JavaQitta.git

Recommended Posts

Java's Angry Code 5 Selections and Amendments
Spring validation and error code