Ternary operator: bad or good practice? [duplicate] - ternary-operator

I'm looking for reasons to use/not to use it and for original ideas (in their use and to replace them).
Duplicate:
To Ternary Or Not To Ternary
Related (but does not address the question being asked):
Which coding style you use for ternary operator?

For the sake of readability, I only use a ternary if it fits into one 80-char line.

Good for short tags in templating languages like PHP, e.g:
<form>
<input type='radio' name='gender' value='m' <?=($gender=='m')?"checked":""?>>Male
<input type='radio' name='gender' value='f' <?=($gender=='f')?"checked":""?>>Female
</form>
Good for switches in javascript/jQuery:
var el = $("#something");
$(el).is(':visible') ? $(el).hide("normal") : $(el).fadeIn("normal");
Good for assignment, especially where a particular variable name can take different types:
$var = ($foo->isFoo()) ? 'Success!' : false;

It's something like the for loop. Makes sense for what it's made for but when you try to stick more stuff in it, it becomes unreadable.

The conditional ternary operator can definitely be overused, and some find it quite unreadable. However, I find that it can be very clean in most situations that a boolean expression is expected, provided that its intent is clear. If the intent is not clear, it is best to use a temporary variable with a clear name whose value is assigned using an if-statement, or to use a function with a good name that returns the expected value.

Which ternary operator are you talking about?
A ternary operator is any operator that takes three arguments.
If you're talking about the ? : operator, this is called the conditional operator. I can't live without it anymore, personally. If-else statements look so messy to me, especially when doing a conditional assignment. Some complain that it looks messy, but it is still possible (especially if using Visual Studio or another intelligent-formatting IDE) to make things easily readable, and you should be commenting all your conditionals anyway.

Related

nl2br in JSF2 without violating MVC paradigm?

i'm trying to figure out how to most elegantly integrate something like PHP's nl2br() function into a current project done with JSF2/Spring. I could easily create getters using something like return text.replaceAll("\n","<br/>"); in my model classes, however that does seem like putting view related code where it does not belong. I have the same feeling about storing actual html content in my database.
I guess the cleanest solution would be using tags/EL for this, however i couldn't find something that seemed to do just that. How would you guys implement something like this? Thank you in advance, any hints are highly appreciated!
Use either CSS, assuming that the text doesn't contain any HTML
<div style="white-space: pre">#{bean.text}</div>
Or create a custom EL function and then display it unescaped (Facelets implicitly escapes HTML)
<div><h:outputText value="#{my:nl2br(bean.text)}" escape="false" /></div>
You should only make absolutely sure that it's free of XSS.
Well, in the first place JSF is a Web UI framework. So, anything that you expect to output to the user will end as HTML (with the only exception of javascript, though). So, I don't find it a grave violation of MVC (if any at all). Maybe you could even push the envelope and directly use <br/> inside the text, instead of replacing \n
In a more general sense, if you have different lines/paragraphs in your text, the more flexible/standard solution would be break your text in the different elements and let your presentation logic handle it. So, instead of a properties with
presentationPage.introductionText=Ipse Lorum ...sum.\nVini vidi vinci.
You would end with
presentationPage.introductionText.par1=Ipse Lorum ...sum.
presentationPage.introductionText.par2=vini vidi vinci.

Best way to switch classes in Angular

In my Angular4 application, I have a code to conditionally apply two classes that looks like this:
<some-element [ngClass]="{ 'fa-toggle-on': category.active, 'fa-toggle-off': !category.active }">
</some-element>
This works, but is this really the recommended way? It feels a bit unclean to write down category.active twice. I was looking for something more if-else like, but could not find anything so far in the docs and on SO.
ngClass directive takes an expression. The expression can evaluate to a string.
Here is how to how to use the directive with the string:
<some-element [ngClass]="'first second'">...</some-element>
So in your case use the expression that evaluates to a string:
<some-element [ngClass]="category.active ? 'fa-toggle-on' : 'fa-toggle-off'">

JsPrettier in Sublime 3 does not single quote className

I´m using Sublime Text 3 with JSPrettier to format Javascript and JSX code.
I have the following set in my JSPrettier configuration:
// If true, will use single instead of double quotes
"singleQuote": true
Even with that, when formatting the code JSPrettier still insists on putting double quote on className and other JSX properties, like:
<div className="uxm-details-header-buttons">
or
<Icon name="refresh" />
I really expected everything with single quote, keeping double ones for special cases.
Any hint on how to solve that ?
This is currently not possible using just prettier. Quotes in JSX will always be double and prettier will ignore this setting.
If you want to understand the motivation behind this decision, you can read this GitHub thread. The gist of the argument for keeping JSX quotes as double is to enforce a style across the community and the fewer config options the better.
Any hint on how to solve that ?
You can run your code through prettier-eslint. This formats your code via prettier, and then passes the result of that to eslint --fix. This way you can get the benefits of prettier's superior formatting capabilities, but also benefit from the configuration capabilities of eslint.

java web playframework: how to ignore Evaluating Expression in a Template?

In view (may be Application/index.html), I had the content like:
...
<div>${don't need evaluate this content} </div>
...
I don't want Play! evaluate it, only display absolutely this content on screen, so how can I do it ?
Update:
Thank Loic and Niels, I have just checked them, both solutions:
#{verbatim}${don't need evaluate this content}#{/verbatim}
and
<div>'${don't need evaluate this content}' </div>
didn't work!
Note that, I use XML in response type.
I managed to get this working using the following
<div>${don't need evaluate this content}</div>
The best practice is to use the verbatim tag:
#{verbatim}${don't need evaluate this content}#{/verbatim}
From my groovy experience I would guess to use simple quotes so
<div>'${don't need evaluate this content}' </div>
But it's not tested. Perhaps \$ works too. At least $ instead of $ should work.
All above didn't work in multilines.
Solution:
${""" ANY
THING
IN HERE"""}
""" is here-doc expression in Groovy and PHP.
''' is also here-doc without evaluation.
so, we can also do %{print(''' ${test} ''')}%

What is the difference between using “logic:equal” and “logic:notEqual” tags?

For example, I can have a code like:
<logic:equal name="userForm" property="isRishi" value="YES">
Welcome, your name is Rishi</logic:equal>
And another code like this:
<logic:notEqual name="userForm" property="isRishi" value="NO">
Welcome, your name is Rishi</logic:notEqual>
Both would function the same. So what difference is there in using these two tags?
If there are only two possible values, the only difference is that the second form is harder to understand due to the double negation.
If isRishi can have values other than "YES" and "NO" (e.g. null) then the difference should be obvious.
They exist for better readability. notEqual to no is convoluted and harder to grasp.
And also, for the same reason why the ! operator exists.
There is also the else-case. What if you want to do one thing in the "YES" case and one in the not-YES case? You could phrase the other case as equal to NO, but (a) it might be null and (b) if you dont have boolean but something else there might be many values you are not interested in.

Resources