Invert "if" statement to reduce nesting

Resharper suggest inverting the "if" statement on the following code.


      if (id > 0)
      {
        var result = PersonLog.Select(id);
        Assert.AreEqual(id, result.Id);
        Assert.IsTrue(CommonData.IsNotBlank(result.DateAdded));
        Assert.IsTrue(CommonData.IsNotBlank(result.IPAddress));
        Assert.IsTrue(result.PersonId > 0);
      }

Accepting the suggestion produces:

      if (id <= 0)
      {
        return;
      }
      var result = PersonLog.Select(id);
      Assert.AreEqual(id, result.Id);
      Assert.IsTrue(CommonData.IsNotBlank(result.DateAdded));
      Assert.IsTrue(CommonData.IsNotBlank(result.IPAddress));
      Assert.IsTrue(result.PersonId > 0);

This is not quite correct as StyleCop requires a blank line after the last brace. The correct code is:

      if (id <= 0)
      {
        return;
      }

      var result = PersonLog.Select(id);
      Assert.AreEqual(id, result.Id);
      Assert.IsTrue(CommonData.IsNotBlank(result.DateAdded));
      Assert.IsTrue(CommonData.IsNotBlank(result.IPAddress));
      Assert.IsTrue(result.PersonId > 0);

6 comments
Comment actions Permalink

My resharper defaults have been adjusted by stylecop for compatibility.

0
Comment actions Permalink

Hello,

If you'll run Code Cleanup (with Reformat code profile) on the resulting
code, is the necessary line break added? Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

My resharper defaults have been adjusted by stylecop for
compatibility.

---
Original message URL:
http://devnet.jetbrains.net/message/5304284#5304284



0
Comment actions Permalink

With the block:
      if (c == '\r' && this._delimiter != '\r')
      {
        return true;
      }
      else
        return false;

Remove redundant else gives:
      if (c == '\r' && this._delimiter != '\r')
      {
        return true;
      }
      return false;

Clean up Code - reformat gives:
      if (c == '\r' && this._delimiter != '\r')
      {
        return true;
      }
      return false;

Cleanup Code - full reformat gives:
      if (c == '\r' && this._delimiter != '\r')
      {
        return true;
      }
      return false;

Cleanup code - stylecop gives:
      if (c == '\r' && this._delimiter != '\r')
      {
        return true;
      }
      return false;

... and what is required is:
      if (c == '\r' && this._delimiter != '\r')
      {
        return true;
      }

      return false;

0
Comment actions Permalink

Hello,

At the moment there's no way to force a blank line after an 'if' statement.
You're welcome to put a feature request through http://youtrack.jetbrains.net/issues/RSRP.
Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

With the block:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
else
return false;
Remove redundant else gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
Clean up Code - reformat gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
Cleanup Code - full reformat gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
Cleanup code - stylecop gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
.. and what is required is:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;

---
Original message URL:
http://devnet.jetbrains.net/message/5304424#5304424



0
Comment actions Permalink

RSRP-26082 Additional "Blank Lines" styles

"Andrew Serebryansky"  wrote in message
news:c8a898dd1238e8cdea81898be8fd@news.intellij.net...

Hello,

At the moment there's no way to force a blank line after an 'if' statement.
You're welcome to put a feature request through
http://youtrack.jetbrains.net/issues/RSRP.
Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

With the block:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
else
return false;
Remove redundant else gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
Clean up Code - reformat gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
Cleanup Code - full reformat gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
Cleanup code - stylecop gives:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;
.. and what is required is:
if (c == '\r' && this._delimiter != '\r')
{
return true;
}
return false;

>

---
Original message URL:
http://devnet.jetbrains.net/message/5304424#5304424


0
Comment actions Permalink

Hello Greg,

Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

RSRP-26082 Additional "Blank Lines" styles

"Andrew Serebryansky"  wrote in message
news:c8a898dd1238e8cdea81898be8fd@news.intellij.net...

Hello,

At the moment there's no way to force a blank line after an 'if'
statement.
You're welcome to put a feature request through
http://youtrack.jetbrains.net/issues/RSRP.
Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>> With the block:
>> if (c == '\r' && this._delimiter != '\r')
>> {
>> return true;
>> }
>> else
>> return false;
>> Remove redundant else gives:
>> if (c == '\r' && this._delimiter != '\r')
>> {
>> return true;
>> }
>> return false;
>> Clean up Code - reformat gives:
>> if (c == '\r' && this._delimiter != '\r')
>> {
>> return true;
>> }
>> return false;
>> Cleanup Code - full reformat gives:
>> if (c == '\r' && this._delimiter != '\r')
>> {
>> return true;
>> }
>> return false;
>> Cleanup code - stylecop gives:
>> if (c == '\r' && this._delimiter != '\r')
>> {
>> return true;
>> }
>> return false;
>> .. and what is required is:
>> if (c == '\r' && this._delimiter != '\r')
>> {
>> return true;
>> }
>> return false;
>> ---
>> Original message URL:
>> http://devnet.jetbrains.net/message/5304424#5304424


0

Please sign in to leave a comment.