views:

38

answers:

3

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?

+2  A: 

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);
}
rsp
+2  A: 

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.

Dean J
Brackets in loops don't just clarify, they can prevent errors. I try to always make loops and ifs explicit.
Jeremy Roberts
+1  A: 

Why not simply:

List<IssueType> selectedIssueType = Arrays.asList(selectedItems);
tylermac
Thanks for the suggestion! But this question is generally about style. I'm reviewing projects code and try to make it better. I've already found many really 'smelly' places but this one made me think.
Roman
Ah, I see now. I guess I didn't really understand the question.
tylermac