Ability to refactor boolean expressions

What would you think about the ability to refactor boolean expressions?

For instance:
- extract !

(value <= 1) => !(value > 1)

- inline !

!(value == null) => (value != null)

- change && to ||

(a && b) => !(!a || !b)

- change || to &&

(a || b) => !(!a && !b)

I can imagine, that the most intuitive solution would be intentions.

Tom

40 comments
Comment actions Permalink

I was thinking of it before too, but I thought that though software
developer does not have to understand quantum physics, the boolean
operations is his/her turf, so having this refactoring is... damaging for
developers' brains :)

Michael J.

"Thomas Singer" <fake@fake.com> wrote in message
news:oprora8tyw3im8zg@news.intellij.net...

What would you think about the ability to refactor boolean expressions?

>

For instance:
- extract !

>

(value <= 1) => !(value > 1)

>

- inline !

>

!(value == null) => (value != null)

>

- change && to ||

>

(a && b) => !(!a || !b)

>

- change || to &&

>

(a || b) => !(!a && !b)

>

I can imagine, that the most intuitive solution would be intentions.

>

Tom



0
Comment actions Permalink

... is his/her turf, so having this refactoring is... damaging for
developers' brains :)


But sometimes you /have/ weird boolean expressions, that scream for
refactoring. If IDEA could do it, it would reduce the error rate to zero.

Tom

0
Comment actions Permalink

Though I feel refactoring is very useful for most things, I don't think IDEA should add this. If a boolean expression is THAT convoluted, it suggests that the developer may not fully understand what is happening, and I would say that would make any refactoring operation dangerous. It could fundamentally change what is intended versus what the code will actually do.

My 2 cents worth.

0
Comment actions Permalink

Though I feel refactoring is very useful for most things, I don't think
IDEA should add this. If a boolean expression is THAT convoluted, it
suggests that the developer may not fully understand what is happening,
and I would say that would make any refactoring operation dangerous. It
could fundamentally change what is intended versus what the code will
actually do.


Well, that is refactoring. Making it more understandable. Exchanging &&
and || or eliminating too much ! help significantly to understand the
expressions. And if IDEA does it, it will be a much safer way, than the
developer can do it without IDEA's support.

Tom

0
Comment actions Permalink

+1

I agree with Tom, there are very many well-defined formulas for
simplifying boolean expressions. It would be great if IDEA could use
some of them in refactoring.

Peter

Thomas Singer wrote:
>> Though I feel refactoring is very useful for most things, I don't
>> think IDEA should add this. If a boolean expression is THAT
>> convoluted, it suggests that the developer may not fully understand
>> what is happening, and I would say that would make any refactoring
>> operation dangerous. It could fundamentally change what is intended
>> versus what the code will actually do.


Well, that is refactoring. Making it more understandable. Exchanging
&& and || or eliminating too much ! help significantly to understand the
expressions. And if IDEA does it, it will be a much safer way, than the
developer can do it without IDEA's support.

Tom


0
Comment actions Permalink

I agree.

Refactoring may alter the code but, by definition, does not alter what the
code does, therefore refactoring should never be seen as dangerous (as long
as it is performed by a tested, trusted engine). The whole point here is to
remove the danger by having it done by IDEA (a tested, trusted engine).

N.

Thomas Singer wrote:
>> Though I feel refactoring is very useful for most things, I don't
>> think IDEA should add this. If a boolean expression is THAT
>> convoluted, it suggests that the developer may not fully understand
>> what is happening, and I would say that would make any refactoring
>> operation dangerous. It could fundamentally change what is intended
>> versus what the code will actually do.
>

Well, that is refactoring. Making it more understandable.
Exchanging && and || or eliminating too much ! help significantly to
understand the expressions. And if IDEA does it, it will be a much
safer way, than the developer can do it without IDEA's support.

>

Tom



0
Comment actions Permalink

On Tue, 06 May 2003 17:43:58 +0200, Thomas Singer <fake@fake.com>
wrote:

>> ... is his/her turf, so having this refactoring is... damaging for
>> developers' brains :)
>
>But sometimes you /have/ weird boolean expressions, that scream for
>refactoring. If IDEA could do it, it would reduce the error rate to zero.

I had one of these yesterday.

It was a convoluted if statement:
if (negative expression)
xyz;
else
abc;

and I wanted to make it a positive one:
if (positive expression)
abc;
else
xyz;

I looked for a refactoring & was surprised when I didn't find one.

Neil

0
Comment actions Permalink

Neil Galarneau wrote:

It was a convoluted if statement:
if (negative expression)
xyz;
else
abc;

and I wanted to make it a positive one:
if (positive expression)
abc;
else
xyz;

I looked for a refactoring & was surprised when I didn't find one.


There is an Intention for this instead. Position the caret on the 'if'
keyword and if you have Intentions enabled you'll get a popup with
"Invert If condition". You can also force the popup to display by
pressing Alt-Enter if you have it turned off (like I do).

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919

0
Comment actions Permalink


All of these are good, but should be intentions rather than refactorings. The DeMorgan's Laws in particular would be fine intentions.

0
Comment actions Permalink

