java


Conditional method calling by abusing ternary wrapped in if statement?


Recently I was doing a code review and came across this guy:
if(!sharePermission.isExpired() ? activePermissions.add(sharePermission) : expiredPermissions.add(sharePermission));
Basically using a ternary if statement to call methods that return a boolean value and wrapping it in an if(...) statement to satisfy the requirement of assignment. Is this more or less valid than
if(!sharePermission.isExpired())
activePermissions.add(sharePermission);
else
expiredPermissions.add(sharePermission);
if you really need to condense your code to one line? Is any space allocated for the if(...) if assignment is indeed occurring?
I'm not a fan of either of them, just curious.
There is no assignment happening, only an evaluation of a boolean condition. No extra memory is allocated for the result of the evaluation.
Using a ternary expression to emulate a ternary statement, however, is grossly unorthodox. It is going to reduce readability of your code, without bringing any additional benefit. Hence, using a plain if with an else is a better alternative.
Note that if activePermissions and expiredPermissions are of the same type, you can use a ternary expression to decide between the targets of the add call, as follows:
(sharePermission.isExpired() ? expiredPermissions : activePermissions).add(sharePermission);
It's an abuse of the if statement to do that, never mind the conditional expression.
It would be cleaner to write either a full if statement, or to use the conditional operator to select a list to add to:
List<Permission> list = isExpired() ? expiredPermission : activePermission;
list.add(sharePermission);
The ternary equivalent for the if..else that you are looking for is something like -
(!sharePermission.isExpired() ? activePermissions : expiredPermissions).add(sharePermission); // no if here
is equivalent to
if(!sharePermission.isExpired()) {
activePermissions.add(sharePermission);
}
else {
expiredPermissions.add(sharePermission);
}

Related Links

Appsflyer sending uninstall event
Occasionally getting callbacks after removeLocationUpdates()
Modify JSESSIONID cookie in request headers
Moving objects on screen
Prority based threads?
Attempt to read from null array-Trying to get Array from other Activity returns null
Android jni UnsatisfiedLinkError wrong function name
Selendroid: By locator ByCssSelector is curently not supported
Why can't I try-with-resources in MY servlet?
Can you use JDBC to connect to just an instance without specifying a database?
Why is my redirectAttributes.addFlashAttribute and data Re-populating not working after validation fails?
Java 8 Lambdas - Bitwise AND Operations
how to format string line that contains numbers with regex
Java code to create bigquery table using json schema
Java androd ExpandableListView checkbox in group view
Counting the number of times a specific word appears in a txt file while using a try/catch block in java

Categories

HOME
cloud
asp.net-core
wso2
amazon-ec2
homebrew
fft
platform-builder
syntax
tesseract
leon
microservices
jgroups
jxls
ip
contact
awesome-wm
metatrader4
dosgi
invantive-sql
flux
jqwidget
contextmenu
crystal-reports-2008
lucene.net
visual-composer
visjs
object-detection
realex-payments-api
language-agnostic
microsoft-r
blazemeter
sparse-matrix
spark-jobserver
user-interaction
tooltipster
ejabberd-module
saas
objectanimator
srcset
tinymce-4
bosh
scorm2004
copying
kendo-ui-grid
windows-dev-center
android-nestedscrollview
google-api-nodejs-client
picasso
suricata
bitbucket-pipelines
neuroscience
abstract-class
hilbert-curve
leading-zero
mapzen
dynamic-reports
http-live-streaming
botbuilder
http-redirect
import-from-excel
galleria
atomicity
rails-routing
promela
boost-preprocessor
spring-mongodb
chord-diagram
synchronous
nested-sets
google-feed-api
removechild
login-control
clang-static-analyzer
skype4py
freedesktop.org
collapse
atk4
purge
0xdbe
deis
p2
clicktag
tt-news
file-copying
issuu
rtmfp
concurrent-collections
jsctypes
gridfs
dataservice
mylyn
django-nonrel
lcs
itmstransporter
mt
javaspaces
libc++
qt-jambi
doh
vdsp
invite
calling-convention
radcombobox
eqatec
premature-optimization
noscript
data-acquisition
document-conversion

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App