Idea 7 tags

public Collection toCollection() {
ArrayList c = new ArrayList();
Collection copyList = c;
return copyList;

toCollection() method, c and copyList are geting colored and when I come onto them, they show:
Required tag '@return' is missing more..... (Ctrl+F1)
Local varaible c is redundant more...
Local varaible copyList is redundant more...

Can I turn them off or do I really need them?

Dave

1 comment
Comment actions Permalink

While you can certain turn off those inspections, or suppress them for the method you have, I -- and most others here -- would recommend following the advice the inspection offers. If you place your cursor at the highlighted warning (you can use F2 and shiftF2 to navigate to the next and previous error if desired), then type altenter, IDEA will offer the suggestion of "inline variable" to fix the items. If you select that fix for both warnings, the code becomes:



The resultant code is much cleaner, simpler to read, and clearer to what is happening. Yet it does the exact same thing the original code did. As the inspection was stating, the declaring of the variables was redundant. Again, you could turn the inspection off (altenter, right-arrow,) or suppress it for the statement, method or class (also altenter, right-arrow) . But I would strongly recommend against it. And I think most people here would as well. I can honestly say that when I started using IDEA five years ago, those inspections helped me to become a better developer. They still do today. For example, if you do not understand why the code above is the same as the original code, do some reading up on polymorphism to learn why. Hitting ctrl+F1 twice when at a highlighted error or warning will give some detail as to why the inspection is there and what it is warning against.

Some inspections and the things they warn against might be more stylist choses. But most are more about preventing errors, bugs, and developing cleaner code. My recommendation is to not turn off or suppress an inspection unless you are able to make a strong and valid argument as to why you are doing such. For example, years go when I started to see people write "".equals(myVariable) or "some value".equals(myVariable) rather than myVariable.equals("") or myVariable.equals("some value") I thought it was purely a choice of coding style. But IDEA's inspection taught me that using the former rather than the latter will prevent null pointer exceptions. (And that was only after I turned on some additional inspections since that one is not on by default.) Both are syntactically correct. But one is less error prone and prevents bugs. And that is how IDEA can help you become a better developer. It's an on the fly code review.

0

Please sign in to leave a comment.