Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is respective branch? (suggested by ReSharper)

I am using ReSharper to refactor my code, and one of the suggestions of ReSharper is to use a respective branch instead a if statement that is always true.

If I have

if (myVar != null){
   //code
}

And I apply the respective branch the code is like the following

{
   //code
}

Is this a short cut of an if statement? What is this used for? What is the difference between the cutely brackets and nothing?

Thanks in advance.

like image 582
Amra Avatar asked Jun 21 '11 15:06

Amra


2 Answers

The block is required to stay there, or semantics would change.

The scope of variables declared inside the branch block would change when you remove the braces. Potentially with conflicting/hiding (lambda) variables this would lead to surprises.

To remove the braces, position the cursor on either one and hit Alt-Enter, 'Remove Braces'

Reintroduce braces: select block of code, Ctrl-Alt-J, 7 to surround with block :)

like image 140
sehe Avatar answered Sep 25 '22 22:09

sehe


In ReSharper 6.0 this command also removes braces if you don't have variable declarations inside.

like image 43
Dmitry Osinovskiy Avatar answered Sep 25 '22 22:09

Dmitry Osinovskiy