///
Search
4️⃣

복잡하고 읽기 어려운 코드

해당 코드는 엑셀의 특정 컬럼을 찾기 위해 만든 코드. 하지만 if, 3중 for 문이 사용되어 상당히 읽기 어려운 코드. 해당 코드를 어떤 방식으로 수정해야 할지 고민함.
private List<ColumnFrame> findHeaderIndex(List<ColumnFrame> columnFrames) { List<ColumnFrame> newList = new ArrayList<>(); int maxRow = Math.min(this.sheet.getLastRowNum(), 100); int size = columnFrames.size(); // size-- 해서 0이되면 종료 for (short i = 0; i < maxRow && size > 0; i++) { Row row = this.sheet.getRow(i); if (row == null) continue; for (Cell next : row) { if (next == null || next.getCellType() != CellType.STRING) { continue; } String headerName = next.getStringCellValue(); if (headerName == null) { continue; } for (ColumnFrame f : columnFrames) { if (f.getImportNameOptions().contains(headerName.trim())) { ColumnFrame importOnlyColumnFrame = f.createImportOnlyColumnFrame(next.getRowIndex(), next.getColumnIndex()); newList.add(importOnlyColumnFrame); size--; break; } } } } return newList; }
Java
복사
수정전
수정 후