Optimize ternary operator

That’s just horrible code. It’s badly formatted. I don’t see the hierarchy of the expression. Even if it had good formatting, the expression would be way too complex to quickly parse with the human eye. The intention is unclear. What’s the purpose of those conditions? So what can you do? Use conditional statements (if). Extract … Read more

Javascript && operator versus nested if statements: what is faster?

The performance difference is negligible. The && operator won’t check the right hand expression when the left hand expression evaluates false. However, the & operator will check both regardless, maybe your confusion is caused by this fact. In this particular example, I’d just choose the one using &&, since that’s better readable.

What is the result type of ‘?:’ (ternary/conditional operator)?

Expressions don’t have return types, they have a type and – as it’s known in the latest C++ standard – a value category. A conditional expression can be an lvalue or an rvalue. This is its value category. (This is somewhat of a simplification, in C++11 we have lvalues, xvalues and prvalues.) In very broad … Read more

How do I use the conditional (ternary) operator?

It works like this: (condition) ? true-clause : false-clause It’s most commonly used in assignment operations, although it has other uses as well. The ternary operator ? is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (IIf(condition,true-clause,false-clause) in VB, for example). For example: bool Three = … Read more

Benefits of ternary operator vs. if statement

Performance The ternary operator shouldn’t differ in performance from a well-written equivalent if/else statement… they may well resolve to the same representation in the Abstract Syntax Tree, undergo the same optimisations etc.. Things you can only do with ? : If you’re initialising a constant or reference, or working out which value to use inside … Read more

Ternary operator in JSTL/EL

I tested the following page in Tomcat 5.59, JSP 2.0 and JSTL 1.1. It ran without any errors. <%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%> <c:set var=”value” scope=”request” value=”someValue”/> <c:out default=”None” escapeXml=”true” value=”${not empty value ? value : ‘None’}” /> <c:out default=”None” escapeXml=”true” value=”${empty value ? ‘None’ : value}” /> <c:set var=”value” scope=”request” value=”” /> <br/> <c:out default=”None” escapeXml=”true” … Read more