We all know that this book is the most important book for writing efficient and maintainable Java code. Although Android is written in Java, the advice in the book doesn’t necessarily apply. Some even argue that most of the recommendations don’t apply to Android development. In my opinion, some Java optimizations such as Enums, serialization, etc., do not work well in Android due to the differences between the virtual machines (Dalvik/ART) used in Android and traditional JVMS. Of course, many of the recommendations in the book, with some minor modifications, can help us develop more robust, clean, and maintainable code.
In this article, I’ll take a look at some of the most important points in the book that I think apply to Android development. Review it for those of you who have read it, and study it for those who haven’t.
Force cannot generate instance
If you don’t want to use the new keyword to create objects, you can make access to constructors private, especially for utility classes that contain only static methods.
Static factory
Use the static factory method insteadnew
The keyword creates the object, and the factory method can return different subclass objects with different names as needed, without having to create a new object every time if necessary.
[Update] A reader suggested that using a static factory is not convenient for testing, if so, you can simulate it during the testing period by using a non-static factory.
Builders
When you have more than three parameters in a constructor, consider using a Builder to build objects. It can be a bit cumbersome, but it’s easier to expand and more readable. To create a value class, use AutoValue
Avoid variable
Immutable means that an object remains the same value throughout its life, with all required values set at creation time. This has many benefits, such as thread safety and shareability.
However, not all objects are immutable, so try to do so (such as defining private final member variables, final classes).
Static inner class
If you create an inner class without relying on an external class, you must define it as a static class; otherwise, the instance of the inner class will hold a reference to the instance of the external class.
Use generic
We should thank Java for its type-safety (as opposed to JS) and try to be as type-safe as possible at compile time:
Don’t forget to use generics for method arguments and return values:
You can also use restricted wildcards to restrict the upper and lower bounds of a type:
Returns a null value
If a method returns a value of type List/collection, instead of returning null, an empty collection can be returned:
Do not use the “+” operation String
Consider using “+” when there are only a few strings, and use StringBuilder when there are many:
Abnormal return
I do not support throwing exceptions to indicate errors. If you want to do this, make sure your program can recover from exceptions:
conclusion
The article doesn’t contain all the advice in the book, just a memo of some of the more practical tips.
Effective Java for Android (Cheatsheet)
Please pay attention to the public account Wutongke, and push the cutting-edge technology articles of mobile development every day: