Wednesday, April 6, 2011

Why are assertEquals() parameters in the order (expected, actual) ?

Why do so many assertEquals() or similar function take the expected value as first parameter and the actual one as second ?
This seems counter-intuitive to me, so is there a particular reason for this unusual order ?

From stackoverflow
  • Because the authors had a 50% change of matching your intuition.

    Because of the other overload

    assertWhatever(explanation, expected, actual)
    

    And the explanation, which is part of what you know, goes with the expected, which is what you know, as opposed to the actual, which you don't know at the time you write the code.

    jor : However in that case, it becomes slightly inconsistent because in most languages, the required parameters appear first, and the mandatory ones last. This is why I would more naturally go for assertWhatever(actual, expected [,explanation])
    jor : Sorry, I meant the _optional_ ones appear last, not mandatory...

0 comments:

Post a Comment