开发者

Code formatting for initializing lists

开发者 https://www.devze.com 2022-12-29 23:21 出处:网络
I\'ve just found in my java project this code snippet: List<IssueType> selectedIssueTypes = new ArrayList<IssueType>();

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消