nus-cs2113-AY2324S2 / forum

2 stars 0 forks source link

Type Inference when instantiating generic classes #18

Closed YHWong20 closed 4 months ago

YHWong20 commented 4 months ago

Hi, I'd like to ask if type inference is considered an acceptable coding practice (when instantiating generic classes).

Normally, we would create new instances of generic classes as such: List<Integer> newList = new ArrayList<Integer>(); // No type inference

However, we can use type inference here: List<Integer> newList = new ArrayList<>(); // Type inference used

I can't seem to find any guidance regarding this (on both the provided style guide, as well as the Google style guide). I would appreciate some insight regarding this. Thanks!

PS: IntelliJ's linter seems to recommend using type inference. But I was also taught previously that we should try to avoid type inference when possible.

okkhoy commented 4 months ago

List<Integer> newList = new ArrayList<>(); is acceptable.

See this link for some explanation.

YHWong20 commented 4 months ago

Understood, thanks Prof!