Well, the ternary operator in Java acts like this...
return_value = (true-false condition) ? (if true expression) : (if false expression);
...Another way of looking at it...
return_value = (true-false condition)
? (if true expression)
: (if false expression);
You question is kind of vague and we have to assume here.
If (and only if)
callFunction(...)declares anon-voidreturn value (Object,String,int,double, etc..) - it seems like it does not do that via your code - then you could do this...return_value = (string != null) ? (callFunction(...)) : (null);If
callFunction(...)does not return a value, then you cannot use the ternary operator! Simple as that. You will be using something that you don't need.- Please post more code to clear up any issues
Nonetheless, ternary operator should represent alternative assignments only!! Your code does not seem to do that, so you should not be doing that.
This is how they should work...
if (obj != null) { // If-else statement
retVal = obj.getValue(); // One alternative assignment for retVal
} else {
retVal = ""; // Second alternative assignment for retVale
}
This can be converted to...
retVal = (obj != null)
? (obj.getValue())
: ("");
Since it seems like you might be trying to just refactor this code to be a one-liner, I have added the following
Also, if your false-clause is truely empty, then you can do this...
if (string != null) {
callFunction(...);
} // Take note that there is not false clause because it isn't needed
OR
if (string != null) callFunction(...); // One-liner
Answer from Christopher Rucinski on Stack Overflowjava - Ternary Operator - Stack Overflow
What is so awful about the ternary operator?
Depends on how it's used, it should improve readability if used correctly. It is however often misused and that might be why Sonar doesn't like it. The rules should be configured to fit the needs of the project, if the review process ensures that the operator is used in appropriate situations just change the rule.
More on reddit.comIs using the ternary operator bad practice?
Ternary Operators
I use them frequently. If it's a simple case like:
if (someValue > 0) {
label = "positive";
}
else {
label = "negative";
}Then I'll essentially always replace that with:
label = (someValue > 0) ? "positive" : "negative";
I'll also often replace:
if (someValue > 0) {
doSomething("positive");
}
else {
doSomething("negative");
}With:
doSomething(someValue > 0 ? "positive" : "negative");
Reasons not to use a ternary might include:
-
I need to do something (like logging, or calling some other function), rather than just assigning a value
-
The line ends up being long / complex / messy and it's more readable to break it up
-
I'd end up nesting ternaries (usually that gets ugly)
Videos
Well, the ternary operator in Java acts like this...
return_value = (true-false condition) ? (if true expression) : (if false expression);
...Another way of looking at it...
return_value = (true-false condition)
? (if true expression)
: (if false expression);
You question is kind of vague and we have to assume here.
If (and only if)
callFunction(...)declares anon-voidreturn value (Object,String,int,double, etc..) - it seems like it does not do that via your code - then you could do this...return_value = (string != null) ? (callFunction(...)) : (null);If
callFunction(...)does not return a value, then you cannot use the ternary operator! Simple as that. You will be using something that you don't need.- Please post more code to clear up any issues
Nonetheless, ternary operator should represent alternative assignments only!! Your code does not seem to do that, so you should not be doing that.
This is how they should work...
if (obj != null) { // If-else statement
retVal = obj.getValue(); // One alternative assignment for retVal
} else {
retVal = ""; // Second alternative assignment for retVale
}
This can be converted to...
retVal = (obj != null)
? (obj.getValue())
: ("");
Since it seems like you might be trying to just refactor this code to be a one-liner, I have added the following
Also, if your false-clause is truely empty, then you can do this...
if (string != null) {
callFunction(...);
} // Take note that there is not false clause because it isn't needed
OR
if (string != null) callFunction(...); // One-liner
Yes. You can with keeping same null in else block.
String result = str !=null ? callFunction(parameters) : null;
Make sure that callFunction(parameters) return a String.
SonarQube flags this a a major code smell. It seems to be OK to use it with a more functional language like JavaScript, but for some reason should be avoided at all costs when coding in Java.
I think one of the worst things about Java is its verbosity. I find that the newer, more functional extensions help with that a lot, and the ternary operator (although it's been around much longer) works very well with that programming style.
If it's such a bad thing, why was it made part of the language?
There's nothing awful about it. It might be just easy to abuse.
I think it should be used for simple expressions that fit on one line, and not to evalute complex expressions.
Good:
String x = datehour >= 12 ? "PM" : "AM";
Bad:
String x = foo.getBaz().getBar(a, b * 13, 42) != null && x.doSomething() < y.some().other().long().expression() ? foo.calculate().something().ifTrue(123) : array[bar.getIndex()].stream().map(x -> x.getField()).collect(Collectors.toList());
I believe that unreadable code is never the fault of a language construct (ternary operator in this case) but the programmer who wrote it. Many of the syntaxes can be abused and result in hard to read code. It's fine to discourage its use to help increasing readability in larger operations, but saying it should be avoided at all costs is ridiculous.
major code smell
https://en.wikipedia.org/wiki/Code_smell
a code smell is any characteristic in the source code of a program that possibly indicates a deeper problem.
It's definitely not a code smell in itself.
I think one of the worst things about Java is its verbosity.
I think on the contrary (opinion). I believe it is a major benefit that Java is verbose, and doesn't have simple syntax for expressing complex operations. It makes it easier to read, as readers doesn't have to keep in mind too may contextual information when reading a given expression. It also makes newcomers understand the code easier without requiring them to google for every second syntax.
Following up, I also think that it is for the better that Java doesn't have properties like C#, the .? operator, operator overloading, and others.
Depends on how it's used, it should improve readability if used correctly. It is however often misused and that might be why Sonar doesn't like it. The rules should be configured to fit the needs of the project, if the review process ensures that the operator is used in appropriate situations just change the rule.