GithubHelp home page GithubHelp logo

Comments (6)

kjlubick avatar kjlubick commented on June 9, 2024

I say go for it!

from fb-contrib.

mebigfatguy avatar mebigfatguy commented on June 9, 2024

I would suspect the reason they are doing this is because the List
returned from Arrays.asList is immutable, and they want to be able to
add or remove items or such. (unless you are saying you can determine
that the code will do no such thing, then that's another story.



On 2015-03-24 16:00, Trevor Pounds wrote:

I noticed the following anti-pattern in some service code the other day. Arrays.asList(...) already creates a copy of the original array so nesting it inside an additional ArrayList is unnecessary.

final List<...> valueList = new ArrayList<...>(Arrays.<...>asList(valueArray));

I'll take a crack at adding new detector for this bug but wanted to report it in case anyone wanted to get to it beforehand.

Reply to this email directly or view it on GitHub [1].

Links:

[1] #37

from fb-contrib.

tpounds avatar tpounds commented on June 9, 2024

@mebigfatguy Looks like you are correct. I noticed after posting the issue that we were removing elements from the newly constructed list elsewhere. I think this idea still has some merit in the scenario where it is being constructed as above but never being mutated. However, the detector would require some additional escape and usage analysis on the object. Thoughts?

from fb-contrib.

mebigfatguy avatar mebigfatguy commented on June 9, 2024

@tpounds One easy thing would be to see if Guava, (or perhaps some other collections library) was on the class path, and if so, recommend using

List<?> valueList = Lists.newArrayList(valueArray);

instead.

I'd think determining if an ArrayList is used mutably would be kind of rough to do. As it's highly likely that that collection is going to be passed as a parameter somewhere. I suppose you could look for local-only use of the collection and report against that.

from fb-contrib.

ThrawnCA avatar ThrawnCA commented on June 9, 2024

ArrayList really should have a constructor that accepts an array. But that's for Oracle to fix...

from fb-contrib.

mebigfatguy avatar mebigfatguy commented on June 9, 2024

Guava is useful here.

from fb-contrib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.