Possible refactoring idea - convert if/else to ternary operator and vice-versa

I've found myself occasionally refactoring code that looks like this:

if ( test )
a = b;
else
a = c;

It would be nice to be able to have a refactoring to convert this to:

a = ( test ) ? b : c

Matt.

8 comments
Comment actions Permalink

Hello Matthew,

Also vice versa!

Refactorings seem to always come in pairs don't you think?

Ronnie

I've found myself occasionally refactoring code that looks like this:

if ( test )
a = b;
else
a = c;
It would be nice to be able to have a refactoring to convert this to:

a = ( test ) ? b : c

Matt.



1
Comment actions Permalink

Of course! :)

ronnie@freenet.co.uk wrote:

Hello Matthew,

Also vice versa!

Refactorings seem to always come in pairs don't you think?

Ronnie

>> I've found myself occasionally refactoring code that looks like this:
>>
>> if ( test )
>> a = b;
>> else
>> a = c;
>> It would be nice to be able to have a refactoring to convert this to:
>>
>> a = ( test ) ? b : c
>>
>> Matt.
>>


0
Comment actions Permalink

Great Idea... It should also be a Code Reformatting option..


0
Comment actions Permalink

I've added the request to the tracker


0
Comment actions Permalink

I've actually run across some code recently that has lots of instances of
this:

return (some condition) ? true : false;

Now, that's completely redundant and silly code that one would hopefully
never write, but it sure would be nice if Resharper could notice that this
is redundant and flag it as such, and offer a quick-fix to reduce it to the
simple:

return (some condition);


"Matthew Mastracci" <matt@aclaro.com> wrote in message
news:ddinqs$6lq$1@is.intellij.net...

I've found myself occasionally refactoring code that looks like this:

>

if ( test )
a = b;
else
a = c;

>

It would be nice to be able to have a refactoring to convert this to:

>

a = ( test ) ? b : c

>

Matt.



0
Comment actions Permalink

There's also the infamous:

if (some condition == true) ...

which would be nice to mark as redundant...

"Paul Bradshaw" <pbradshaw@advsol.com> wrote in message
news:decs0g$qac$1@is.intellij.net...

I've actually run across some code recently that has lots of instances of
this:

>

return (some condition) ? true : false;

>

Now, that's completely redundant and silly code that one would hopefully
never write, but it sure would be nice if Resharper could notice that this
is redundant and flag it as such, and offer a quick-fix to reduce it to
the simple:

>

return (some condition);

>
>

"Matthew Mastracci" <matt@aclaro.com> wrote in message
news:ddinqs$6lq$1@is.intellij.net...

>> I've found myself occasionally refactoring code that looks like this:
>>
>> if ( test )
>> a = b;
>> else
>> a = c;
>>
>> It would be nice to be able to have a refactoring to convert this to:
>>
>> a = ( test ) ? b : c
>>
>> Matt.
>



0
Comment actions Permalink

This is done in 201 build

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Amir Kolsky" <amir@xpandsoft.com> wrote in message
news:def1vf$cqs$1@is.intellij.net...

There's also the infamous:

>

if (some condition == true) ...

>

which would be nice to mark as redundant...

>

"Paul Bradshaw" <pbradshaw@advsol.com> wrote in message
news:decs0g$qac$1@is.intellij.net...

>> I've actually run across some code recently that has lots of instances of
>> this:
>>
>> return (some condition) ? true : false;
>>
>> Now, that's completely redundant and silly code that one would hopefully
>> never write, but it sure would be nice if Resharper could notice that
>> this is redundant and flag it as such, and offer a quick-fix to reduce it
>> to the simple:
>>
>> return (some condition);
>>
>>
>> "Matthew Mastracci" <matt@aclaro.com> wrote in message
>> news:ddinqs$6lq$1@is.intellij.net...
>>> I've found myself occasionally refactoring code that looks like this:
>>>
>>> if ( test )
>>> a = b;
>>> else
>>> a = c;
>>>
>>> It would be nice to be able to have a refactoring to convert this to:
>>>
>>> a = ( test ) ? b : c
>>>
>>> Matt.
>>
>>
>



0
Comment actions Permalink

Yeah, I actually noticed this one already being marked. Found a few of them
in my code the first time I loaded 201 up :)


"Amir Kolsky" <amir@xpandsoft.com> wrote in message
news:def1vf$cqs$1@is.intellij.net...

There's also the infamous:

>

if (some condition == true) ...

>

which would be nice to mark as redundant...

>


0

Please sign in to leave a comment.