Editing potential errors and bad practices

Hello,

I´m new to ReSharper and was playing around with the Code Inspection settings.
There are really cool features in the "potential errors & bad practices" section.

But I am missing a feature to check, if i really use the equals method instead of "==" for object.

E.g. if I write code like:

String s = "";

if(s=="")


I want ReSharper to underline the "==" and suggest "s.equals("")" instead.


Is there a way to do this? If yes, how can I do this?

Or did anybody added this functionality before and can sent me a copy of it?


Regards,
Martin

4 comments
Comment actions Permalink

Hi Martin,

>

String s = "";

>

if(s=="")

>
>

I want ReSharper to underline the "==" and suggest "s.equals("")" instead.

>

I think the appropiate code (best practice) here would be

if (string.IsNullOrEmpty(s)) DoSomething();

Additionally, s might be NULL which would cause s.Equals("") to throw a
NullReferenceException.

What I want to say: The Equals method is not always the better way, so R#
should not underline it every time... Combined with the "Possible Null
Reference" analysis this could work, though.

Kind regards,
Henning

0
Comment actions Permalink

Indeed, the static String.Equals(String, String, StringComparison) method is
generally the best practice for string comparisons in my opinion (in most
cases using StringComparison.InvariantCulture), as it eliminates the
potential for null reference exceptions. And as Henning points out,
String.IsNullOrEmpty(String) is ideal when checking for a valid (non-empty)
value rather than a specific value.

And for the love of Bob don't use "if (String.Compare(s1, s2) == 0) {
... }". I see that all the time in our other developers' code, and it
drives me nuts. String.Compare is meant for determining culture-specific
sort order, not for evaluating equality.

Apologies for the rant--I've had to correct this stuff far too many times
:).


"Henning Krause" <newsgroups_remove@this.infinitec.de> wrote in message
news:fvf4jv$dp2$1@is.intellij.net...

I think the appropiate code (best practice) here would be

>

if (string.IsNullOrEmpty(s)) DoSomething();

>

Additionally, s might be NULL which would cause s.Equals("") to throw a
NullReferenceException.

>

What I want to say: The Equals method is not always the better way, so R#
should not underline it every time... Combined with the "Possible Null
Reference" analysis this could work, though.

>

Kind regards,
Henning



0
Comment actions Permalink

Hi Mike,

"Mike Strobel" <mstrobel@cdcsoftware.com> wrote in message
news:fvfe80$1cg$1@is.intellij.net...

Indeed, the static String.Equals(String, String, StringComparison) method
is generally the best practice for string comparisons in my opinion (in
most cases using StringComparison.InvariantCulture), as it eliminates the
potential for null reference exceptions. And as Henning points out,


Using InvariantCulture is also not best practices - use the Ordnial one...
see http://blogs.msdn.com/abhinaba/archive/2005/10/28/486173.aspx

Kind regards,
Henning Krause

0
Comment actions Permalink

Whoops, quite right. My current project has me using InvariantCulture
comparisons a lot, so I guess I just had that one on my mind. Ordinal is
usually the way to go when you want to test string equality.

"Henning Krause" <newsgroups_remove@this.infinitec.de> wrote in message
news:fvfgn6$qfk$1@is.intellij.net...

Hi Mike,

>

"Mike Strobel" <mstrobel@cdcsoftware.com> wrote in message
news:fvfe80$1cg$1@is.intellij.net...

>> Indeed, the static String.Equals(String, String, StringComparison) method
>> is generally the best practice for string comparisons in my opinion (in
>> most cases using StringComparison.InvariantCulture), as it eliminates the
>> potential for null reference exceptions. And as Henning points out,
>

Using InvariantCulture is also not best practices - use the Ordnial one...
see http://blogs.msdn.com/abhinaba/archive/2005/10/28/486173.aspx

>

Kind regards,
Henning Krause


0

Please sign in to leave a comment.