Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often find myself writing methods in languages such as Java or C++ that are only meant to loop over an array or something similar, accumulate the values, and then return the total. The problem is, it usually takes at least 4 lines (1 for init, 2 for loop, 1 for return), which feels much too bloated for such a common and simple operation.

How can I generically condense accumulation code in a way that's similar to the extremely compact for loop?

Bloated Code Examples (in java):

    int totalDays = 0;
    for (int month= ymd[1] -1; month >0; month--) {
        totalDays += daysInMonth(month);
    return totalDays; 

    for (int month= ymd[1] -1, totalDays= 0;; month--) {
        totalDays += daysInMonth(month);
        if (month == 1) return totalDays;
share|improve this question

closed as off-topic by Jamal Apr 18 '15 at 18:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

To refer to Java's standard writing : put totalDays in the for(..) like in the second sample. The second sample add an if which looks like a doublon with the ;; (if nested and parsed in the for, even if omitted) – cl-r Dec 3 '12 at 13:40
C++/Java/C# are verbose that way. It does not make it a bad thing. Readability of code that will be read by many thousands of person hours is important. If this is just a piece of throw away code then readability is not such an issue then a more compact language may by appropriate. – Loki Astari Dec 3 '12 at 16:47
it doesn't matter whether it takes 3 or 10 lines as long as it is optimized by the compiler. prefer clarity before compactness. – Anders K. Dec 3 '12 at 19:11
@cl-r I'm not sure what you're saying in your second sentence; could you elaborate? – Griffin Dec 3 '12 at 23:05
@AndersK I'd say I'm going more for conciseness than compactness, which definitely enhance readability ^_^ – Griffin Dec 3 '12 at 23:06

In the case of C++, use the standard library and boost. You want a range from ymd[1]-1 to 0, and you want to accumulate over it. Seeing as the order doesn't matter, it's easier to reverse it.

boost::accumulate(boost::irange(1, ymd[1]), 0, [](int i, int j) { return i + daysInMonth(j); });

As you can see, this isn't much better than a for loop. If you find yourself writing these kinds of algorithms often, then perhaps defining some helper range classes to get rid of some of the boilerplate would be nice. (I suspect there's a transform algorithm that has the new range as its return value, but I'm not familiar with it.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.