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

How to organize java classes?
Execute multiple SQL queries simultaneously via multi threading
Filtering on a list
DateFormat format.parse parse exception: Unparseable date
use of javax.servlet package in maven-bundle-plugin
Why static method cannot hide instance method in java
How can change font to JXteeTable for particular row
com.mysql.jdbc.Driver Error when I run the Jar File although that exists in my Build Path
What is the best way to share only the getter methods to client?
postgresql driver not loaded by the OSGI container?
Eclipse E4 RCP global error handling
PayPal Integration with java
am getting error with android studio R.java
How do i remove \r\n in a JSON file
is there a difference between searching in txt files and searching in html file in java-android
SymmetricDS conditional sync

Categories

HOME
java
pandas
log4j
gitlab
testng
react-router
rsyslog
jsp-tags
yahoo-oauth
paw-app
retrofit
fancybox
podio
session-variables
handsontable
google-cloud-ml
quartz-scheduler
libtiff
fatal-error
reactcsstransitiongroup
nhibernate-envers
viewport
mps
csrf-protection
dcevm
wijmo
spark-jobserver
kannel
facebook-apps
kendo-datasource
lightswitch-2013
stormpath
amazon-kinesis-kpl
simplexml
hockeyapp
twitch
hue
temporary-files
ruby-on-rails-3.1
glew
import-from-excel
flashair
hibernate-tools
qsslsocket
jquery-nestable
sage-one
nstextview
colorama
knockout-components
libusb-win32
knpmenubundle
nbconvert
imanage
minimization
watchconnectivity
medium.com
asp.net-4.5
paypal-express
firebaseui
gridview-sorting
qcustomplot
pickadate
dlna
dataview
r-tree
marching-cubes
cannon.js
coveralls
angular-leaflet-directive
browser-bugs
apache-commons-fileupload
ami
composite
remobjects
angular-local-storage
directoryservices
javax.mail
npapi
kineticjs
cctv
id3v2
document-classification
responsive-slides
openexr
article
message-driven-bean
uv-mapping
access-rights
flash-builder4.5
file-locking
html4
selected
mhtml
frameset
bubble-chart
eclipse-templates
fireworks
servicehost
self-extracting
filtered-index
vc90
ext3
substrings
ctp4
text-coloring
ugc
paul-graham
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App