Effective Java 3rd Edition Chapter 8 Methods

[Effective Java 3rd Edition](https://www.amazon.co.jp/Effective-Java-%E7%AC%AC3%E7%89%88-%E3%], which is a must-have book for intermediate Java users and above. 82% B8% E3% 83% A7% E3% 82% B7% E3% 83% A5% E3% 82% A2% E3% 83% BB% E3% 83% 96% E3% 83% AD% E3% 83% 83% E3% 82% AF-ebook / dp / B07RHX1K53) has a Kindle version, so I will summarize it.

Previous: Effective Java 3rd Edition Chapter 7 Lambda and Stream Next: Effective Java 3rd Edition Chapter 9 General Program

Item 49 Check the validity of parameters

--For public and protected methods, use the Javadoc @ throws tag to document the exception that is thrown if the parameter value constraints are not adhered to. Generally the exceptions are ʻIllegalArgumentException, ʻIndexOutOfBoundsException, NullPointerException. --The ʻObjects.requireNonNull` method added in Java7 is flexible and convenient, so you don't have to do null checking manually.

//Throw a NullPointerException if strategy is null
this.strategy = Objects.requireNonNull(strategy, "strategy must not be null.")

--In Java 9, a range checking mechanism has been added to java.util.Objects. It consists of three methods, checkFromIndexSize, checkFromToIndex, and checkIndex, and checks whether the index of the list and array is within the range. --Methods that are not public should use assertions to check their parameters.

Item 50 Defensive copy if necessary

--When a mutable object like the Date class comes in as a parameter, it is important to copy it defensively.

Item 51: Carefully design the signature of the method

--Choose the method name carefully. Avoid long method names. --Do not provide too many useful methods. Individual methods should "play their own role", and many methods make testing and maintenance difficult. --Avoid long list of parameters. Aim for 4 or less. It is harmful to have multiple parameters of the same type. There are three techniques for shortening the list of overly long parameters: --Split the methods so that each method requires only a subset of the parameters. --Create a helper class that holds a collection of parameters. --Apply the Builder pattern from object creation to method invocation. --For the parameter type, select the interface rather than the class. If an appropriate interface exists to define the parameters, use that interface rather than the class that implements it. (Example: HashMap → Map) --If the meaning of boolean is not clear from the method name, use an enum type that has two elements rather than boolean.

Item 52 Use with caution overload

--The overload chooses which method to call at compile time. In a Collection instance, if the content is ʻArrayList, ʻArrayList cannot successfully call the argument method. --A safe and conservative policy does not provide two overloaded methods with the same number of parameters. Instead of overloading, give the method a different name.

Item 53: Use variable length arguments with caution

--The problem is that the variable length argument method can only be checked at runtime when one or more arguments are required. --Variadic arguments are placed after the required parameters.

Item 54 Returns an empty collection or an empty array instead of null

--Do not return null instead of empty collections or arrays. --Null makes the API difficult to use and has no performance benefits.

Item 55: Carefully return the option

--There are three ways to represent an object that has no value. --null (danger of NullPointerException) --Exception (high cost) --Optional (from java8) --ʻOptional.empty () empty. Optional that contains value in ʻOptional.of (value) . --ʻOptional.ofNullable (value) determines and returns one of the above depending on whether value is null or not. --Methods that return Optional must not return null. --Returning an option that includes a boxed basic data type is expensive because you do two levels of boxing. For that purpose, the basic data types ʻOptionalInt, ʻOptionalLong, and ʻOptionalDouble are prepared. --It should be rare to use Optional other than the return value.

Item 56 Write document comments for all public API elements

--Document comments must be written before all published classes, interfaces, constructors, methods and field declarations. --Document comments about methods should briefly describe the contract between the method and its clients. --List all preconditions, postconditions, and side effects of the method. --The @ param, @ return, and @ throws tags should be written. --Document comments are converted to HTML, so you can use HTML tags. --When designing a class for inheritance, the self-use pattern must be documented. For that, use @implSpec. -HTML metacharacters (<``>``&Etc.) when using@literalEnclose in tags. Example){@literal |r| < 1}

Recommended Posts

Effective Java 3rd Edition Chapter 8 Methods
Effective Java 3rd Edition Chapter 5 Generics
Effective Java 3rd Edition Chapter 9 Program General
Effective Java 3rd Edition Chapter 3 Methods Common to All Objects
Effective Java 3rd Edition Chapter 6 enums and annotations
Effective Java 3rd Edition Chapter 7 Lambda and Streams
Effective Java Chapter 2
Effective Java Chapter 6 34-35
Effective Java 3rd Edition Chapter 2 Object Creation and Disappearance
Effective Java Chapter 4 15-22
Effective Java Chapter 3
effective java 3rd summary
Effective Java 3rd Edition Section 85 Choose Alternatives Over Java Serialization
Java methods
Java methods
Java class methods
[Read Effective Java] Chapter 2 Item 1 "Consider static factory methods instead of constructors"
[Read Effective Java] Chapter 3 Item 10 "Always Override toString"
[Learning Memo] Metaprogramming Ruby 2nd Edition: Chapter 3: Methods
Java Performance Chapter 1 Introduction
I tried to explain Effective Java 3rd edition "almost all chapters" in "easy-to-read Japanese".
[Read Effective Java] Chapter 2 Item 6 "Remove obsolete object references"
Builder pattern (Effective Java)
Updates from Effective Java Third Edition 2nd Edition Personal Notes
Java Performance Chapter 3 Java Performance Toolbox
I will write an excerpt of what I found interesting while reading Effective Java 3rd Edition
I started Java Gold (Chapter 1-1)
Modern Java Development Guide (2018 Edition)
Personal comment criteria [Java edition]
(Note) Java classes / variables / methods
[Java] Generics classes and generics methods
Java Performance Chapter 2 Performance Testing Approach
Check Java9 Interface private methods
Java methods and method overloads
About Java class variables class methods
From Ineffective Java to Effective Java
Java abstract methods and classes
[Read Effective Java] Chapter 2 Item 4 "Force uninstantiation with a private constructor"
[Read Effective Java] Chapter 3 Item 9 "When overriding equals, always override hashCode"
[Read Effective Java] Chapter 2 Item 5 "Avoid the creation of unnecessary objects"