From Ineffective Java to Effective Java

The world where Effective Java does not exist, that is Ineffective Java

Recently, when developing in Java, I think that applications are often built using frameworks such as Spring Boot. Spring Boot is really good. There are many great mechanisms such as DI and AutoConfigure to reduce the amount of code written.

Programmers who are familiar with Java will often design and implement around the framework as architects. Create an architecture that is easy to mass-produce by using template method patterns and so on. After that, by touching the programmer who implements each function, the screen and business processing will be created according to the framework.

However, business processing is of course necessary. ** How about the quality of such a large amount of code written in parallel at your site? ** ** I had a problem around that, and I thought that ** this requires an engineer to be aware of it correctly at the development site **, so I started Java education for the company. This article summarizes what I noticed in the code review during the education, ** "Oh, I don't understand this" **. I hope that the managers who often ask "Why are you writing this kind of fucking code?", Which is often the case when you find a bug, will realize the importance of education and reviews. Perhaps those workplaces have books such as Effective Java / Readable Code, or are they dusty? ** Education is very important. ** ** I use HashMap for everything, and the reason I spit out logs in logic is because I don't know that.

What is Java education?

2 cools held

--June-September 2018 26 people once a week 16 times in total --November 2018-February 2019 15 people, once a week, 8 times in total

It's like giving a weekly lecture (2 hours), giving exercises, getting a pull request, reviewing and returning. I followed on Slack during the lecture. As for the teaching materials, we purchased and implemented the teaching materials of Mr. Casa Real. It is more expensive than buying a general book, but it is very good that the order of explanation is well organized. I don't think you need a textbook if you can always be a pair pro, but I think that I was addicted to in-house education like this one.

The target person is such a person

――First year when I started writing Java in business ――Actually, I'm writing Java, but it's about 3-4 years ――It's been 10 years since I switched from another language to Java.

Then, I will introduce all the contents that I saw in the pull request and that I should know. The ranking is my feeling. If you want to know this, please ask Newbie around you. There are probably around you too. ** I don't know what I haven't learned **.

Ineffective Java

Item 1 What is toString ()?

I was surprised that many people didn't know the role of this method. Have you ever seen the code ʻobject.getXXX () + object.getYYY ()` even though toString () is already implemented in the object?

This was behind the scenes where I didn't know the meaning of toString (). Please ask the young people around you.

Item 2 The declaration of the type on the left side is always the same as the type on the right side.

ArrayList<String> list = new ArrayList<>();

