Smarter inspection: 'if' statement can be re-written as '?:' expression

I'm all for consistent look and feel of the code in our project, and
removing redundant code seems to help towards that end.

Converting
if (c) x = e1; else x = e2;
to
x = c ? e1 : e2;
every time seemed like a good idea.

But the analyzer would also have me do the following conversion:
if (c_a) x = e1;
else if (c_b) x = e2;
else x = e3;
to
if (c_a) x = e1;
else x = c_b ? e2 : e3;
which is rather less readable than it was.

Would you consider only suggesting conversion for if's that aren't preceeded
by an else?

+ Hans Christian

3 comments
Comment actions Permalink

In addition to your request, I'd like to not be prompted for '?:' in cases where the conditional is fills up a full line of code: 'if (blah || blah || a really big condition that takes up a lot of space || etc.)' should not show that prompt.

0
Comment actions Permalink

Hello,

In addition to your request, I'd like to not be prompted for '?:' in
cases where the conditional is fills up a full line of code: 'if (blah
|| blah || a really big condition that takes up a lot of space ||
etc.)' should not show that prompt.


I'm afraid this will be a confusing behavior, when the suggestion comes and
goes in seemingly identical situations — when the complexity is just above
or just below the threshold.

In some cases the suggestion could be reduced to a hint or just a silent
context action, though. This probably will not be addressed in 4.0.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Please sign in to leave a comment.