我有一个看起来很棒的功能,我想要清理它。它包含的是一些map,for循环和if语句。
基本上我有一张地图,我只想从中获取某些信息,但我需要的其中一个键在每张地图中都会改变一个数字。
我在想也许是一个简单的switch语句或者应该修复它的东西,但我经常对简化这些事情感到困惑。
这是函数的样子:
public void separateBooksFromList() {
book1 = [:] //map for book1
book2 = [:] //map for book2
book3 = [:] //map for book3
book4 = [:] //map for book4
book5 = [:] //map for book5
book6 = [:] //map for book6
book7 = [:] //map for book7
book8 = [:] //map for book8
book9 = [:] //map for book9
book10 = [:] //map for book10
lastModified = new Date(dataFile.lastModified()) //last time the file was scanned
readDate = new Date() //current date the text file was read
for(int i = 0; i < bookList.size(); i++) {
if(i==0) {
book1['lastScan'] = lastModified
book1['readDate'] = readDate
book1['bookNumber'] = bookList['Book Number 0'][i] // <- only part of the map that changes
book1['bookTitle'] = bookList['Book Title'][i]
}
if(i==1) {
book2['lastScan'] = lastModified
book2['readDate'] = readDate
book2['bookNumber'] = bookList['Book Number 1'][i] // <- only part of the map that changes
book2['bookTitle'] = bookList['Book Title'][i]
}
if(i==2) {
book3['lastScan'] = lastModified
book3['readDate'] = readDate
book3['bookNumber'] = bookList['Book Number 2'][i] // <- only part of the map that changes
book3['bookTitle'] = bookList['Book Title'][i]
}
if(i==3) {
book4['lastScan'] = lastModified
book4['readDate'] = readDate
book4['bookNumber'] = bookList['Book Number 3'][i] // <- only part of the map that changes
book4['bookTitle'] = bookList['Book Title'][i]
}
if(i==4) {
book5['lastScan'] = lastModified
book5['readDate'] = readDate
book5['bookNumber'] = bookList['Book Number 4'][i] // <- only part of the map that changes
book5['bookTitle'] = bookList['Book Title'][i]
}
if(i==5) {
book6['lastScan'] = lastModified
book6['readDate'] = readDate
book6['bookNumber'] = bookList['Book Number 5'][i] // <- only part of the map that changes
book6['bookTitle'] = bookList['Book Title'][i]
}
if(i==6) {
book7['lastScan'] = lastModified
book7['readDate'] = readDate
book7['bookNumber'] = bookList['Book Number 6'][i] // <- only part of the map that changes
book7['bookTitle'] = bookList['Book Title'][i]
}
if(i==7) {
book8['lastScan'] = lastModified
book8['readDate'] = readDate
book8['bookNumber'] = bookList['Book Number 7'][i] // <- only part of the map that changes
book8['bookTitle'] = bookList['Book Title'][i]
}
if(i==8) {
book9['lastScan'] = lastModified
book9['readDate'] = readDate
book9['bookNumber'] = bookList['Book Number 8'][i] // <- only part of the map that changes
book9['bookTitle'] = bookList['Book Title'][i]
}
if(i==9) {
book10['lastScan'] = lastModified
book10['readDate'] = readDate
book10['bookNumber'] = bookList['Book Number 9'][i] // <- only part of the map that changes
book10['bookTitle'] = bookList['Book Title'][i]
}
}
}
正如你所看到的,这是一个非常丑陋的功能。
我能够做一个简单的切换声明或其他东西来减少代码并让它看起来更专业吗?
答案 0 :(得分:2)
你可以在这做一些事情。你可以使用书籍清单而不是book1,book2等......这样可以节省很多重复。
这几乎是相同的,但随着这种变化。它将创建一个大小为10的列表(假设有10本书),其中的地图条目就像您之前的那样。
public void separateBooksFromList() {
lastModified = new Date(dataFile.lastModified()) //last time the file was scanned
readDate = new Date() //current date the text file was read
// Use a list of Maps instead of separate variables
int numberOfBooks = bookList.size()
def books = []
numberOfBooks.times {
books[it] = [
lastScan: lastModified,
readDate: readDate,
bookNumber: booklist["Book Number $it"][it],
bookTitle: booklist["BookTitle"][it]
]
}
}