Thomas,
are you forgetting your own request or are you being manipulative... ;)

http://www.intellij.net/tracker/idea/viewSCR?publicId=6021

I do agree 1000% BTW (I even voted and surprisingly I was the first. I had to scavenge some other votes in the process though... More VOTE more Vote!)

0
Comment actions Permalink

I agree with the fact that refactoring are there to make the code more legible and easier to understand.
However, I don't find !(!a || !b) more legible than (a && b)... and while you could add lots of neat things to IDEA, I respectfully think that this is not one of them. The Invert condition intention is good enough.

0
Comment actions Permalink

Try to read

if !(x != null && !x.empty())
return;

and

if (x==null || x.empty())
return

Which one do you think is easier to read?

0
Comment actions Permalink

Didn't think of it that way... Please forgive my sleepy self! :)

0
Comment actions Permalink

I could remember that I already requested such a feature, just could
not find it any more. And wanted to make the eap-newsgroup a little
bit more active ;)

>I had to scavenge some other votes in the process though... More VOTE more Vote!)

Yes, the votes are a serious problem. I think, JIRA has made it
better: you can vote on any issue with only one vote.

Tom

0
Comment actions Permalink

Chris, these were examples about what IDEA should be able to do, no
examples of readable code.

Tom

0
Comment actions Permalink

I wanted this feature for a long time.

I am not an OpenAPI expert, but anyway one can make this as a plugin ? Does
OpenAPI provide hook for the little light bulb ?

-Dash.

"Thomas Singer" <fake@fake.com> wrote in message
news:oprora8tyw3im8zg@news.intellij.net...

What would you think about the ability to refactor boolean expressions?

>

For instance:
- extract !

>

(value <= 1) => !(value > 1)

>

- inline !

>

!(value == null) => (value != null)

>

- change && to ||

>

(a && b) => !(!a || !b)

>

- change || to &&

>

(a || b) => !(!a && !b)

>

I can imagine, that the most intuitive solution would be intentions.

>

Tom



0
Comment actions Permalink


All of these are good, but should be intentions
rather than refactorings. The DeMorgan's Laws in
particular would be fine intentions.


Careful here! DeMorgan does not take side effects into account:


Regards, Jens

0
Comment actions Permalink

My ideas already went in the intentions direction. Currently I'm just
unsure, where they should invoked.

Tom

0
Comment actions Permalink

Thomas Singer wrote:

My ideas already went in the intentions direction. Currently I'm just
unsure, where they should invoked.


IMO they shopuld be invoked when the caret is next to or in the boolean operator.

If the operator is && or || the intention would suggest DeMorgan transformation.
If the operator is ! the intention should suggest to double invert the expression.

-- dimiter

0
Comment actions Permalink

And what, if you want to extract the ! ? Invoked on the opening ( of a
boolean expression?

Tom


On Wed, 07 May 2003 11:58:49 +0300, dimiter <dimiter@blue-edge.bg> wrote:

Thomas Singer wrote:

>> My ideas already went in the intentions direction. Currently I'm just
>> unsure, where they should invoked.
>

IMO they shopuld be invoked when the caret is next to or in the boolean
operator.

>

If the operator is && or || the intention would suggest DeMorgan
transformation.
If the operator is the intention should suggest to double invert the
expression.

>

-- dimiter

>
>


0
Comment actions Permalink

Thanks for the pointer!

There seems to be a usability issue here.

Neil

On Tue, 06 May 2003 14:06:35 -0400, Gordon Tyler
<gordon.tyler@sitraka.com> wrote:

>Neil Galarneau wrote:
>> It was a convoluted if statement:
>> if (negative expression)
>> xyz;
>> else
>> abc;
>>
>> and I wanted to make it a positive one:
>> if (positive expression)
>> abc;
>> else
>> xyz;
>>
>> I looked for a refactoring & was surprised when I didn't find one.
>
>There is an Intention for this instead. Position the caret on the 'if'
>keyword and if you have Intentions enabled you'll get a popup with
>"Invert If condition". You can also force the popup to display by
>pressing Alt-Enter if you have it turned off (like I do).
>
>Ciao,
>Gordon

0
Comment actions Permalink

After seeing some of the later posts after mine, I retract my original statement. I can see why this is a good idea, and why you want to prevent the developer from doing this manually.

0
Comment actions Permalink

Hi all,

Supposing we open an interface for intention actions on the same terms as
PSI, will there be undertakers for such a plugin?
We think it is rather a good opportunity for testing PSI interfaces and
figuring out the state of PSI w.r.t. its becoming an official OpenAPI,

Friendly,
Dmitry

Debabrata Dash wrote:

I wanted this feature for a long time.

I am not an OpenAPI expert, but anyway one can make this as a plugin ?
Does OpenAPI provide hook for the little light bulb ?

-Dash.

"Thomas Singer" <fake@fake.com> wrote in message
news:oprora8tyw3im8zg@news.intellij.net...

>> What would you think about the ability to refactor boolean expressions?
>>
>> For instance:
>> - extract !
>>
>> (value <= 1) => !(value > 1)
>>
>> - inline !
>>
>> !(value == null) => (value != null)
>>
>> - change && to ||
>>
>> (a && b) => !(!a || !b)
>>
>> - change || to &&
>>
>> (a || b) => !(!a && !b)
>>
>> I can imagine, that the most intuitive solution would be intentions.
>>
>> Tom

--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

I have no doubt there will be. And that eventually there will be many different intention-based plugins if you open the API (even as experimental and non-officially published). I agree that it could be a good opportunity for the additional testing of the PSI API since looks like they should be closely related (with intentions depending on PSI and heavily using it).


Timur

Hi all,

Supposing we open an interface for intention actions
on the same terms as
PSI, will there be undertakers for such a plugin?
We think it is rather a good opportunity for testing
PSI interfaces and
figuring out the state of PSI w.r.t. its becoming an
official OpenAPI,

Friendly,
Dmitry

0
Comment actions Permalink

>Supposing we open an interface for intention actions on >the same terms as
>PSI, will there be undertakers for such a plugin?

Good god yes! Here's an "off the top of my head" list of intentions that I would commit to code up, if the API where open and of adequate quality

"Create subclass": still can't believe you guys don't do this

"Convert And to Or": foo && bar -> !(!foo) || (!bar))
"Convert Or to And": foo || bar -> !(!foo) && (!bar))
"Invert Comparison": !(foo < bar) -> foo >= bar
(all three of the previous ones should also remove any double negations they create, and add no more parentheses than necessary)

