Rule for Prefer of toArray(new T[0]) to toArray(new T[size])

According to https://shipilev.net/blog/2016/arrays-wisdom-ancients/ toArray(new T[0]) should be preferred to toArray(new T[size]).

If you agree, can you make a rule for this?

I think this question should be moved to the New Rules category.

1 Like

I see the New rules tag. Anything else that needs to be done?

Hello @vogella

Anything else that needs to be done?

Looks good, no more action needed on your side.

Thanks for the proposition and the interesting blog.

When talking about performance, we are always extra careful. The blog itself shows us examples of what could go wrong: PMD and IntelliJ implemented a rule for this and ended up misleading users since their claim is no longer (or never was) true. For the story, a similar problem happened to us recently with S3027, we realized that our claim was outdated and no longer true.

Performance suggestions are funny things. Their shelf life is limited, especially if you […] don’t bother to track how the world evolves.

To me, this extract from the article sum-up well why I’m reluctant to go further. The analyzer has more than 600 rules, it is impossible to track such things, I would rather avoid creating a rule that is not designed to last.

Of course, we are always eager to discuss the subject further if needed.

1 Like