patternrubyMinor
Get individual item count
Viewed 0 times
itemcountgetindividual
Problem
get_item_count calls to an API and gets a count. There are several pages to get this count from until there are no more results.How do I improve this code to remove all of the extra variables and make it easier to read?
final_count = 0
page = 1
final_count = individual_count = get_item_count(config, page)
while individual_count > 20
page = page + 1
individual_count = get_item_count(config,page)
final_count = individual_count + final_count
endSolution
Use a do-while loop, so the loop body gets always executed at least once:
final_count = 0
page = 1
begin
individual_count = get_item_count(config, page)
final_count += individual_count
page += 1
end while individual_count > 20Code Snippets
final_count = 0
page = 1
begin
individual_count = get_item_count(config, page)
final_count += individual_count
page += 1
end while individual_count > 20Context
StackExchange Code Review Q#31453, answer score: 2
Revisions (0)
No revisions yet.