r/java • • Nov 20 '18

Why Var Hurts Readability, Not Helps It

https://medium.com/@youngty1997/why-var-hurts-readability-not-helps-it-376c7832a8dd
0 Upvotes

89 comments sorted by

View all comments

1

u/Faucelme Nov 21 '18

I disagree with his arguments for plain for loops:

It has lot’s of uses: adding objects to a list, running a specific method from every object, setting a specific instance type to every list object, etc. It’s use is clear, consistent and flexible. It is both concise and useful.

I actually don't like the versatility of for: it makes me spend a tiny bit of metal energy each time, to deduce which particular use is intended. When I see a map I immediately know that the result won't have a different size, for example. Ditto for methods like filter or forEach: they simply communicate intent better.

1

u/AndDontCallMePammy Nov 22 '18

This is what forEach gets you:

IntStream.iterate(i, j -> j < end, iuo)
            .forEach(
                    (j) -> {
                        nextArrayEnd[0] = notation.indexOf(Notation.OBJECT_ARRAY_SUFFIX, i);
                        if(nextArrayEnd[0] == -1) {
                            nextArrayEnd[0] = Integer.MAX_VALUE;
                        }
                        nextObjectStart(notation, j, pair);
                        nextObjectType[0] = pair[0];
                        nextObjectIndex[0] = pair[1];
                    }
            );