I think the below code is difficult to understand. I also feel it abuses Java's ternary operator.
String name = ((this.getAllNamesAsDelimitedString().contains(incomingName) ?
incomingName:
(CollectionsUtils.isEmptyCollection(this.getEntityOperationMap()) ?
null :
this.getEntityOperationMap.get(incomingName))));
Above getAllNamesAsDelimitedString
will return a comma (,
) delimited String
.
I'd prefer something instead of such a nested ternary operator, if it looks cleaner and easier to understand. It would also be easy to sort out merge issue in, say, SVN.
N.B: There are even more nested ternary conditions in the code that I just have taken over.
Is there any general guideline which suggests the number of levels a ternary operator should be allowed? In other words, any guideline suggesting when you should break your ternary operator for better code readability?
Map.get
will returnnull
if the map is empty, so theCollectionsUtils.isEmptyCollection
branch is entirely unnecessary. \$\endgroup\$null
or empty. However, not the best of ideas to considernull
maps the same as empty maps. \$\endgroup\$null
, since that's the only real purpose. \$\endgroup\$this
would help \$\endgroup\$