Feature request: refactor if-statement that uses booleans in the condition

I have the following code:

 4669 if (isCurrencyFound == false)
 4670 {
 4671     mainCurrencyCode = magazineCurrency;
 4672 }



I want to refactor the if-statement to this:

 4669 if (!isCurrencyFound)
 4670 {
 4671     mainCurrencyCode = magazineCurrency;
 4672 }



But this does not seem to be possible, or is it?

9 comments
Comment actions Permalink

Hello Gabriel Lozano-Moran,

But this does not seem to be possible, or is it?


Try to use new feature of R# 5.0 - "Structured Patterns".

Best regards,
Alexander Nesterenko
ReSharper developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Hi,

I would really appreicate it you could please demonstrate how to use the structured search / patterns catalogue / search with pattern in R#5?

Showing how to search for the above would be great, as well as other scenarios

Thanks!
Dominique

0
Comment actions Permalink

Hello,

Search pattern should be something like this:

if ($expr$ == false) $stmt$
where
expr is expression of type boolean
stmt is 1 or more statements

Replace pattern would be
if (!$expr$) $stmt$

PS: This sounds like a good sample to bundle in Patterns Catalogue, thanks!

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


IR> Hi,
IR>
IR> I would really appreicate it you could please demonstrate how to use
IR> the structured search / patterns catalogue / search with pattern in
IR> R#5?
IR>
IR> Showing how to search for the above would be great, as well as other
IR> scenarios
IR>
IR> Thanks!
IR> Dominique
IR> ---
IR> Original message URL:
IR> http://www.jetbrains.net/devnet/message/5256238#5256238


0
Comment actions Permalink

Thanks for demonstrating this!

Based on your explanation, I was able to define the placeholders and define a search pattern.

Here's a little bit of feedback on the placeholder dialogs:

For expressions, you might want to put some very common expression types in the drop down list (int, bool, object, etc..)

For statement placeholders, I found it a little bit unintuitive to use checkboxes to specify "at least x" and "at most x", maybe a drop down list for common selections might make it more intutiive and that interface as "advanced" for more "advanced" selections.

Also, it wasn't intutitive that you had to surround placeholders with "$" characters, and if you mess up, the error "can not parse pattern" is not very helpful.

Also, it would be cool to drag and drop placeholders in the search patterns

Another thing I noticed:

I defined a pattern to search for blocks of more than 25 statements, and I noticed that say in a method that had 30 statements, it would report this 5 times - it might be overcalculating in some cases  

These are all improvement suggestions, it's awesome to see these feature in this tool!!

Thanks,

Dominique

0
Comment actions Permalink

Hi, Ilya.

I still could not find a promised patterns documentation, so it caused me having a following question/scenario:

I would replace this:

     if (argument == null)

          throw new ArgumentException("argument schould not be null", "argument");

to this:

     if (argument == null)

          throw new ArgumentNullException("argument");

i have tried following without success:

     if ($argument$ == null) throw new $exp$($s$)

where argument - expression of type object or derived; exp - type "System.ArgumentExpression"; s - any number of arguments.

The Resharper can parse and find code only with a last part:

     new $exp$($s$)

And a question: is it (will be) such thing even possible with patterns? or at least to find a first expressions?

0
Comment actions Permalink

Hi, Andreas,

Your first pattern can not be found, because you forgot semicolon at the end. Second pattern runs fine, because its parsed as expression, while first one is a statement, and can not be parsed as expression.

Also I suggest you change your first pattern to

    if ($argument$ == null) throw new $exp$($msg$, $param$);


Where $msg$ and $param$ are expressions of type System.String. And therefore your replace pattern will be:

    if ($argument$ == null) throw new $exp$($param$);


This works as expected.
0
Comment actions Permalink

For expressions, you might want to put some very common expression types in the drop down list (int, bool, object, etc..)

You can easily filter this list inplace, like in any other type selection field in R#, just type "int" and you'll see.

For statement placeholders, I found it a little bit unintuitive to use checkboxes to specify "at least x" and "at most x", maybe a drop down list for common selections might make it more intutiive and that interface as "advanced" for more "advanced" selections.

Thanks for suggestion, we will consider it for future versions.

Also, it wasn't intutitive that you had to surround placeholders with "$" characters, and if you mess up, the error "can not parse pattern" is not very helpful.

We will provide error highlighting later.

Also, it would be cool to drag and drop placeholders in the search patterns ;)

It has been already suggested, you can vote for this task in our issue tracker.

I defined a pattern to search for blocks of more than 25 statements, and I noticed that say in a method that had 30 statements, it would report this 5 times - it might be overcalculating in some cases  :)

I just tried to search "{$stmt$}", where stmt - is "at least 25 statements" and got every long method only once in search results. How did you get it?

Thanks for your feedback!

0
Comment actions Permalink

Very good! Big thanks, it works.
Next question:
Can i define a pattern to replace something like

$enumerable$.Where($condition$).FirstOrDefault()

with something like

$enumerable$.FirstOrDefault($condition$) 

???

0
Comment actions Permalink

Ok, if somebody else interested in how to accomplish this:
$enumerable$ - expression of System.Collections.IEnumerable or derived
$condition$ - did not work for me as expression, so i have it defined as an argument

The pattern search it the great and very useful feature, but needs some improvements
It bothers me that i can not replace a selected pattern part with Ctl+V or simply typing. I must use backspase.
Very useful would be "Create pattern from selection" option.
And some more improvement suggestions:
, e.g. if i rename a pattern parameter it should be also renamed in the pattern text and vice versa, may be something like "refactor rename"?
string with regex as a pattern parameter working similar to Find in text from Power toys.

0

Please sign in to leave a comment.