CheckParametersNullityInVisibleMethodsRule

53 views
Skip to first unread message

Vlad

unread,
Aug 9, 2011, 1:54:53 PM8/9/11
to Gendarme
Hi,

I have coding habits that come from C which seem to be violating some
C# style guideline.

I prefer writing:

if (null == foo)
{
DoStuff();
}

But it seems the rule does not consider the above as good a null check
as

if (foo == null)

Which I consider bad because it is all too easy to drop an =. C# only
allows Booleans to be there, so should I change my convention?.

Bevan Arps

unread,
Aug 9, 2011, 3:22:46 PM8/9/11
to gend...@googlegroups.com
On 10/08/2011 5:54 a.m., Vlad wrote:
But it seems the rule does not consider the above as good a null check
as

if (foo == null)

Which I consider bad because it is all too easy to drop an =.  C# only
allows Booleans to be there, so should I change my convention?.

In C#, bool is a real type, not just a convention on treatment of bit patterns as it is in C.

This means that C# is much safer - in the example you give above, inadvertently writing assignment "=" instead of equality "==" gives a compiler error.

Another side effect of having a real bool type is that testing against true and false isn't needed. So instead of

    if (string.IsNullOrEmpty(foo) == true)

You can (and should) write

    if (string.IsNullOrEmpty(foo))

As a part of my own migration to C#, I've dropped that particular practice because (I in my opinion) it makes code harder to read for no benefit. (vs the very real benefit it has for C code).

Just my 2c,
Bevan.

Alex

unread,
Aug 9, 2011, 3:03:26 PM8/9/11
to gend...@googlegroups.com
Hi,

IMO, change your convention. This mistake is only possible in C# if
foo is a boolean, but in that case you shouldn't even be doing this to
begin with, but just a simple if (foo) or whatever.

Regards,
Alex

> --
> You received this message because you are subscribed to the Google Groups "Gendarme" group.
> To post to this group, send email to gend...@googlegroups.com.
> To unsubscribe from this group, send email to gendarme+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/gendarme?hl=en.
>
>

Oleg Deribas

unread,
Sep 5, 2011, 11:20:09 AM9/5/11
to gend...@googlegroups.com
Hi,


On Tuesday, August 9, 2011 7:54:53 PM UTC+2, Vlad wrote:
 
if (null == foo)
{
  DoStuff();
}

Also known as Yoda conditions.

--
Oleg
 
Reply all
Reply to author
Forward
0 new messages