I particularly like the use of ternary condition, but I see a lot of developers out there saying, there's even rule in CheckStyle when validating your code that encourages you not to use.
See the example below:
var idElemento = $(this).attr("id");
var isDataInicial = idElemento.contains("Inicial");
$("#" + (isDataInicial ? idElemento.replace("Inicial", "Final") : idElemento.replace("Final", "Inicial"))).datepicker("option", isDataInicial ? "minDate" : "maxDate", selectedDate == "" ? null : selectedDate);
Taking the fact that if something else has to be implemented when the condition is true and / or false we will have more work to undo the ternary and create the if/else
structure I do not see any problem in the above code, maybe better formatting in the maximum, but in a row was solved the problem that without the ternary we would have something like:
if ((idElemento.contains("Inicial")) {
if (selectedDate == "") {
$("#" + idElemento.replace("Inicial", "Final")).datepicker("option", "minDate", null);
} else {
$("#" + idElemento.replace("Inicial", "Final")).datepicker("option", "minDate", selectedDate)
}
} else {
if (selectedDate == "") {
$("#" + idElemento.replace("Final", "Inicial")).datepicker("option", "maxDate", null);
} else {
$("#" + idElemento.replace("Final", "Inicial")).datepicker("option", "maxDate", selectedDate)
}
}
So the question remains, when should we use the ternary? And is it so bad to use ternaries nested with a good indentation?