Reputation: 10693
While working on jstl tags I came across foreach loop. For 'foreach' loop jstl provides the two attributes var and varstatus. As far as I understand they can be used to directly play on index parameter. But I figured out the other way of directly working on index in foreach loops.
<c:forEach items="${someForm.colorSettings}">
<tr>
<c:if test="${someForm.colorSettings[index].parameterValue1 == 'GreaterThanColor'}">
<td height="30"><fmt:message key="${someForm.colorSettings[index].parameterName.messageKey}" /></td>
<td class="rightbordernone"><div class="relative mlbdiv">
<div class="color_picker_dis" style="background-color: ${someForm.colorSettings[index].colorCodeBackground}"></div>
<div class="color_picker_dis" style="background-color: ${someForm.colorSettings[index].colorCodeForeground}"></div>
</div></td>
</c:if>
</tr>
</c:forEach>
Just wanted an advise. If I am using the above mentioned way to directly work on index inspite of using var and varstatus variable, is it wrong in some way ? Is there any performance overhead using directly index inspite of using varstatus attribute ? I guess its just way of representing like using while or for loop.
Upvotes: 0
Views: 1557
Reputation: 160231
It's longer, hence harder to read, and continually indexing into the collection.
Is it really preferable to type bddForm.colorSettings[index]
than, say, colorSetting
?
Performance-wise it depends on the underlying collection: an ArrayList
provides O(1) access to indexed members while a linked list is O(N). Would it matter? Not for any reasonably-sized JSP page, but it depends on what you're actually doing with the list.
Upvotes: 1
Reputation: 490
I don't thinks so it is making any difference if you use index variable directly or var argument. As it is mentioned above it is a similar case like we use while or for loop in java. Storing a list reference in var or directly using the list is almost one and a same thing. As far as I can judge there is not a performance impact anyways if we use index variable
Upvotes: 2
Reputation: 11
Its completely fine is you use index variable instead of using attributes var and var status. Its just a way of representation.
A very similar case of using while or foreach loop in java. Using while loop instead of for loop in some situation where its not making any logical difference is completely individual choice.
Upvotes: 0
Reputation: 9162
There is no penalty AFAIK.
But consider a foreach
in a foreach
tag. Index variable name will not be obvious.
To avoid this - I suggest you always use varstatus / var.
Upvotes: 0