"Convert ternary conditional to if": applicable if the ternary op is on the RHS of an assignment or a return;

"Convert if to ternary conditional": applicable if the if has an else branch and both branches are either "return" or assignment to the same variable.

"Convert if to switch"
"Convert switch to if"

"Box primitive value": 3 -> new Integer(3);
applicable in cases where an primitive value is used but an object value is expected.

"Parse string value: "3" -> Integer.parseInt("3")
applicable in cases where you have a string and need a primitive value.

"Convert == to 'equals'": "3" == bar -> "3".equals(bar)

"Reverse 'equals'": bar.equals("3") ->"3".equals(bar)

--Dave Griffith

0
Comment actions Permalink

"Dmitry Lomov" <dsl@intellij.com> wrote

Supposing we open an interface for intention actions on the same terms as
PSI, will there be undertakers for such a plugin?
We think it is rather a good opportunity for testing PSI interfaces and
figuring out the state of PSI w.r.t. its becoming an official OpenAPI,


Yesssss. A (Semi-)OpenAPI for intentions would be great. No doubt
that this would be the start of many new wonderful plugins.


Sascha


0
Comment actions Permalink

+100. This would be a very good decision.

/kesh

"Sascha Weinreuter" <sascha.weinreuter@cit.de> wrote in message
news:b9dq8d$8nc$1@is.intellij.net...

"Dmitry Lomov" <dsl@intellij.com> wrote

Supposing we open an interface for intention actions on the same terms

as

PSI, will there be undertakers for such a plugin?
We think it is rather a good opportunity for testing PSI interfaces and
figuring out the state of PSI w.r.t. its becoming an official OpenAPI,

>

Yesssss. A (Semi-)OpenAPI for intentions would be great. No doubt
that this would be the start of many new wonderful plugins.

>
>

Sascha

>
>


0
Comment actions Permalink

Dmitry,

If you were looking for a commitment by someone to use an intention-action
API in a plugin, I'll commit to writing one that does some of these boolean
expression refactorings. I have some experience with PSI after enhancing
the tabifier plugin. I'd also be happy to work with IntelliJ on improving
documentation for PSI.

-Dave

"Dmitry Lomov" <dsl@intellij.com> wrote in message
news:b9d5on$i49$1@is.intellij.net...

Hi all,

>

Supposing we open an interface for intention actions on the same terms as
PSI, will there be undertakers for such a plugin?
We think it is rather a good opportunity for testing PSI interfaces and
figuring out the state of PSI w.r.t. its becoming an official OpenAPI,

>

Friendly,
Dmitry

>

Debabrata Dash wrote:

>

I wanted this feature for a long time.

>

I am not an OpenAPI expert, but anyway one can make this as a plugin ?
Does OpenAPI provide hook for the little light bulb ?

>

-Dash.

>

"Thomas Singer" <fake@fake.com> wrote in message
news:oprora8tyw3im8zg@news.intellij.net...

>> What would you think about the ability to refactor boolean expressions?
>>
>> For instance:
>> - extract !
>>
>> (value <= 1) => !(value > 1)
>>
>> - inline !
>>
>> !(value == null) => (value != null)
>>
>> - change && to ||
>>
>> (a && b) => !(!a || !b)
>>
>> - change || to &&
>>
>> (a || b) => !(!a && !b)
>>
>> I can imagine, that the most intuitive solution would be intentions.
>>
>> Tom

>

--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"



0
Comment actions Permalink

Dave Griffith wrote:

]]>

"Parse string value: "3" -> Integer.parseInt("3")
applicable in cases where you have a string and need a primitive value.


I have a Feature Request open for this:
http://www.intellij.net/tracker/idea/viewSCR?publicId=10770

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919

0

Please sign in to leave a comment.