Of course, the left side should be List <String>. However, if there is no consideration of interface or polymorphism, the left side will be declared with the same type as the right side. For people in such a state, ** OCP ** will be a mess, so it is necessary to explain step by step in the order of inheritance-> interface-> collection-> polymorphism. With this alone, you will be able to correctly think and define the left side. (I don't know if the terms left and right are correct)

Item 3 Convert to wrapper anyway

Long.valueOf(Files.size(pathA)).equals(Long.valueOf(Files.size(pathB)));

I think it was copied somewhere. Or, making it a rapper may give you a sense of security. You need to understand correctly to treat primitives as primitives. By understanding this, you will gradually learn that it is a bad idea to create objects in vain.

Item 4 Unnecessary comments

You've seen this code, haven't you? ʻEclipse auto-generated comments remain, variable explanations and other comments` In the former case, I was too desperate to write the code in the first place, and I couldn't see the comments. The latter is an example of growing up after receiving only the education that comments are important.

I instantly realize that I'm doing pair pros, but if I don't, I won't learn from anyone, so it will continue forever. Let me tell you correctly. ** Basically, no comment required. It's necessary if you want to do something irregular in your business. Otherwise, a bug may appear based on the comment in the later maintenance phase **.

Item 5 I don't need an if statement here

public boolean isLessThan1KB(String file) throws Exception {  
        //Path object creation
        Path path = Paths.get(file);  
        if (Files.size(path) < 1024) {  
            return true;  
        } else {  
            return false;  
        }  
    }

This is a lot. After the if statement, it is rewritten with return Files.size (path) <1024;. If you think that the code you wrote once will end if it works, there are many ways to write it like this. In order to feel something redundant, a code review of a superior is essential.

Item 6 I don't see the return value for some reason

    public void delLine(String line) { 
        lines.remove(lines.indexOf(line)); 
    }

The List #remove method has a return value. In some cases, the return value is ignored and used as is. There may be such a way of writing this somewhere on the net. Behind the scenes, beginners are often not good at handling errors. If you understand ** exceptions, error handling **, etc., they will be resolved naturally.

Item 7 Not familiar with try-with-resources

Even now, there are many people who say that the JDK in the field is 6. How long will I continue to write in the old way? This would be the same situation for all for statements, Streams, lambdas, etc., regardless of twr. We recommend that you study Java 8 properly. ** For newcomers starting with Java 11, these are only a threat. ** **

Item 8 Want to nest

if(xxx == true) {
 //processing
} else {
 //processing
}

A serious programmer who always writes like this. I want to tell you that there is a guard clause in the world. Most of the time, using the guard clause makes it even more refreshing. ** One of the causes is that the code is full of nests and is usually covered. Recognize. You can't tell the difference even if you reduce the number of nests by one **, but if you review it properly, you can understand it in one shot, so the immediate effect is extremely high.

Item 9 I don't understand the meaning of equals ()

This is very difficult in the first place. Even intermediate Java users may find it difficult to explain at once. However, if I don't understand this, I'm scared and I can't use TreeMap. Because I don't know if any object will be sorted correctly. So, ** I always become a HashMap bastard, get () and then knead. ** ** When you implement equals (), you naturally implement hashCode () as well. If you don't, a lot of strange things will happen. If you can understand this correctly, your code will be more sophisticated than this understanding. This is exactly what Effective Java is.

Item 10 Immutable? ??

I don't know immutable objects. This is very important knowledge to go from beginner to intermediate. The ability to infer the state of an object in order to eradicate the hidden bugs. I think this is an intermediate Java user. How can I create an immutable object? When do you use it? ** If you know that, the range of class design will be greatly expanded. On the contrary, if you do not do this, you can only design a uniform design forever. ** **

Also, although it is not directly related to immutables, there are cases where it is not possible to correctly explain what final means even though it is a List such as final List <String> stringList;, so it is extremely important to understand this correctly as well.

From Ineffective Java to a world with Effective Java

There's been a lot of content like a bridge from beginner to intermediate, but many of these can grow by reading Effective Java. Ask beginners what intermediates don't understand. If you are an intermediate person, invite beginners to hold a reading session. Effective Java 3rd Edition

This is recommended if you want to relearn Java 8 Java SE 8 Practical Programming that Java Programmers Should Learn

Recommended Posts

From Ineffective Java to Effective Java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
The road from JavaScript to Java
[Java] Conversion from array to List
Effective Java Chapter 2
Effective Java Chapter 6 34-35
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
[Java] Introduction to Java
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
Introduction to monitoring from Java Touching Prometheus
Introduction to java
Precautions when migrating from VB6.0 to JAVA
Effective Java Chapter 4 15-22
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Effective Java Chapter 3
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
Migrate from Java to Server Side Kotlin + Spring-boot
How to get Class from Element in Java
I want to write quickly from java to sqlite
Minecraft BE server development from PHP to Java
Select * from Java SDK to Azure Cosmos DB
Migrate from JUnit 4 to JUnit 5
Eval Java source from Java
[Java] Connect to MySQL
Access API.AI from Java
Kotlin's improvements to Java
Introduction to java command
Launch Docker from Java to convert Office documents to PDF
Convert Java enum enums and JSON to and from Jackson
[Java] I want to calculate the difference from the date
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
6 features I missed after returning to Java from Scala
Java development for beginners to start from 1-Vol.1-eclipse setup
How to change from Oracle Java 8 to Adopt Open JDK 9
[Java] How to erase a specific character from a character string
Generate models from JSON to Swift, PHP, C #, JAVA
Updates from Effective Java Third Edition 2nd Edition Personal Notes
Moved from iBATIS to MyBatis3
[Java] How to use Map
Try Spring Boot from 0 to 100.
Introduction to Effective java by practicing and learning (Builder pattern)
Effective Java 3rd Edition Chapter 3 Methods Common to All Objects
How to lower java version
[Java] How to use Map
Convert Java Powerpoint to XPS