Code formatting for initializing lists
I've just found in my java project this code snippet:
List<IssueType> selectedIssueTypes = new ArrayList<IssueType>();
for (Object item : selectedItems)
selectedIssueTypes.add((IssueType) item);
开发者_如何转开发
How do you think, can this style be used?
Imho, this style seems to show a hierarchy that isn't there. Also I would advocate to always use braces and I like to separate declarations and code with an empty line, so I would use:
List<IssueType> selectedIssueTypes = new ArrayList<IssueType>();
for (Object item : selectedItems) {
selectedIssueTypes.add((IssueType) item);
}
I think this would be much clearer and less prone to problems when another developer looks at it.
List<IssueType> selectedIssueTypes = new ArrayList<IssueType>();
for (Object item : selectedItems) {
selectedIssueTypes.add((IssueType) item);
}
Always use brackets to clarify loops. Do not indent the for statement, as it's against standard practice.
Why not simply:
List<IssueType> selectedIssueType = Arrays.asList(selectedItems);
精彩评论