1

I cannot decide which method is better with consideration of both readability and fault-tolerance.

Method #1:

Object[] objects = getArrayOfKnownLength();
int i = -1;
someMethod(objects[++i], objects[++i], objects[++i], objects[++i]);

Method #2:

Object[] objects = getArrayOfKnownLength();
someMethod(objects[0], objects[1], objects[2], objects[3]);

I have always done the second method, but there have been several cases where I got IndexOutOfBoundsException because I made some changes which affected the expected array element position. So I have been considering using the first method to prevent this issue. My only concern is that it may be less readable.

I know this is a very minor thing to be questioning, but I am curious what the majority opinion is.

  • 4
    Order of evaluation of arguments is not specified for C++, while it is for Java and C#; perhaps you should pick one of these three languages to ask about? – Josh Caswell May 20 '15 at 23:48
  • 6
    I don't see how Method 1 is an improvement. I also don't see how it prevents IndexOutOfBoundsException. – Robert Harvey May 21 '15 at 0:04
  • 4
    Even when it's defined in Java, people with lots of C or C++ experience will hate the first one and change it to the second one. In the end, it relies on some rather obscure behaviour, and it is is only usable in one of 24 cases (when I want to pass objects in order 0,1,2,3 and not 0,1,3,2 or 0,2,1,3 or 0,2,3,1 or ... – gnasher729 May 21 '15 at 0:06
  • 4
    why would you ever do the first one? – Ewan May 21 '15 at 12:33
  • "...because I made some changes which affected the expected array element position" I think that is the real problem. – h.j.k. May 21 '15 at 15:40
6

As far as I can see, when you write:

I made some changes which affected the expected array element position

The advantage that you're focusing on is that in Method #1, the start position is only found in one place (the initial value of i) whereas in Method #2, that information is implicitly contained in all four array indexes.

Seen this way, the second method is a violation of the Don't Repeat Yourself (DRY) principle, as you're repeating knowledge. What you described is exactly the expected outcome when you violate DRY: when the repeated knowledge changes, you have a tricky, error-prone process of updating it in all the places it's contained.


However, I think we can do better than the first method too. Instead of the needlessly tricky increment operator, why not say directly what we mean:

Object[] objects = getArrayOfKnownLength();
int start = 0;
someMethod(objects[start], objects[start+1], objects[start+2], objects[start+3]);

Now we have all the benefits of the first method, without the ugliness and difficulty to read.

  • Good answer. I'd like to add that most programmers expect ++ to be inside a loop, so Method#1 might be confusing there. – user949300 May 21 '15 at 13:59
  • I like this answer best. The reason I was second guessing my original method (#2) was because one of my coworkers said it was better to do method #1 for the reason that it prevents copy paste errors which had me persuaded. – Jon McPherson May 21 '15 at 19:40
  • 1
    +1, Would add another one for the correct application of DRY principle (avoiding repeat of knowledge, not avoiding repetition per se) – Marjan Venema May 22 '15 at 18:58
5

Your first method has 4 side effects (the pre-increments) while the second has none.

In general you should avoid side-effects if you can.

Furthermore, the second one is more clear about your intent. If I would see the first one in someone else's code I would ask myself if there was any reason for performing the pre-increments.

So, stick with the second option.

  • ok. ya I have always just gone with the second option and never gave it any other thought until one of my coworkers told me it was better to do it this way, and he persuaded me. Thanks for clearing that up! – Jon McPherson May 21 '15 at 19:34
3

The First method is horrible on my vision and it does not prevent "Exception". The same feelings I have when I see "magic numbers"! XD

If possible, you need to change the architecture, something is wrong! Or You need to check it before to use it in the method! Or be right that those params always come with their values filled and in the same order!

Did You change the position of parameters? Refactoring or change the design...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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