Re: Feedback on ForStatement
"Milles, Eric (TR Technology) via dev" <dev-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]>
| Newsgroups | gmane.comp.lang.groovy.devel |
|---|---|
| Message-ID | <PH7PR03MB7224C1040A2A707DF20818228A66A@PH7PR03MB7224.namprd03.prod.outlook.com> |
I tried to make minimal changes when adding index variable to ForStatement. It seemed that getValueVariable() and getIndexVariable() was the cleanest extension of getVariable() (now deprecated). One option versus adding a constructor would be to add a "forS" factory overload to GeneralUtils that supports classical "for (int i = 0; i < n; ++i) ..." without making reference to the dummy variable. A bolder approach would be to separate "for (x in y)" from "for (int i = 0; i < n; ++i)" as distinct statement classes. Maybe they extend from ForStatement to keep visitors happy. When getIndexVariable() and getValueVariable() were introduced, I designed them so you could use them equally. That is, they return null when no index or value variable was given. To that end, I would not want to see hasValueVariable() and hasIndexVariable() introduced. You should be able to do any "if (hasValueVariable()) f(getValueVaruable())" with "Optional.ofNullable(getValueVaruable()).ifPresent(v -> f(v))" or of course "var v = getValueVariable(); if (v != null) f(v);". In summary, I'd favor a light tough for a refactoring; add as little as possible to the statement class. Unless you are willing to separate the abstractions of collection and counting loops. Eric M.