5

which one of the following is better?

<c:set var="var1" value="false" scope="request"/>
<c:if test="${someCondition}">
    <c:set var="var1" value="true" scope="request"/>
</c:if>

Or the following

<c:choose>
    <c:when test="${someCondition}">
        <c:set var="var1" value="true" scope="request"/>
    </c:when>
    <c:otherwise>
        <c:set var="var1" value="false" scope="request"/>
    <c:otherwise>
</c:choose>
user624558
  • 559
  • 2
  • 8
  • 20

3 Answers3

5

Neither, this looks best for me:

<c:set var="var1" value="${someCondition}" scope="request"/>
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

The first, because it is more concise.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

I'd do what Tomasz suggested. If you have different values rather than booleans, you can use a ternary statement:

<c:set var="var1" value="${someCondition == 'someValue' ? 'valueA' : 'valueB'}" scope="request"/>
alextiley
  • 31
  • 1
  • 5