0

I've found the following piece of code in java.util.Calendar:

public final void clear()
{
    for (int i = 0; i < fields.length; ) {
        stamp[i] = fields[i] = 0; // UNSET == 0
        isSet[i++] = false;
    }
    areAllFieldsSet = areFieldsSet = false;
    isTimeSet = false;
}

I understand what it's doing and why it's working. But missing the increment and doing it at the end of the loop, it clearly differs from the "normal" for pattern (as described in http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html)

for (initialization; termination; increment) {
    statement(s)
}

Is there any advantage in the approach I've shown above?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
stuXnet
  • 4,309
  • 3
  • 22
  • 31
  • 3
    No, and God kills a kitten every time you do it in the way you showed. – Dennis Meng Dec 03 '13 at 19:02
  • 1
    @Nambari The given code is equivalent to putting the `i++` in the for itself. – user2864740 Dec 03 '13 at 19:04
  • 2
    It is left in there to highlight how awful java.util.Date and Calendar are, a bit like Sex Offender Registry, but for code. – Taylor Dec 03 '13 at 19:05
  • I agree with you and the killed kittens. But why would someone put something like that in the JDK? – stuXnet Dec 03 '13 at 19:05
  • @stuXnet Why is it so bad? Code is code. Code is art (and not all art is "good", or good to always people). It doesn't have to be the same. I'll sometimes write my for loops with the increment inside, if I think it makes the code more clear. Perhaps it is residue from different code where it made a difference .. anyway, closing as Opinion-based. – user2864740 Dec 03 '13 at 19:06
  • 1
    @user2864740 Hey to each their own, I think that programming is indeed art (taking an idea from inside your mind and using a structured language to express it), but not code itself, which should be clean and readable. We follow conventions for a reason. I guess if it compiles then it's technically acceptable. But not really nice if you have to pass your code over to someone else. – PaulG Dec 03 '13 at 19:09
  • 3
    `Code is art.` No, just no. Code is a set of instruction that should be logical, clear and concise. The example in the OP buries the incrementation in an unconventional way in the for loop body, rather than explicitly in the for statement, and it does so for no discernible gain. – Taylor Dec 03 '13 at 19:09
  • 1
    @user2864740 It's more like I've spent too much time fixing code written by people who say things like `code is art`. – Taylor Dec 03 '13 at 19:11
  • 1
    @user2864740 "it makes the code more clear" Obviously not. There is a common way to use for loops, and there are reasons to use other ways. If you don't use the common way, people like me could question why. That is the opposite of making the code more clear. Because it differs from the usual way how to use Java loops, it could be possible that there is, indeed, a reason. That's what I'm asking. Opinions are for comments. – stuXnet Dec 03 '13 at 19:14

1 Answers1

1

No. If the addition was prefix instead of postfix, it would make sense since the meaning would change entirely. In this particular case, there does not even seem to be any reason in terms of optimization. As per the comments, this is probably just a case of hasty coding.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264